<?php

namespace App\Models\Mangboard;

use App\Models\CommonModel;
use App\Entities\Mangboard\BoardsEntity;

// +-----------------------+----------------------+------+-----+---------------------+----------------+
// | Field                 | Type                 | Null | Key | Default             | Extra          |
// +-----------------------+----------------------+------+-----+---------------------+----------------+
// | pid                   | int(10) unsigned     | NO   | PRI | NULL                | auto_increment |
// | board_name            | varchar(50)          | NO   | MUL |                     |                |
// | description           | varchar(255)         | NO   |     |                     |                |
// | image_path            | varchar(255)         | NO   |     |                     |                |
// | skin_name             | varchar(100)         | NO   | MUL |                     |                |
// | model_name            | varchar(100)         | NO   | MUL |                     |                |
// | table_link            | varchar(100)         | NO   | MUL |                     |                |
// | mobile_skin_name      | varchar(100)         | NO   | MUL |                     |                |
// | post_id               | bigint(20)           | NO   |     | 0                   |                |
// | board_header          | text                 | NO   |     | NULL                |                |
// | board_footer          | text                 | NO   |     | NULL                |                |
// | board_content_form    | text                 | NO   |     | NULL                |                |
// | editor_type           | varchar(50)          | NO   |     | N                   |                |
// | api_type              | varchar(50)          | NO   |     | mb                  |                |
// | page_size             | smallint(5) unsigned | NO   |     | 20                  |                |
// | comment_size          | smallint(5) unsigned | NO   |     | 50                  |                |
// | block_size            | tinyint(3) unsigned  | NO   |     | 10                  |                |
// | category_type         | varchar(50)          | NO   |     | NONE                |                |
// | category_data         | text                 | NO   |     | NULL                |                |
// | use_board_vote_good   | tinyint(3) unsigned  | NO   |     | 0                   |                |
// | use_board_vote_bad    | tinyint(3) unsigned  | NO   |     | 0                   |                |
// | use_comment           | tinyint(3) unsigned  | NO   |     | 0                   |                |
// | use_comment_vote_good | tinyint(3) unsigned  | NO   |     | 0                   |                |
// | use_comment_vote_bad  | tinyint(3) unsigned  | NO   |     | 0                   |                |
// | use_secret            | tinyint(3) unsigned  | NO   |     | 0                   |                |
// | use_notice            | tinyint(3) unsigned  | NO   |     | 0                   |                |
// | use_list_title        | tinyint(3) unsigned  | NO   |     | 1                   |                |
// | use_list_search       | tinyint(3) unsigned  | NO   |     | 1                   |                |
// | list_level            | tinyint(3) unsigned  | NO   |     | 0                   |                |
// | view_level            | tinyint(3) unsigned  | NO   |     | 0                   |                |
// | write_level           | tinyint(3) unsigned  | NO   |     | 0                   |                |
// | reply_level           | tinyint(3) unsigned  | NO   |     | 0                   |                |
// | delete_level          | tinyint(3) unsigned  | NO   |     | 8                   |                |
// | modify_level          | tinyint(3) unsigned  | NO   |     | 8                   |                |
// | secret_level          | tinyint(3) unsigned  | NO   |     | 8                   |                |
// | manage_level          | tinyint(3) unsigned  | NO   |     | 8                   |                |
// | comment_level         | tinyint(3) unsigned  | NO   |     | 0                   |                |
// | point_board_read      | int(10)              | NO   |     | 0                   |                |
// | point_board_write     | int(10)              | NO   |     | 0                   |                |
// | point_board_reply     | int(10)              | NO   |     | 0                   |                |
// | point_comment_write   | int(10)              | NO   |     | 0                   |                |
// | board_type            | varchar(50)          | NO   | MUL | board               |                |
// | reg_date              | datetime             | NO   | MUL | 0000-00-00 00:00:00 |                |
// | is_show               | tinyint(3) unsigned  | NO   | MUL | 1                   |                |
// | ext1                  | varchar(255)         | NO   |     |                     |                |
// | ext2                  | varchar(255)         | NO   |     |                     |                |
// | ext3                  | varchar(255)         | NO   |     |                     |                |
// | ext4                  | varchar(255)         | NO   |     |                     |                |
// | ext5                  | varchar(255)         | NO   |     |                     |                |
// +-----------------------+----------------------+------+-----+---------------------+----------------+

class BoardsModel extends CommonModel
{
    protected $table            = 'mb_boards';
    protected $primaryKey       = 'pid';
    protected $returnType       = BoardsEntity::class;
    protected $allowedFields = [
        "board_name",
        "board_type",
        "description",
        "list_level",
        "write_level",
        "modify_level",
        "view_level",
        "reply_level",
        "delete_level",
        "secret_level",
        "manage_level",
        "comment_level",
        "is_show",
        "reg_date",
    ];
    public function __construct()
    {
        parent::__construct();
    }
    public function getTitleField(): string
    {
        return 'title';
    }
    public function getFieldRule(string $field, array $rules): array
    {
        switch ($field) {
            case "board_type":
                $rules[$field] =  "if_exist|trim|in_list[board,admin,user]";
                break;
            case "description":
                $rules[$field] =  "if_exist|trim|string";
                break;
            case "list_level":
            case "write_level":
            case "modify_level":
            case "view_level":
            case "reply_level":
            case "delete_level":
            case "secret_level":
            case "manage_level":
            case "comment_level":
            case 'is_show':
                $rules[$field] =  "if_exist|numeric";
                break;
            case "reg_date":
                $rules[$field] = "if_exist|valid_date";
                break;
            default:
                $rules = parent::getFieldRule($field, $rules);
                break;
        }
        return $rules;
    }

    public function getEntityByPK(int $uid): null | BoardsEntity
    {
        $this->where($this->getPKField(), $uid);
        return $this->getEntity();
    }
    public function getEntityByID(string $board_name): null | BoardsEntity
    {
        $this->where('board_name', $board_name);
        return $this->getEntity();
    }

    //create용
    public function create(array $formDatas = []): BoardsEntity
    {
        return $this->create_process(new BoardsEntity(), $formDatas);
    }
    //modify용
    public function modify(BoardsEntity $entity, array $formDatas): BoardsEntity
    {
        return $this->modify_process($entity, $formDatas);
    }
}
