<?php

namespace App\Models\Customer;

use App\Entities\Customer\ClientEntity;

class ClientModel extends CustomerModel
{
    const TABLE = "clientinfo";
    const PK = "uid";
    const TITLE = "name";
    protected $table            = self::TABLE;
    protected $primaryKey       = self::PK;
    protected $returnType       = ClientEntity::class;
    protected $allowedFields = [
        "name",
        "email",
        "phone",
        "role",
        "account_balance",
        "coupon_balance",
        "point_balance",
        "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 "name":
            case "role":
                $rule  = "required|trim|string";
                break;
            case "email":
                $rule = "if_exist|trim|valid_email";
                $rule .= in_array($action, ["create", "create_form"]) ? "|is_unique[{$this->table}.{$field}]" : "";
                break;
            case "account_balance":
            case "coupon_balance":
            case "point_balance":
                $rule  = "if_exist|numeric";
                break;
            default:
                $rule = parent::getFormFieldRule($action, $field);
                break;
        }
        return $rule;
    }
    //List 검색용
    public function setList_WordFilter(string $word): void
    {
        $this->orLike(self::TABLE . '.email', $word, 'both');
        parent::setList_WordFilter($word);
    }
}
