<?php

namespace App\Services\Customer;

use App\Entities\Customer\ClientEntity;
use App\Services\CommonService;

use App\Services\Customer\ClientService;
use App\Services\Equipment\ServerService;
use App\Services\UserService;
use CodeIgniter\Model;

abstract class CustomerService extends CommonService
{
    private ?UserService $_userService = null;
    private ?ClientService $_clientService = null;
    private ?ServerService $_serverService = null;
    private $_equipmentService = [];
    protected function __construct(Model $model)
    {
        parent::__construct($model);
        $this->addClassName('Customer');
    }
    final public function getClientService(): ClientService
    {
        if (!$this->_clientService) {
            $this->_clientService = new ClientService();
        }
        return $this->_clientService;
    }
    final public function getUSerService(): UserService
    {
        if (!$this->_userService) {
            $this->_userService = new UserService();
        }
        return $this->_userService;
    }
    final public function getServiceService(): ServiceService
    {
        if (!$this->_serviceService) {
            $this->_serviceService = new ServiceService();
        }
        return $this->_serviceService;
    }
    final public function getServerService(): ServerService
    {
        if (!$this->_serverService) {
            $this->_serverService = new ServerService();
        }
        return $this->_serverService;
    }
    //ServiceItemController,ServiceController에서 사용
    //기본기능
    //FieldForm관련용
    public function getFormFieldOption(string $field, array $options = []): array
    {
        switch ($field) {
            case 'user_uid':
                $options = $this->getUserService()->getEntities();
                break;
            case 'clientinfo_uid':
                $options = $this->getClientService()->getEntities();
                break;
            case 'serviceinfo_uid':
                $options = $this->getServiceService()->getEntities();
                break;
            case 'serverinfo_uid':
                $options = $this->getServerService()->getEntities();
                break;
            default:
                $options = parent::getFormFieldOption($field, $options);
                break;
        }
        return $options;
    }
    //ItemType에 따른 FilterOption 설정용
    final public function getClient(int $uid): ClientEntity
    {
        $entity = $this->getClientService()->getEntity($uid);
        if (!$entity) {
            throw new \Exception("{$uid}에 해당하는 고객정보가 존재하지 않습니다. uid: {$uid}");
        }
        return $entity;
    }
}
