<?php

namespace App\Services\Customer;

use App\Entities\Customer\ServiceItemEntity;
use App\Entities\Customer\ServicePaymentEntity;
use App\Models\Customer\ServicePaymentModel;
use App\Services\Customer\ServiceService;

class ServicePaymentService extends CustomerService
{
    private ?ServiceService $_serviceService = null;
    public function __construct(mixed $request = null)
    {
        parent::__construct($request);
        $this->addClassName('ServicePayment');
    }
    public function getModelClass(): ServicePaymentModel
    {
        return new ServicePaymentModel();
    }
    public function getEntityClass(): ServicePaymentEntity
    {
        return new ServicePaymentEntity();
    }
    public function getFormFields(): array
    {
        return [
            "serviceinfo_uid",
            "ownerinfo_uid",
            "item_type",
            "item_uid",
            "billing_cycle",
            "amount",
            "billing_at",
            "issue_at",
            "status",
        ];
    }
    public function getFilterFields(): array
    {
        return ["serviceinfo_uid", "ownerinfo_uid", 'item_type', 'item_uid', 'billing_cycle', 'status'];
    }
    public function getBatchJobFields(): array
    {
        return ['status'];
    }
    public function getIndexFields(): array
    {
        return ['serviceinfo_uid', "ownerinfo_uid", 'item_type', 'item_uid', 'billing_cycle',  'amount', 'billing_at', 'issue_at', 'countdown', 'status'];
    }

    public function getServiceService(): ServiceService
    {
        if (!$this->_serviceService) {
            $this->_serviceService = new ServiceService($this->request);
        }
        return $this->_serviceService;
    }
    //Entity의 관련객체정의용
    //기본 기능부분
    //FieldForm관련용
    public function getFormFieldOption(string $field, array $options = []): array
    {
        switch ($field) {
            case 'serviceinfo_uid':
                $options = $this->getServiceService()->getEntities();
                break;
            case 'item_uid':
                $options = [];
                break;
            default:
                $options = parent::getFormFieldOption($field, $options);
                break;
        }
        return $options;
    }
    //ServiceItemService에서 사용
    public function createPaymentByServiceItem(ServiceItemEntity $serviceItemEntity): ServicePaymentEntity
    {
        $serviceEntity = $this->getServiceService()->getEntity($serviceItemEntity->getServiceUid());
        if (!$serviceEntity) {
            throw new \Exception("{$serviceItemEntity->getServiceUid()}에 대한 서비스정보를 찾을수 없습니다.");
        }
        $formDatas = [
            'serviceinfo_uid' => $serviceItemEntity->getServiceUid(),
            'ownerinfo_uid' => $serviceEntity->getOwnerUid(),
            'item_type' => $serviceItemEntity->getItemType(),
            'item_uid' => $serviceItemEntity->getItemUid(),
            'billing_cycle' => $serviceItemEntity->getBillingCycle(),
            'amount' => $serviceItemEntity->getAmount(),
            'billing_at' => $serviceEntity->getBillingAt(),
            'issue_at' => date('Y-m-d'),
        ];
        return $this->create($formDatas);
    }
}
