<?php

namespace App\Services\Customer\Service;

use App\Entities\Customer\ServiceEntity;
use App\Entities\PaymentEntity;
use App\Services\PaymentService as ParentService;
use App\Interfaces\Customer\ServiceInterface;

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

  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 = []): array
  {
    $formDatas['clientinfo_uid'] = $this->getServiceEntity()->getClientInfoUID();
    $formDatas['serviceinfo_uid'] = $this->getServiceEntity()->getPK();
    $formDatas['serverinfo_uid'] = $this->getServiceEntity()->getServerInfoUID();
    $formDatas['title'] = $this->getServiceEntity()->getServerEntity()->getTitle();
    $formDatas['amount'] = $this->getServiceEntity()->getAmount();
    $formDatas['billing'] = PAYMENT['BILLING']['MONTH'];
    $formDatas['billing_at'] = $this->getServiceEntity()->getBillingAt();
    return $formDatas;
  }
  public function createService(): ServiceEntity
  {
    //필수정보처리 후 결제정보등록
    $entity = $this->create($this->action_process());
    //서비스정보 Entity에 결제정보 설정
    return $this->getServiceEntity()->setPaymentEntity($entity);
  }
  public function modifyService(): ServiceEntity
  {
    //미납상태의 결제정보 가져오기
    $entity = $this->getEntity(['serviceinfo_uid' => $this->getServiceEntity()->getPK(), 'status' => STATUS['UNPAID']]);
    if (!$entity instanceof PaymentEntity) {
      throw new \Exception(__METHOD__ . "에서 오류발생: {$this->getServiceEntity()->getPK()}에 해당하는 결제정보를 찾을수 없습니다.");
    }
    //필수정보처리 후 결제정보수정
    $entity = $this->modify($entity, $this->action_process());
    //서비스정보 Entity에 결제정보 설정
    return $this->getServiceEntity()->setPaymentEntity($entity);
  }
  public function deleteService(): ServiceEntity
  {
    //삭제시에는 아무것도 하지 않는다.
    return $this->getServiceEntity();
  }
}
