<?php

namespace App\Controllers\Admin\Customer;

use App\Entities\Customer\ServicePaymentEntity;
use App\Helpers\Customer\ServiceItemHelper;

use App\Services\Customer\ServiceItemService;
use App\Services\Customer\ServicePaymentService;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

class ServiceItemController extends CustomerController
{
    private ?ServicePaymentService $_servicePaymentService = null;
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->content_title = lang("{$this->getService()->getClassName()}.title");
        $this->class_path .= $this->getService()->getClassName();
        $this->uri_path .= strtolower($this->getService()->getClassName('/')) . '/';
        // $this->view_path .= strtolower($this->getService()->getClassName()) . DIRECTORY_SEPARATOR;

    }
    protected function initAction(string $action, $fields = []): void
    {
        parent::initAction($action, $fields);
        //LINE,IP,SERVER등 추가 FilterOption 셋팅용
        $this->setFilterOptionsByItemType();
    }
    public function getService(): ServiceItemService
    {
        if (!$this->_service) {
            $this->_service = new ServiceItemService();
        }
        return $this->_service;
    }
    public function getHelper(): ServiceItemHelper
    {
        if (!$this->_helper) {
            $this->_helper = new ServiceItemHelper();
        }
        return $this->_helper;
    }
    public function getServicePaymentService(): ServicePaymentService
    {
        if (!$this->_servicePaymentService) {
            $this->_servicePaymentService = new ServicePaymentService();
        }
        return $this->_servicePaymentService;
    }
    protected function getResultSuccess(string $message = MESSAGES["SUCCESS"], ?string $actionTemplate = null): RedirectResponse|string
    {
        switch ($this->getAction()) {
            case 'index':
                $result = parent::getResultSuccess($message, $this->request->getVar('ActionTemplate') ?? $actionTemplate ?? 'popup');
                break;
            default:
                $result = parent::getResultSuccess($message, $actionTemplate);
                break;
        }
        return $result;
    }
    public function getFormFieldOption(string $field, array $options = []): array
    {
        switch ($field) {
            case 'item_uid':
                $item_type = $this->request->getVar('item_type');
                if (!$item_type) {
                    throw new \Exception(__FUNCTION__ . "에서 item_type이 지정되지 않았습니다.");
                }
                $options = $this->getFilterFieldOption('item_type');
                break;
            default:
                $options = parent::getFormFieldOption($field, $options);
                break;
        }
        return $options;
    }
    //Index,FieldForm관련

    protected function create_process(array $formDatas): void
    {
        $serviceEntity = $this->getServiceService()->getEntity($formDatas['serviceinfo_uid']);
        if (!$serviceEntity) {
            throw new \Exception("{$formDatas['serviceinfo_uid']}에 대한 서비스정보를 찾을수 없습니다.");
        }
        parent::create_process($formDatas);
        //결제 처리
        $paymentFormDatas = [];
        //금액(amount)가 0원일경우는 바로 결제완료 처리함.
        if ($this->entity->getAmount() === 0) {
            // 관리자 UID는 현재 인증된 사용자로 설정
            $paymentFormDatas['user_uid'] = $this->getMyAuth()->getUIDByAuthInfo();
            $paymentFormDatas['status']   = ServicePaymentEntity::STATUS_PAID;
        }
        // dd($paymentFormDatas);
        $this->getServicePaymentService()->createByServiceItemService($paymentFormDatas, $this->entity);
    }
}
