<?php

namespace App\Models;

use App\Entities\BoardConfigEntity;

class BoardConfigModel extends BaseModel
{
    protected $table            = 'tw_board_config';
    // protected $primaryKey       = 'uid';
    protected $useAutoIncrement = false;
    protected $allowedFields    = ['uid', 'name', 'isaccess', 'isread', 'iswrite', 'isreply', 'isupload', 'isdownload', 'head', 'tail', 'status', 'updated_at'];
    protected $validationRules      = [
        'uid'               => 'required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]',
        'name'              => 'required|string',
        'isaccess'          => 'required|string',
        'isread'            => 'required|string',
        'iswrite'           => 'required|string',
        'isreply'           => 'required|string',
        'isupload'          => 'required|string',
        'isdownload'        => 'required|string',
        'head'              => 'if_exist|string',
        'tail'              => 'if_exist|string',
        'status'            => 'if_exist|string',
        'updated_at'        => 'if_exist|valid_date',
        'created_at'        => 'if_exist|valid_date',
    ];

    public function getEntityByField($field, $value): ?BoardConfigEntity
    {
        $entity = $this->asObject(BoardConfigEntity::class)->where($field, $value)->first();
        if (is_null($entity)) {
            throw new \Exception("해당 데이터가 없습니다.\n {$field}->{$value}");
        }
        return $entity;
    }
    public function getEntity($uid): ?BoardConfigEntity
    {
        return $this->getEntityByField($this->primaryKey, $uid);
    }
    public function getFieldFormOptions(array $wheres = array(), $temps = array()): array
    {
        foreach ($this->asObject(BoardConfigEntity::class)->where($wheres)->findAll() as $entity) {
            $temps[$entity->getPrimaryKey()] = $entity->getTitle();
        }
        return $temps;
    }

    protected function changeFormData($field, $value)
    {
        switch ($field) {
            case 'head':
            case 'tail':
                return htmlentities($value);
                break;
            default:
                return $value;
                break;
        }
    }
    public function create(array $formDatas): BoardConfigEntity
    {
        $entity = new BoardConfigEntity($formDatas);
        foreach ($formDatas as $field => $value) {
            $entity->$field = $this->changeFormData($field, $value);
        }
        return parent::create_process($entity);
    }
    public function modify(BoardConfigEntity $entity, array $formDatas): BoardConfigEntity
    {
        foreach ($formDatas as $field => $value) {
            $entity->$field = $this->changeFormData($field, $value);
        }
        return parent::modify_process($entity);
    }

    //Index관련
    public function setIndexWordFilter(string $word)
    {
        parent::setIndexWordFilter($word);
        $this->orLike('name', $word, 'both'); //befor , after , both
    }
    public function setIndexOrderBy($field, $order = 'ASC')
    {
        $this->orderBy("name", "ASC");
        parent::setIndexOrderBy($field, $order);
    }
}
