<?php

namespace App\Services\Customer;

use App\Entities\Customer\AccountEntity;
use App\Entities\Customer\ClientEntity;
use App\Entities\PaymentEntity;
use App\Helpers\Customer\AccountHelper;
use App\Interfaces\PaymentInterface;
use App\Models\Customer\AccountModel;

class AccountService extends CustomerService implements PaymentInterface
{
    private ?ClientService $_clientService = null;
    public function __construct()
    {
        parent::__construct(new AccountModel(), new AccountHelper());
        $this->addClassName('Account');
    }
    public function getFormFields(): array
    {
        return [
            "clientinfo_uid",
            "bank",
            "title",
            "alias",
            "issue_at",
            "amount",
            "status",
            "content"
        ];
    }
    public function getFormFilters(): array
    {
        return [
            "clientinfo_uid",
            "bank",
            "status",
        ];
    }
    public function getBatchjobFields(): array
    {
        return ['status'];
    }
    final public function getClientService(): ClientService
    {
        if (!$this->_clientService) {
            $this->_clientService = new ClientService();
        }
        return $this->_clientService;
    }
    //고객예치금처리
    private function setBalance(array $formDatas): ClientEntity
    {
        if (!array_key_exists('clientinfo_uid', $formDatas)) {
            throw new \Exception("고객정보가 필요합니다.");
        }
        $entity = $this->getClientService()->getEntity($formDatas['clientinfo_uid']);
        if (!$entity instanceof ClientEntity) {
            throw new \Exception("{$formDatas['clientinfo_uid']}에 대한 고객정보를 찾을수 없습니다.");
        }
        return $this->getClientService()->setAccount($formDatas['status'], $entity, intval($formDatas['amount']));
    }
    public function setPayment(string $action, PaymentEntity $paymentEntity, array $paymentDatas): PaymentEntity
    {
        switch ($action) {
            case 'create':
                $formDatas = [
                    'clientinfo_uid' => $paymentEntity->getClientInfoUID(),
                    'bank'           => null,
                    'title'          => "[결제차감] {$paymentEntity->getTitle()}",
                    'issue_at'       => date('Y-m-d H:i:s'),
                    'alias'          => array_key_exists('alias', $paymentDatas) ? $paymentDatas['alias'] : null,
                    'amount'         => $paymentEntity->getAmount(),
                    'status'         => STATUS['WITHDRAWAL'],
                ];
                $this->setBalance($formDatas);
                $this->create($formDatas);
                break;
            // case 'modify':
            // case 'delete':
            default:
                throw new \Exception(__METHOD__ . "에서 오류발생: {$action}는 지원하지 않는 기능입니다.");
        }
        return $paymentEntity;
    }
    //기본 기능부분
    //생성
    public function create(array $formDatas): AccountEntity
    {
        $this->setBalance($formDatas);
        return parent::create($formDatas);
    }
    //List 검색용
    public function index_condition_filterWord(string $word): void
    {
        $this->getModel()->orLike($this->getModel()->getTable() . '.alias', $word, 'both');
        parent::index_condition_filterWord($word);
    }
}
