<?php

namespace App\Models\Part;

use App\Entities\Part\IPEntity;

class IPModel extends PartModel
{
    const TABLE = "ipinfo";
    const PK = "uid";
    const TITLE = "ip";
    protected $table            = self::TABLE;
    protected $primaryKey       = self::PK;
    protected $returnType       = IPEntity::class;
    protected $allowedFields = [
        "uid",
        "lineinfo_uid",
        "old_clientinfo_uid",
        "clientinfo_uid",
        "serviceinfo_uid",
        "serverinfo_uid",
        "ip",
        "price",
        "status",
        "updated_at"
    ];
    public function __construct()
    {
        parent::__construct();
    }
    public function getFormRule(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":
                $rule  = "required|numeric";
                break;
            case "old_clientinfo_uid":
            case "clientinfo_uid":
            case "serviceinfo_uid":
            case "serverinfo_uid":
                $rule = "permit_empty|numeric";
                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;
            case "status":
                $rule  = "required|trim|string";
                break;
            default:
                $rule = parent::getFormRule($action, $field);
                break;
        }
        return $rule;
    }
}
