<?php

namespace App\Models\Customer;

use App\Entities\Customer\ServiceEntity;

class ServiceModel extends CustomerModel
{
    const TABLE = "serviceinfo";
    const PK = "uid";
    const TITLE = "rack";
    protected $table            = self::TABLE;
    protected $primaryKey       = self::PK;
    protected $returnType       = ServiceEntity::class;
    protected $allowedFields = [
        "clientinfo_uid",
        "lineinfo_uid",
        "serverinfo_uid",
        "rack",
        "type",
        "billing_at",
        "start_at",
        "end_at",
        "status",
        "updated_at"
    ];
    public function __construct()
    {
        parent::__construct();
    }
    public function getFieldRule(string $action, string $field): string
    {
        if (is_array($field)) {
            throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true));
        }
        switch ($field) {
            case "clientinfo_uid":
            case "lineinfo_uid":
            case "serverinfo_uid":
                $rule  = "required|numeric";
                break;
            case "rack":
                $rule = "required|trim|min_length[4]|max_length[20]";
                $rule .= in_array($action, ["create", "create_form"]) ? "|is_unique[{$this->table}.{$field}]" : "";
                break;
            case "type":
                $rule = "required|trim|string";
                break;
            case "billing_at":
            case "start_at":
                $rule = "required|valid_date";
                break;
            case "end_at":
                $rule = "if_exist|valid_date";
            default:
                $rule = parent::getFieldRule($action, $field);
                break;
        }
        return $rule;
    }
}
