<?php

namespace App\Models;

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

class SNSUserModel extends CommonModel
{
    protected $table            = 'sns_users';
    protected $primaryKey       = 'uid';
    protected $returnType       = SNSUSerEntity::class;
    protected $allowedFields = [
        "id",
        "name",
        "email",
        "detail",
        "status"
    ];
    public function __construct()
    {
        parent::__construct();
    }
    public function getTitleField(): string
    {
        return 'name';
    }
    public function getFieldRule(string $field, array $rules): array
    {
        switch ($field) {
            case "id":
                $rules[$field] = "required|trim|min_length[4]|max_length[20]|is_unique[{$this->table}.{$field}]";
                break;
            case $this->getTitleField():
                $rules[$field] = "required|trim|string";
                break;
            case "email":
                $rules[$field] = "if_exist|trim|valid_email";
                break;
            default:
                $rules = parent::getFieldRule($field, $rules);
                break;
        }
        return $rules;
    }
    public function getEntityByPK(int $uid): null|SNSUSerEntity
    {
        $this->where($this->getPKField(), $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);
    }
}
