<?php

namespace App\Models;

use App\Entities\SNSUSerEntity;
use App\Models\CommonModel;

class SNSUserModel extends CommonModel
{
    const TABLE = "sns_users";
    const PK = "uid";
    const TITLE = "name";
    const SITE = "site";
    protected $table            = self::TABLE;
    protected $primaryKey       = self::PK;
    protected $returnType       = SNSUSerEntity::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]|max_length[20]|is_unique[{$this->table}.{$field}]";
                break;
            case $this->getTitleField():
                $rule = "required|trim|string";
                break;
            case "site":
                $rule = "required|trim|string";
                break;
            case "email":
                $rule = "if_exist|trim|valid_email";
                break;
            default:
                $rule = parent::getFieldRule($action, $field);
                break;
        }
        return $rule;
    }
    public function getEntityByPK(string $uid): null|SNSUSerEntity
    {
        $this->where($this->getPKField(), intval($uid));
        return $this->getEntity();
    }
    public function getEntityByID(string $id): null|SNSUSerEntity
    {
        $this->where('id', $id);
        return $this->getEntity();
    }

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

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