<?php

namespace App\Models\Equipment;

use App\Entities\Equipment\CSEntity;

class CSModel extends EquipmentModel
{
    const TABLE = "csinfo";
    const PK = "uid";
    const TITLE = "ip";
    protected $table            = self::TABLE;
    protected $primaryKey       = self::PK;
    protected $returnType       = CSEntity::class;
    protected $allowedFields = [
        "clientinfo_uid",
        "serviceinfo_uid",
        "serverinfo_uid",
        "type",
        "ip",
        "accountid",
        "domain",
        "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 "type":
                $rule = "required|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->getTable() . "." . $field . "]" : "";
                break;
            case "amount":
                $rule  = "required|numeric";
                break;
            case "accountid":
            case "domain":
                $rule = "permit_empty|trim|string";
                break;
            default:
                $rule = parent::getFormFieldRule($action, $field);
                break;
        }
        return $rule;
    }
}
