<?php

namespace App\Services;

use App\Entities\UserEntity;
use App\Models\UserModel;

class UserService extends CommonService
{
    public function __construct()
    {
        parent::__construct(new UserModel());
        $this->addClassName('User');
    }
    public function getFormFields(): array
    {
        return [
            'fields' => [
                'id',
                'passwd',
                'confirmpassword',
                'name',
                'email',
                'mobile',
                'role'
            ],
            'filters' => [
                'role',
                'status'
            ],
        ];
    }
    public function getViewFields(): array
    {
        return [
            'fields' => [
                'id',
                'name',
                'email',
                'mobile',
                'role',
                'status'
            ],
            'filters' => [
                'role',
                'status'
            ],
        ];
    }
    public function getIndexFields(): array
    {
        return [
            'fields' => [
                'id',
                'name',
                'email',
                'mobile',
                'role',
                'status'
            ],
            'filters' => [
                'role',
                'status'
            ],
        ];
    }
    public function getBatchjobFields(): array
    {
        return ['status'];
    }
    //기본 기능부분

    public function create(array $formDatas): UserEntity
    {
        $formDatas['role'] = implode(DEFAULTS["DELIMITER_ROLE"], $formDatas['role']);
        return parent::create($formDatas);
    }
    public function modify(mixed $entity, array $formDatas): UserEntity
    {
        // die(var_export($formDatas, true));
        //암호를 입력하지 않았을시는 변경하기 않게 하기위함
        if (isset($formDatas['passwd']) && $formDatas['passwd'] == "") {
            unset($formDatas['passwd']);
            unset($formDatas['confirmpassword']);
        }
        //Role을 지정이 있을경우에만 , toggle이나 batcjhjob에서는 없을수도 있으므로
        if (isset($formDatas['role'])) {
            $formDatas['role'] = implode(DEFAULTS["DELIMITER_ROLE"], $formDatas['role']);
        }
        // die(var_export($formDatas, true));
        return parent::modify($entity, $formDatas);
    }
    //List 검색용
    //FormFilter 조건절 처리
    public function index_condition_filterField(string $field, mixed $filter_value): void
    {
        switch ($field) {
            case 'role':
                $where = "FIND_IN_SET(" . $this->getModel()->escape($filter_value) . ", {$this->getModel()->getTable()}.{$field}) > 0";
                //FIND_IN_SET()은 MySQL 함수이므로 CodeIgniter가 이를 일반 컬럼명으로 착각하고 escape하게 되면 오류가 발생합니다. 따라서 ->where($sql, null, false)로 명시하여 escape를 꺼줘야 정상 작동
                $this->getModel()->where($where, null, false);
                break;
            default:
                parent::index_condition_filterField($field, $filter_value);
                break;
        }
    }
    //검색어조건절처리
    public function index_condition_filterWord(string $word): void
    {
        $this->getModel()->orLike($this->getModel()->getTable() . '.id', $word, 'both');
        $this->getModel()->orLike($this->getModel()->getTable() . "." . $this->getModel()::TITLE, $word, 'both');
        $this->getModel()->orLike($this->getModel()->getTable() . '.email', $word, 'both');
        parent::index_condition_filterWord($word);
    }
}
