<?php

namespace App\Models;

use App\Entities\BoardEntity;

class BoardModel extends CommonModel
{
    const TABLE = "boardinfo";
    const PK = "uid";
    const TITLE = "title";
    protected $table            = self::TABLE;
    protected $primaryKey       = self::PK;
    protected $returnType       = BoardEntity::class;
    protected $allowedFields = [
        "uid",
        "user_uid",
        "worker_uid",
        "category",
        "title",
        "content",
        "status",
        "updated_at"
    ];
    public function __construct()
    {
        parent::__construct();
    }
    public function getFormRule(string $action, string $field): string
    {
        if (is_array($field)) {
            throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true));
        }
        switch ($field) {
            case "category":
            case "title":
                $rule = "required|trim|string";
                break;
            case "user_uid":
                $rule = "required|numeric";
                break;
            case "worker_uid":
                $rule = "permit_empty|numeric";
                break;
            case "content":
                $rule = "permit_empty|string";
                break;
            default:
                $rule = parent::getFormRule($action, $field);
                break;
        }
        return $rule;
    }
    final public function create(array $formDatas): BoardEntity
    {
        // 관리자 UID는 현재 인증된 사용자로 설정
        $formDatas['user_uid'] = $this->getMyAuth()->getUIDByAuthInfo();
        return parent::create($formDatas);
    }
}
