<?php

namespace App\Models;

use App\Entities\BoardConfigEntity;

class BoardConfigModel extends BaseModel
{
    protected $table = "tw_board_config";
    protected $useAutoIncrement = false;
    protected $returnType = BoardConfigEntity::class;
    public function __construct()
    {
        parent::__construct('BoardConfig');
        $this->allowedFields = [
            ...$this->allowedFields,
            'name', "isaccess", "isread", "iswrite", "isreply", "isupload", "isdownload",
            "head", "tail", "status"
        ];
        $this->validationRules = [...$this->validationRules, ...$this->getFieldRules($this->allowedFields),];
    }
    public function getTitleField(): string
    {
        return 'name';
    }
    protected function getFieldRule(string $field, array $rules, string $action = ""): array
    {
        switch ($field) {
            case "uid":
                $rules[$field] = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";
                $rules[$field] .= $action == "insert" ? "|is_unique[{$this->table}.{$field}]" : "";
                break;
            case $this->getTitleField():
                $rules[$field] = "required|trim|string";
                $rules[$field] .= $action == "insert" ? "|is_unique[{$this->table}.{$field}]" : "";
                break;
            case "isaccess":
            case "isread":
            case "iswrite":
            case "isreply":
            case "isupload":
            case "isdownload":
                //아래 Rule은 입력시에는 되는데 수정시에는 않됨 이유를 ?
                // $rules[$field] =   "required|in_list[master,director,cloudflare,manager,gold,silver,brone,vip,user,guest]";
                //아래 Rule은 checkbox를 사용시에는 required만 우선 써야 수정시 validate문제없음
                $rules[$field] = "required";
                break;
            default:
                $rules = parent::getFieldRule($field, $rules, $action);
                break;
        }
        return $rules;
    }
    //Form 선택용 Options Data용
    public function getOptions(array $conditions = array(), $options = array()): array
    {
        foreach ($this->getEntitys($conditions) as $entity) {
            // STATUS가 use가 아닐때 option을 disabled되게 하기위함
            if ($entity->getStatus() != DEFAULTS['STATUS']) {
                $options[$entity->getPrimaryKey() . "\" disabled=\"disabled"] = $entity->getTitle();
            } else {
                $options[$entity->getPrimaryKey()] = $entity->getTitle();
            }
        }
        return $options;
    }

    //Field별 Form Option용
    protected function changeFormData(string $action, string $field, array $formDatas, $entity)
    {
        switch ($field) {
            case "isaccess":
            case "isread":
            case "iswrite":
            case "isreply":
            case "isupload":
            case "isdownload":
            case "isaccess":
                if (array_key_exists($field, $formDatas) && !is_null($formDatas[$field])) {
                    $entity->$field = is_array($formDatas[$field]) ? implode(DEFAULTS['DELIMITER_ROLE'], $formDatas[$field]) : $formDatas[$field];
                }
                break;
            case "head":
            case "tail":
                if (array_key_exists($field, $formDatas) && !is_null($formDatas[$field])) {
                    $entity->$field = htmlentities($formDatas[$field]);
                }
                break;
            default:
                $entity = parent::changeFormData($action, $field, $formDatas, $entity);
                break;
        }
        return $entity;
    }

    public function getEntity($conditions): BoardConfigEntity
    {
        return parent::getEntity($conditions);
    }
    public function create(array $formDatas): BoardConfigEntity
    {
        return $this->create_process(new BoardConfigEntity(), $formDatas);
    }
    public function modify(BoardConfigEntity $entity, array $formDatas): BoardConfigEntity
    {
        return $this->modify_process($entity, $formDatas);
    }

    //Index관련
    public function setIndexWordFilter(string $word)
    {
        parent::setIndexWordFilter($word);
        $this->orLike($this->getTitle(), $word, "both"); //befor , after , both
        $this->orLike("isaccess", $word, "both"); //befor , after , both
        $this->orLike("isread", $word, "both"); //befor , after , both
        $this->orLike("iswrite", $word, "both"); //befor , after , both
        $this->orLike("isreply", $word, "both"); //befor , after , both
        $this->orLike("isupload", $word, "both"); //befor , after , both
        $this->orLike("isdownload", $word, "both"); //befor , after , both
    }
}
