<?php

namespace App\Models\Mangboard;

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

// +------------------+----------------------+------+-----+---------------------+----------------+
// | Field            | Type                 | Null | Key | Default             | Extra          |
// +------------------+----------------------+------+-----+---------------------+----------------+
// | pid              | int(10) unsigned     | NO   | PRI | NULL                | auto_increment |
// | user_pid         | int(10) unsigned     | NO   |     | 0                   |                |
// | user_name        | varchar(100)         | NO   | MUL |                     |                |
// | board_name       | varchar(50)          | NO   | MUL |                     |                |
// | table_name       | varchar(100)         | NO   | MUL |                     |                |
// | board_pid        | int(10) unsigned     | NO   | MUL | 0                   |                |
// | file_name        | varchar(255)         | NO   | MUL |                     |                |
// | file_path        | varchar(255)         | NO   | MUL |                     |                |
// | file_type        | varchar(255)         | NO   | MUL |                     |                |
// | file_caption     | varchar(255)         | NO   |     |                     |                |
// | file_alt         | varchar(255)         | NO   |     |                     |                |
// | file_description | text                 | NO   |     | NULL                |                |
// | file_size        | int(10) unsigned     | NO   | MUL | 0                   |                |
// | link_count       | int(10) unsigned     | NO   |     | 0                   |                |
// | download_count   | int(10) unsigned     | NO   |     | 0                   |                |
// | file_sequence    | smallint(5) unsigned | NO   | MUL | 1                   |                |
// | is_download      | tinyint(3) unsigned  | NO   |     | 0                   |                |
// | reg_date         | datetime             | NO   | MUL | 0000-00-00 00:00:00 |                |
// | ip               | varchar(40)          | NO   |     |                     |                |
// | agent            | varchar(30)          | NO   |     |                     |                |
// +------------------+----------------------+------+-----+---------------------+----------------+

class FileModel extends CommonModel
{
    protected $table            = 'mb_files';
    protected $primaryKey       = 'pid';
    protected $returnType       = FileEntity::class;
    protected $allowedFields = [
        "user_pid",
        "user_name",
        "board_pid",
        "board_name",
        "table_name",
        "file_name",
        "file_path",
        "file_type",
        "file_caption",
        "file_alt",
        "file_description",
        "file_size",
        "file_sequence",
        "reg_date"
    ];
    public function __construct()
    {
        parent::__construct();
    }
    public function getTitleField(): string
    {
        return 'file_name';
    }
    public function getFieldRule(string $field, array $rules): array
    {
        switch ($field) {
            case "board_pid":
            case "user_pid":
            case "file_sequence":
            case "file_size":
                $rules[$field] =  "if_exist|numeric";
                break;
            case "board_name":
            case "table_name":
            case "file_name":
            case "file_path":
            case "file_type":
                $rules[$field] =  "required|trim|string";
                break;
            case "file_description":
            case "file_caption":
            case "file_alt":
                $rules[$field] =  "if_exist|trim|string";
                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 | FileEntity
    {
        $this->where($this->getPKField(), $uid);
        return $this->getEntity();
    }
    public function getEntityByID(string $id): null | FileEntity
    {
        $this->where('user_id', $id);
        return $this->getEntity();
    }

    // protected function convertEntityData(string $field, array $formDatas): string|int
    // {
    //     switch ($field) {
    //         default:
    //             $value = parent::convertEntityData($field, $formDatas);
    //             break;
    //     }
    //     return $value;
    // }

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