<?php

namespace App\Models;

use App\Entities\UserSNSEntity;
use App\Models\CommonModel;

class UserSNSModel extends CommonModel
{
    const TABLE = "user_sns";
    const PK = "uid";
    const TITLE = "name";
    const SITE = "site";
    protected $table = self::TABLE;
    protected $primaryKey = self::PK;
    protected $returnType = UserSNSEntity::class;
    protected $allowedFields = [
        "site",
        "id",
        "name",
        "email",
        "detail",
        "status"
    ];
    public function __construct()
    {
        parent::__construct();
    }
    public function getTitleField(): string
    {
        return self::TITLE;
    }
    public function getFieldRule(string $action, string $field): string
    {
        switch ($field) {
            case "id":
                $rule = "required|trim|min_length[4]|is_unique[{$this->table}.{$field}]";
                break;
            case $this->getTitleField():
                $rule = "required|trim|string";
                break;
            case "site":
                $rule = "required|trim|string";
                break;
            case "email":
                $rule = "required|trim|valid_email";
                break;
            case "detail":
                $rule = "required|trim|string";
                break;
            default:
                $rule = parent::getFieldRule($action, $field);
                break;
        }
        return $rule;
    }
    protected function convertEntityData(string $field, array $formDatas): mixed
    {
        switch ($field) {
            case "detail": //content등 textarea를 사용한 Field
                $value = htmlentities($formDatas[$field], ENT_QUOTES);
                break;
            default:
                $value = parent::convertEntityData($field, $formDatas);
                break;
        }
        return $value;
    }
    public function getEntityByPK(string $uid): null|UserSNSEntity
    {
        $this->where($this->getPKField(), intval($uid));
        return $this->getEntity();
    }
    public function getEntityByID(string $id): null|UserSNSEntity
    {
        $this->where('id', $id);
        return $this->getEntity();
    }

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

    //modify용
    public function modify(UserSNSEntity $entity, array $formDatas): UserSNSEntity
    {
        return $this->modify_process($entity, $formDatas);
    }
}
