<?php

namespace App\Services;

use App\Entities\UserEntity;
use App\Models\UserModel;
use CodeIgniter\HTTP\IncomingRequest;

class UserService extends CommonService
{
    protected ?IncomingRequest $request = null;
    public function __construct(?IncomingRequest $request = null)
    {
        parent::__construct($request);
        $this->addClassName('User');
    }
    public function getModelClass(): UserModel
    {
        return new UserModel();
    }
    public function getEntityClass(): UserEntity
    {
        return new UserEntity();
    }

    public function getFormFields(): array
    {
        return ['id', 'passwd', 'confirmpassword', 'name', 'email', 'mobile', 'role'];
    }
    public function getFilterFields(): array
    {
        return ['role', 'status'];
    }
    public function getBatchJobFields(): array
    {
        return ['status'];
    }
    public function getIndexFields(): array
    {
        return ['id', 'name', 'email', 'mobile', 'role', 'status'];
    }
    public function create(array $formDatas, mixed $entity = null): UserEntity
    {
        $formDatas['role'] = implode(DEFAULTS["DELIMITER_ROLE"], $formDatas['role']);
        return parent::create($formDatas, $entity ?? new UserEntity());
    }
    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);
    }
}
