<?php

namespace App\Models\Mangboard;

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

// +-----------------+---------------------+------+-----+---------------------+----------------+
// | Field           | Type                | Null | Key | Default             | Extra          |
// +-----------------+---------------------+------+-----+---------------------+----------------+
// | pid             | int(10) unsigned    | NO   | PRI | NULL                | auto_increment |
// | gid             | int(10) unsigned    | NO   | MUL | 0                   |                |
// | reply           | int(10) unsigned    | NO   |     | 0                   |                |
// | depth           | int(10) unsigned    | NO   |     | 0                   |                |
// | user_id         | varchar(150)        | NO   | MUL |                     |                |
// | user_name       | varchar(100)        | NO   | MUL |                     |                |
// | title           | varchar(255)        | NO   | MUL |                     |                |
// | passwd          | varchar(100)        | NO   |     |                     |                |
// | homepage        | varchar(255)        | NO   |     |                     |                |
// | email           | varchar(255)        | NO   |     |                     |                |
// | address         | varchar(255)        | NO   |     |                     |                |
// | phone           | varchar(50)         | NO   |     |                     |                |
// | reg_date        | datetime            | NO   | MUL | 0000-00-00 00:00:00 |                |
// | modify_date     | datetime            | NO   | MUL | 0000-00-00 00:00:00 |                |
// | calendar_date   | datetime            | NO   | MUL | 0000-00-00 00:00:00 |                |
// | hit             | int(10) unsigned    | NO   | MUL | 0                   |                |
// | user_pid        | int(10) unsigned    | NO   | MUL | 0                   |                |
// | parent_pid      | int(10) unsigned    | NO   | MUL | 0                   |                |
// | parent_user_pid | int(10) unsigned    | NO   | MUL | 0                   |                |
// | level           | tinyint(3) unsigned | NO   |     | 0                   |                |
// | file_count      | int(10) unsigned    | NO   |     | 0                   |                |
// | comment_count   | int(10) unsigned    | NO   |     | 0                   |                |
// | vote_good_count | int(10) unsigned    | NO   |     | 0                   |                |
// | vote_bad_count  | int(10) unsigned    | NO   |     | 0                   |                |
// | vote_type       | int(10) unsigned    | NO   |     | 0                   |                |
// | ip              | varchar(40)         | NO   |     |                     |                |
// | agent           | varchar(30)         | NO   |     |                     |                |
// | is_notice       | tinyint(3) unsigned | NO   | MUL | 0                   |                |
// | is_secret       | tinyint(3) unsigned | NO   |     | 0                   |                |
// | status          | varchar(30)         | NO   | MUL | publish             |                |
// | is_show         | tinyint(3) unsigned | NO   | MUL | 1                   |                |
// | reply_email     | tinyint(3)          | NO   |     | 0                   |                |
// | text            | mediumtext          | NO   |     | NULL                |                |
// | content         | mediumtext          | NO   |     | NULL                |                |
// | content_type    | varchar(20)         | NO   |     |                     |                |
// | data_type       | varchar(20)         | NO   |     | text                |                |
// | editor_type     | varchar(10)         | NO   |     | N                   |                |
// | tag             | varchar(255)        | NO   |     |                     |                |
// | category1       | varchar(100)        | NO   | MUL |                     |                |
// | category2       | varchar(100)        | NO   |     |                     |                |
// | category3       | varchar(100)        | NO   |     |                     |                |
// | image_path      | varchar(255)        | NO   |     |                     |                |
// | site_link1      | varchar(255)        | NO   |     |                     |                |
// | site_link2      | varchar(255)        | NO   |     |                     |                |
// | gps_latitude    | decimal(10,8)       | NO   | MUL | 0.00000000          |                |
// | gps_longitude   | decimal(11,8)       | NO   |     | 0.00000000          |                |
// | ext1            | varchar(255)        | NO   |     |                     |                |
// | ext2            | varchar(255)        | NO   |     |                     |                |
// | ext3            | varchar(255)        | NO   |     |                     |                |
// | ext4            | varchar(255)        | NO   |     |                     |                |
// | ext5            | varchar(255)        | NO   |     |                     |                |
// | ext6            | varchar(255)        | NO   |     |                     |                |
// | ext7            | varchar(255)        | NO   |     |                     |                |
// | ext8            | varchar(255)        | NO   |     |                     |                |
// | ext9            | varchar(255)        | NO   |     |                     |                |
// | ext10           | varchar(255)        | NO   |     |                     |                |
// +-----------------+---------------------+------+-----+---------------------+----------------+

class BoardModel extends CommonModel
{
    protected $primaryKey       = 'pid';
    protected $returnType       = BoardEntity::class;
    protected $allowedFields = [
        "gid",
        "title",
        "user_pid",
        "user_id",
        "user_name",
        "level",
        "data_type",
        "editor_type",
        "image_path",
        "reg_date",
        "hit",
        "content"
    ];
    public function __construct(string $table)
    {
        $this->table = $table;
        parent::__construct();
    }
    public function getTitleField(): string
    {
        return 'title';
    }
    public function getFieldRule(string $field, array $rules): array
    {
        switch ($field) {
            case 'gid':
                $rules[$field] =  "if_exist|numeric";
                break;
            case "data_type":
                $rules[$field] =  "if_exist|trim|in_list[html,text]";
                break;
            case "editor_type":
                $rules[$field] =  "if_exist|trim|in_list[N,S]";
                break;
            case "reg_date":
                $rules[$field] = "if_exist|valid_date";
                break;
            case "content":
            case "image_path":
                $rules[$field] =  "if_exist|trim|string";
                break;
            case 'hit':
            case 'level':
            case 'user_pid':
                $rules[$field] =  "if_exist|numeric";
                break;
            default:
                $rules = parent::getFieldRule($field, $rules);
                break;
        }
        return $rules;
    }

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

    //create용
    public function create(array $formDatas = []): BoardEntity
    {
        $entity = $this->create_process(new BoardEntity(), $formDatas);
        //입력후 PID값을 GID값에 넣어주기 위함
        return $this->modify($entity, ['gid' => intval($entity->getPK())]);
    }
    //modify용
    public function modify(BoardEntity $entity, array $formDatas): BoardEntity
    {
        return $this->modify_process($entity, $formDatas);
    }
    public function createByCrawler(
        BoardsEntity $boards_entity,
        UserEntity $user_entity,
        int $cnt,
        array $listInfo,
        array $storages
    ): BoardEntity {
        $formDatas = [];
        //미디어관련정보 entity에 넣기
        $formDatas['title'] = $listInfo["title"];
        $formDatas['user_pid'] = $user_entity->getPK();
        $formDatas['user_id'] = $user_entity->getID();
        $formDatas['user_name'] = $listInfo["nickname"] != "" ? $listInfo["nickname"] : $user_entity->getTitle();
        $formDatas['level'] = $boards_entity->getListLevel();
        $formDatas['hit'] = intval($listInfo['hit']);
        $formDatas['reg_date'] = date("Y-m-d H:i:s", strtotime($listInfo['date']));
        $formDatas['data_type'] = "html";
        $formDatas['editor_type'] = "S";
        $formDatas['image_path'] = "";
        $formDatas['content'] = "";
        foreach ($storages as $storage) {
            if ($formDatas['image_path'] == "") {
                $formDatas['image_path'] = $storage->getBasePath() . DIRECTORY_SEPARATOR . $storage->getPath() . DIRECTORY_SEPARATOR . $storage->getOriginName();
            }
            $formDatas['content'] .= $storage->getHTMLTag();;
        }
        //망보드 게시판에 등록
        if ($formDatas['content'] == "") {
            throw new \Exception(sprintf(
                "%s=>%s번째 %s 내용이 없어 => %s 등록 안함 : storage->%s",
                __FUNCTION__,
                $cnt,
                $listInfo["title"],
                $this->getTable(),
                count($storages)
            ));
        }
        $entity = $this->create($formDatas);
        log_message("notice", sprintf(
            "%s=>%s번째 %s => %s 등록 완료 : storage->%s",
            __FUNCTION__,
            $cnt,
            $listInfo["title"],
            $this->getTable(),
            count($storages)
        ));
        return $entity;
    }
}
