<?php

namespace App\Models;

use App\Entities\UserSNSEntity;

class UserSNSModel extends BaseModel
{
    protected $table = "tw_user_sns";
    protected $returnType = UserSNSEntity::class;
    public function __construct()
    {
        parent::__construct('UserSNS');
        $this->allowedFields = [
            ...$this->allowedFields,
            "site", "id", 'name', "email", "detail", "status"
        ];
        $this->validationRules = [...$this->validationRules, ...$this->getFieldRules($this->allowedFields),];
    }
    public function getTitleField(): string
    {
        return 'name';
    }

    protected function getFieldRule(string $field, array $rules, string $action = ""): array
    {
        switch ($field) {
            case "id":
                $rules[$field] =   "required|trim|string";
                $rules[$field] .= $action == "insert" ? "|is_unique[{$this->table}.{$field}]" : "";
                break;
            case $this->getTitleField():
                $rules[$field] =   "required|trim|string";
                break;
            case "email":
                $rules[$field] =   "required|valid_email";
                break;
            case "detail":
                $rules[$field] =   "if_exist|string";
                break;
            case "status":
                $rules[$field] =   "required|string";
                break;
            default:
                $rules = parent::getFieldRule($field, $rules, $action);
                break;
        }
        return $rules;
    }
    public function getEntity($conditions): UserSNSEntity
    {
        return parent::getEntity($conditions);
    }

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

    //Index관련
    public function setIndexWordFilter(string $word)
    {
        parent::setIndexWordFilter($word);
        $this->orLike($this->getTitleField(), $word, "both");
        $this->orLike("email", $word, "both"); //befor , after , both
    }
}
