<?php

namespace App\Services\Equipment;

use App\Entities\Equipment\ServerEntity;
use App\Models\Equipment\ServerModel;
use App\Models\Equipment\ServerPartModel;
use App\Services\Equipment\EquipmentService;

class ServerService extends EquipmentService
{
    private ?ServerPartService $_serverPartService = null;
    public function __construct()
    {
        parent::__construct(new ServerModel());
        $this->addClassName('Server');
    }
    public function getFormFields(): array
    {
        return [
            'fields' => [
                "code",
                "type",
                "title",
                "price",
                "amount",
                "manufactur_at",
                "format_at",
                "status",
                "partinfo_uid_CPU",
                "partinfo_uid_RAM",
                "partinfo_uid_DISK",
                "partinfo_uid_OS",
                "ipinfo_uid",
                "csinfo_uid",
            ],
            'filters' => [
                "clientinfo_uid",
                "serviceinfo_uid",
                "type",
                "title",
                "partinfo_uid_CPU",
                "partinfo_uid_RAM",
                "partinfo_uid_DISK",
                "partinfo_uid_OS",
                "ipinfo_uid",
                "csinfo_uid",
                "status"
            ],
        ];
    }
    public function getIndexFields(): array
    {
        return [
            'fields' => [
                'clientinfo_uid',
                'serviceinfo_uid',
                "type",
                'title',
                'price',
                'amount',
                "serverpartinfo",
                "ipinfo_uid",
                "csinfo_uid",
                'manufactur_at',
                "format_at",
                'status'
            ],
            'filters' => [
                'clientinfo_uid',
                'serviceinfo_uid',
                'type',
                "ipinfo_uid",
                "csinfo_uid",
                'status'
            ],
        ];
    }
    public function getBatchjobFields(): array
    {
        return ['clientinfo_uid', 'status'];
    }
    final public function getServerPartService(): ServerPartService
    {
        if (!$this->_serverPartService) {
            $this->_serverPartService = new ServerPartService();
        }
        return $this->_serverPartService;
    }
    final public function getIPService(): IPService
    {
        if (!$this->_ipService) {
            $this->_ipService = new IPService();
        }
        return $this->_ipService;
    }
    final public function getCSService(): CSService
    {
        if (!$this->_csService) {
            $this->_csService = new CSService();
        }
        return $this->_csService;
    }
    //partEntity 정보 추가
    protected function getEntity_process(mixed $entity): ServerEntity
    {
        //서버 부품정보 정의
        foreach (SERVER['PARTTYPES'] as $partType) {
            $serverPartEnty = $this->getServerPartService()->getEntity(['serverinfo_uid' => $entity->getPK(), 'type' => $partType]);
            if ($serverPartEnty) {
                $entity->addServerPartEntity($partType, $serverPartEnty);
            }
        }
        //서버 IP정보 정의
        foreach ($this->getIPService()->getEntities(['serverinfo_uid' => $entity->getPK()]) as $ipEntity) {
            $entity->addIPEntity($ipEntity);
        }
        //서버 CS정보 정의
        foreach ($this->getCSService()->getEntities(['serverinfo_uid' => $entity->getPK()]) as $csEntity) {
            $entity->addCSEntity($csEntity);
        }
        return $entity;
    }
    //기본 기능부분
    public function getFormFieldOption(string $field, array $options = []): array
    {
        switch ($field) {
            case 'partinfo_uid_CPU':
                $options = $this->getServerPartService()->getFormFieldOption('partinfo_uid_CPU');
                break;
            case 'partinfo_uid_RAM':
                $options = $this->getServerPartService()->getFormFieldOption('partinfo_uid_RAM');
                break;
            case 'partinfo_uid_DISK':
                $options = $this->getServerPartService()->getFormFieldOption('partinfo_uid_DISK');
                break;
            case 'partinfo_uid_OS':
                $options = $this->getServerPartService()->getFormFieldOption('partinfo_uid_OS');
                break;
            case 'partinfo_uid_DB':
                $options = $this->getServerPartService()->getFormFieldOption('partinfo_uid_DB');
                break;
            case 'partinfo_uid_SOFTWARE':
                $options = $this->getServerPartService()->getFormFieldOption('partinfo_uid_SOFTWARE');
                break;
            case 'ipinfo_uid': //수정때문에 전체가 필요
                $options = $this->getIPService()->getEntities();
                break;
            case 'csinfo_uid': //수정때문에 전체가 필요
                $options = $this->getCSService()->getEntities();
                break;
            default:
                $options = parent::getFormFieldOption($field, $options);
                break;
        }
        return $options;
    }
    //FieldForm관련용
    //create용 장비코드 마지막번호 가져오기
    final public function getLastestCode(string $format, int $default): string
    {
        return $this->getModel()->getLastestCode($format, $default);
    }
    final public function codeCheck(array $formDatas)
    {
        //코드 패턴체크
        $pattern = env("Server.Prefix.code.pattern", false);
        if (!$pattern) {
            throw new \Exception(__METHOD__ . "에서 code의 prefix[Server.Prefix.code.pattern]가 정의되지 않았습니다.");
        }
        if (!array_key_exists('code', $formDatas)) {
            throw new \Exception("Server코드가 정의되지 않았습니다");
        }
        if (!preg_match($pattern, $formDatas['code'])) {
            throw new \Exception("Server코드[{$formDatas['code']}의 형식이 맞지않습니다");
        }
    }
    //생성
    public function create(array $formDatas): ServerEntity
    {
        $entity = parent::create($formDatas);
        dd($entity);
        //ServerPart정보 생성
        foreach ($this->getServerPartService()->createByServer($entity, $formDatas) as $serverPartEntity) {
            $entity->addServerPartEntity($serverPartEntity->getType(), $serverPartEntity);
        };
        //IP정보 생성
        $entity->addIPEntity($this->getIPService()->createByServer($entity, $formDatas));
        return $entity;
    }
    public function modify(mixed $entity, array $formDatas): ServerEntity
    {
        $entity = parent::modify($entity, $formDatas);
        // //ServerPart정보 생성
        // foreach ($this->getServerPartService()->modifyByServer($entity, $formDatas) as $serverPartEntity) {
        //     $entity->addServerPartEntity($serverPartEntity->getType(), $serverPartEntity);
        // };
        // //IP정보 생성
        // $entity->addIPEntity($this->getIPService()->modifyByServer($entity, $formDatas));
        return $entity;
    }

    //List 검색용
    //OrderBy 처리
    public function setOrderBy(mixed $field = null, mixed $value = null): void
    {
        $this->getModel()->orderBy("code ASC,title ASC");
        parent::setOrderBy($field, $value);
    }
}
