<?php

namespace App\Services\Equipment\ServerPart;

use App\Entities\Equipment\ServerPartEntity;
use App\Entities\PaymentEntity;
use App\Interfaces\Equipment\ServerPartInterface;
use App\Services\PaymentService as ParentService;

class PaymentService 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
    {
        if ($this->getServerPartEntity()->getServiceInfoUID() === null) {
            throw new \Exception(__METHOD__ . "에서 오류발생: 서비스정보가 정의된 후에만 가능합니다.");
        }
        //일회인이 아닌경우
        if ($this->getServerPartEntity()->getBilling() !== PAYMENT['BILLING']['ONETIME']) {
            throw new \Exception(__METHOD__ . "에서 오류발생: :" . lang("{$this->getClassName()}.BILLING." . PAYMENT['BILLING'][$this->getServerPartEntity()->getBilling()]) . "지급 상품은 처리가 불가, 일회성만 가능합니다.");
        }

        $formDatas = [];
        $formDatas['clientinfo_uid']     = $this->getServerPartEntity()->getClientInfoUID();
        $formDatas['serviceinfo_uid']    = $this->getServerPartEntity()->getServiceInfoUID();
        $formDatas['serverinfo_uid']     = $this->getServerPartEntity()->getServerInfoUID(); //서버연결정보 수정시에 필요함
        //타이틀은 기타의 경우 직접작성한 제목을 등록하고 아닌경우는 Part의 Title을 사용한다.
        $formDatas['title']              = $this->getServerPartEntity()->getType() === 'ETC' ? $this->getServerPartEntity()->getTitle() : $this->getServerPartEntity()->getPartEntity()->getTitle();
        $formDatas['amount']             = $this->getServerPartEntity()->getAmount();
        $formDatas['billing']            = $this->getServerPartEntity()->getBilling();
        return $formDatas;
    }
    public function createServerPart(): ServerPartEntity
    {
        //필수정보처리 후 FormData 가져오기
        $formDatas = $this->action_process();
        //당일결체일로 설정
        $formDatas['billing_at'] = date("Y-m-d");
        //결제정보등록
        $entity = $this->create($formDatas);
        //서버연결정보 Entity에 결제정보 설정
        return $this->getServerPartEntity()->setPaymentEntity($entity);
    }
    public function modifyServerPart(): ServerPartEntity
    {
        //미납상태의 결제정보 가져오기
        $entity = $this->getEntity(['serverpartinfo_uid' => $this->getServerPartEntity()->getPK(), 'status' => STATUS['UNPAID']]);
        if (!$entity instanceof PaymentEntity) {
            throw new \Exception(__METHOD__ . "에서 오류발생: {$this->getServerPartEntity()->getPK()}에 해당하는 결제정보를 찾을수 없습니다.");
        }
        //필수정보처리 후 FormData 가져오기
        $formDatas = $this->action_process();
        //결제정보수정
        $entity = $this->modify($entity, $formDatas);
        //서버연결정보 Entity에 결제정보 설정
        return $this->getServerPartEntity()->setPaymentEntity($entity);
    }

    public function deleteServerPart(): ServerPartEntity
    {
        //삭제시에는 아무것도 하지 않는다.
        return $this->getServerPartEntity();
    }
}
