<?php

namespace App\Services\Customer;

use App\Entities\Customer\ServiceItemEntity;
use App\Models\Customer\ServiceItemModel;
use App\Services\Customer\CustomerService;
use App\Services\Customer\ServicePaymentService;
use App\Services\Customer\ServiceService;

class ServiceItemService extends CustomerService
{
    private ?ServiceService $_serviceService = null;
    private ?ServicePaymentService $_servicePaymentService = null;
    public function __construct(mixed $request = null)
    {
        parent::__construct($request);
        $this->addClassName('ServiceItem');
    }
    public function getModelClass(): ServiceItemModel
    {
        return new ServiceItemModel();
    }
    public function getEntityClass(): ServiceItemEntity
    {
        return new ServiceItemEntity();
    }
    public function getServiceService(): ServiceService
    {
        if (!$this->_serviceService) {
            $this->_serviceService = new ServiceService($this->request);
        }
        return $this->_serviceService;
    }
    public function getServicePaymentService(): ServicePaymentService
    {
        if (!$this->_servicePaymentService) {
            $this->_servicePaymentService = new ServicePaymentService($this->request);
        }
        return $this->_servicePaymentService;
    }
    public function getFormFields(): array
    {
        return [
            "serviceinfo_uid",
            "item_type",
            "item_uid",
            "billing_cycle",
            "price",
            "amount",
            "start_at",
            "status",
        ];
    }
    public function getFilterFields(): array
    {
        return ["serviceinfo_uid", 'item_type', 'item_uid', 'billing_cycle', 'status'];
    }
    public function getBatchJobFields(): array
    {
        return ['status'];
    }
    public function getIndexFields(): array
    {
        return ['serviceinfo_uid', 'item_type', 'item_uid', 'billing_cycle', 'price', 'amount', 'start_at', 'updated_at', 'status'];
    }
    //Entity의 관련객체정의용
    //FieldForm관련용
    public function getFormFieldOption(string $field, array $options = []): array
    {
        switch ($field) {
            case 'serviceinfo_uid':
                $options = $this->getServiceService()->getEntities();
                break;
            default:
                $options = parent::getFormFieldOption($field, $options);
                break;
        }
        return $options;
    }
    public function create(array $formDatas, mixed $entity = null): ServiceItemEntity
    {
        $entity = parent::create($formDatas, $entity);
        //결제정보 ServicePaymentService에 등록
        $this->getServicePaymentService()->createByServiceItem($entity);
        return $entity;
    }
}
