<?php

namespace App\Models\Equipment;

use App\Entities\Equipment\ServerEntity;

class ServerModel extends EquipmentModel
{
    const TABLE = "serverinfo";
    const PK = "uid";
    const TITLE = "title";
    protected $table            = self::TABLE;
    // protected $useAutoIncrement = false;
    protected $primaryKey       = self::PK;
    protected $returnType       = ServerEntity::class;
    protected $allowedFields = [
        "uid",
        "user_uid",
        "clientinfo_uid",
        "serviceinfo_uid",
        "code",
        "type",
        "switch",
        "ip",
        "os",
        "title",
        "price",
        "manufactur_at",
        "format_at",
        "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 "user_uid":
            case "clientinfo_uid":
            case "serviceinfo_uid":
                $rule  = "permit_empty|numeric";
                break;
            case "code":
            case "title":
                $rule = "required|trim|string";
                break;
            case "price":
                $rule  = "required|numeric";
                break;
            case "type":
            case "status":
                $rule = "required|trim|string";
                break;
            case "manufactur_at":
                $rule = "required|valid_date";
                break;
            case "switch":
                $rule = "permit_empty|trim|string";
                $rule .= in_array($action, ["create", "create_form"]) ? "|is_unique[{$this->table}.{$field}]" : "";
                break;
            case "ip":
                $rule = "permit_empty|trim|valid_ip[both]"; //ipv4 , ipv6 , both(ipv4,ipv6)
                $rule .= in_array($action, ["create", "create_form"]) ? "|is_unique[{$this->table}.{$field}]" : "";
                break;
            case "os":
                $rule = "permit_empty|trim|string";
                break;
            case "format_at":
                $rule = "permit_empty|valid_date";
                break;
            default:
                $rule = parent::getFormRule($action, $field);
                break;
        }
        return $rule;
    }
    protected function convertFormDatas(string $action, string $field, array $formDatas): mixed
    {
        // 필드 값 존재 여부 확인
        $value = array_key_exists($field, $formDatas) ? $formDatas[$field] : null;
        switch ($field) {
            case 'clientinfo_uid':
            case 'serviceinfo_uid':
                if ($value === '') {
                    $value = null;
                }
                break;
            default:
                $value = parent::convertFormDatas($action, $field, $formDatas);
                break;
        }
        return $value;
    }
    //기본기능
    //입력전 코드처리
    final public function create(array $formDatas): ServerEntity
    {
        // 관리자 UID는 현재 인증된 사용자로 설정
        $formDatas['user_uid'] = $this->getMyAuth()->getUIDByAuthInfo();
        return parent::create($formDatas);
    }
}
