<?php

namespace App\Models;

use App\Entities\UserSNSEntity;

class UserSNSModel extends CommonModel
{
    const TABLE = "user_sns";
    const PK = "uid";
    const TITLE = "name";
    const PARENT = "user_uid";
    const SITE = "site";
    protected $table = self::TABLE;
    protected $primaryKey = self::PK;
    protected $returnType = UserSNSEntity::class;
    protected $allowedFields = [
        "site",
        "user_uid",
        "id",
        "name",
        "email",
        "detail",
        "status",
        "updated_at"
    ];
    public function __construct()
    {
        parent::__construct();
    }
    public function getFilterFields(): array
    {
        return ['status'];
    }
    public function getBatchJobFields(): array
    {
        return ['status'];
    }
    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 $entity): mixed
    {
        switch ($field) {
            case "detail": //content등 textarea를 사용한 Field
                $entity->$field = htmlentities($formDatas[$field], ENT_QUOTES);
                break;
            default:
                $entity = parent::convertEntityData($field, $formDatas, $entity);
                break;
        }
        return $entity;
    }
    //List 검색용
    public function setList_WordFilter(string $word): void
    {
        $this->orLike(self::TABLE . '.id', $word, 'both');
        $this->orLike(self::TABLE . "." . self::TITLE, $word, 'both');
        $this->orLike(self::TABLE . '.email', $word, 'both');
    }
}
