<?php

namespace App\Services\Customer\Service;

use App\Entities\Customer\ServiceEntity;
use App\Entities\Equipment\ServerEntity;
use App\Services\Equipment\ServerService as ParentService;
use App\Interfaces\Customer\ServiceInterface;

class ServerService extends ParentService implements ServiceInterface
{
  private ?ServiceEntity $_serviceEntity = null;
  public function __construct()
  {
    parent::__construct();
  }

  final public function setServiceEntity(ServiceEntity $entity): self
  {
    $this->_serviceEntity = $entity;
    return $this;
  }
  public function getServiceEntity(): ServiceEntity
  {
    if (!$this->_serviceEntity instanceof ServiceEntity) {
      throw new \Exception(__METHOD__ . "에서 오류발생: 서브스정보가 정의되지 않았습니다.");
    }
    return $this->_serviceEntity;
  }

  private function action_process(array $formDatas = []): ServerEntity
  {
    if (!array_key_exists('status', $formDatas)) {
      throw new \Exception(__METHOD__ . "에서 오류발생: 서버상태가 설정되지 않았습니다.");
    }
    //서버정보 가져오기
    $entity = $this->getEntity(['serviceinfo_uid' => $this->getServiceEntity()->getPK()]);
    if (!$entity instanceof ServerEntity) {
      throw new \Exception("서비스정보[{$this->getServiceEntity()->getPK()}]에 해당하는 서버정보를 찾을수없습니다.");
    }
    //서버정보 수정
    return $this->modify($entity, $formDatas);
  }
  public function createService(): ServiceEntity
  {
    $formDatas = [];
    $formDatas['clientinfo_uid']  = $this->getServiceEntity()->getClientInfoUID();
    $formDatas['serviceinfo_uid'] = $this->getServiceEntity()->getPK();
    $formDatas['status'] = STATUS['OCCUPIED'];
    $entity = $this->action_process($formDatas);
    //서비스정보 Entity에 서버정보 설정
    return $this->getServiceEntity()->setServerEntity($entity);
  }
  public function modifyService(): ServiceEntity
  {
    return $this->createService();
  }
  public function deleteService(): ServiceEntity
  {
    $formDatas = [];
    $formDatas['clientinfo_uid']  = null;
    $formDatas['serviceinfo_uid'] = null;
    $formDatas['status'] = STATUS['AVAILABLE'];
    $entity = $this->action_process($formDatas);
    //서비스정보 Entity에 서버정보 설정
    return $this->getServiceEntity()->setServerEntity($entity);
  }
}
