<?php

namespace App\Models\Equipment;

use App\Entities\Equipment\IPEntity;

class IPModel extends EquipmentModel
{
    const TABLE = "ipinfo";
    const PK = "uid";
    const TITLE = "ip";
    protected $table            = self::TABLE;
    protected $primaryKey       = self::PK;
    protected $returnType       = IPEntity::class;
    protected $allowedFields = [
        "lineinfo_uid",
        "old_clientinfo_uid",
        "clientinfo_uid",
        "serviceinfo_uid",
        "serverinfo_uid",
        "ip",
        "price",
        "amount",
        "status",
        "updated_at"
    ];
    public function __construct()
    {
        parent::__construct();
    }
    public function getFormFieldRule(string $action, string $field): string
    {
        if (is_array($field)) {
            throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true));
        }
        switch ($field) {
            case "lineinfo_uid":
            case "price":
            case "amount":
                $rule  = "required|numeric";
                break;
            case "old_clientinfo_uid":
                $rule = "permit_empty|numeric";
                break;
            case "status":
                $rule  = "permit_empty|trim|string";
                break;
            case "ip":
                $rule = "required|trim|valid_ip[both]"; //ipv4 , ipv6 , both(ipv4,ipv6)
                $rule .= in_array($action, ["create", "create_form"]) ? "|is_unique[{$this->table}.{$field}]" : "";
                break;
            default:
                $rule = parent::getFormFieldRule($action, $field);
                break;
        }
        return $rule;
    }
}
