<?php

namespace App\Services\Equipment\ServerPart;

use App\Entities\Equipment\ServerPartEntity;
use App\Entities\Equipment\CSEntity;
use App\Interfaces\Equipment\ServerPartInterface;
use App\Services\Equipment\CSService as ParentService;

class CSService extends ParentService implements ServerPartInterface
{
    private ?ServerPartEntity $_serverPartEntity = null;
    public function __construct()
    {
        parent::__construct();
    }

    public function setServerPartEntity(ServerPartEntity $entity): self
    {
        $this->_serverPartEntity = $entity;
        return $this;
    }
    public function getServerPartEntity(): ServerPartEntity
    {
        if (!$this->_serverPartEntity instanceof ServerPartEntity) {
            throw new \Exception(__METHOD__ . "에서 오류발생: 서버파트정보가 정의되지 않았습니다.");
        }
        return $this->_serverPartEntity;
    }

    //서버연결정보용 등록
    private function action_process(array $formDatas = []): CSEntity
    {
        if (!array_key_exists('status', $formDatas)) {
            throw new \Exception(__METHOD__ . "에서 오류발생: CS상태가 설정되지 않았습니다.");
        }
        //CS정보가져오기
        $entity = $this->getEntity($this->getServerPartEntity()->getPartUID());
        if (!$entity instanceof CSEntity) {
            throw new \Exception("{$this->getServerPartEntity()->getPartUID()}에 해당하는 CS정보를 찾을수없습니다.");
        }
        //CS정보 수정
        return $this->modify($entity, $formDatas);
    }
    public function createServerPart(): ServerPartEntity
    {
        $formDatas = [];
        $formDatas['clientinfo_uid']  = $this->getServerPartEntity()->getClientInfoUID();
        $formDatas['serviceinfo_uid'] = $this->getServerPartEntity()->getServiceInfoUID();
        $formDatas['serverinfo_uid']  = $this->getServerPartEntity()->getServerInfoUID();
        $formDatas['status'] = STATUS['OCCUPIED'];
        $entity = $this->action_process($formDatas);
        return $this->getServerPartEntity()->setPartEntity($entity);
    }
    public function modifyServerPart(): ServerPartEntity
    {
        return $this->createServerPart();
    }
    public function deleteServerPart(): ServerPartEntity
    {
        $formDatas = [];
        $formDatas['clientinfo_uid']  = null;
        $formDatas['serviceinfo_uid'] = null;
        $formDatas['serverinfo_uid']  = null;
        $formDatas['status'] = STATUS['AVAILABLE'];
        $entity = $this->action_process($formDatas);
        return $this->getServerPartEntity()->setPartEntity($entity);
    }
}
