<?php

namespace App\Services\Part;

use App\Entities\Equipment\ServerEntity;
use App\Entities\Part\PartEntity;
use App\Models\CommonModel;

abstract class PartType3Service extends PartType2Service
{
    protected function __construct(CommonModel $model)
    {
        parent::__construct($model);
    }
    abstract public function getPartEntityByServer(ServerEntity $serverEntity): PartEntity;
    //서버관련 작업
    public function attachToServer(ServerEntity $serverEntity, array $formDatas = []): PartEntity
    {
        $formDatas = [];
        $formDatas['clientinfo_uid'] = $serverEntity->getClientInfoUid();
        $formDatas['serviceinfo_uid'] = $serverEntity->getServiceInfoUid();
        $formDatas['serverinfo_uid'] = $serverEntity->getPK();
        $formDatas['status'] = STATUS['OCCUPIED'];
        //파트정보가져오기
        $entity = $this->getPartEntityByServer($serverEntity);
        if ($entity->getStatus() !== STATUS['AVAILABLE']) {
            throw new \Exception(static::class . '->' . __FUNCTION__ . ":에서 오류발생: {$entity->getTitle()}는 사용중입니다.");
        }
        //파트정보 수정
        $entity = parent::modify_process($entity, $formDatas);
        // dd($entity);
        return $entity;
    }

    public function detachFromServer(ServerEntity $serverEntity, array $formDatas = []): PartEntity
    {
        $formDatas['clientinfo_uid'] = null;
        $formDatas['serviceinfo_uid'] = null;
        $formDatas['serverinfo_uid'] = null;
        $formDatas['status'] = STATUS['AVAILABLE'];
        //파트정보가져오기
        $entity = $this->getPartEntityByServer($serverEntity);
        $entity = parent::modify_process($entity, $formDatas);
        // dd($entity);
        return $entity;
    }
}
