<?php

namespace App\Services\Customer\Wallet;

use App\Entities\CommonEntity;
use App\Entities\Customer\ClientEntity;
use App\Entities\Customer\Wallet\AccountEntity;
use App\Entities\PaymentEntity;
use App\Forms\Customer\Wallet\AccountForm;
use App\Helpers\Customer\Wallet\AccountHelper;
use App\Models\Customer\Wallet\AccountModel;
use RuntimeException;

class AccountService extends WalletService
{
    protected string $formClass = AccountForm::class;
    protected string $helperClass = AccountHelper::class;

    public function __construct(AccountModel $model)
    {
        parent::__construct($model);
        $this->addClassPaths('Account');
        $this->title = "예치금";
        $this->balanceField = 'account_balance';
    }
    public function getEntityClass(): string
    {
        return AccountEntity::class;
    }
    //기본 기능부분
    protected function getEntity_process(mixed $entity): AccountEntity
    {
        return $entity;
    }

    //List 검색용
    //FormFilter 조건절 처리
    //검색어조건절처리
    public function setSearchWord(string $word): void
    {
        $this->model->orLike($this->model->getTable() . '.alias', $word, 'both');
        parent::setSearchWord($word);
    }
    //고객 예치금
    protected function getClientBalance(ClientEntity $clientEntity): int
    {
        return $clientEntity->getAccountBalance();
    }
    //예치금 입금 처리
    protected function deposit_process(ClientEntity $clientEntity, array $formDatas): CommonEntity
    {
        if (!array_key_exists('bank', $formDatas)) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 은행이 정의되지 않았습니다.");
        }
        // 기본값 세팅
        $formDatas['alias'] = $formDatas['alias'] ?? $clientEntity->getTitle();
        $formDatas['issue_at'] = array_key_exists('issue_at', $formDatas) ? $formDatas['issue_at'] : date('Y-m-d');
        return parent::deposit_process($clientEntity, $formDatas);
    }

    //예치금 출금 처리
    protected function withdrawal_process(ClientEntity $clientEntity, array $formDatas): CommonEntity
    {
        if (!array_key_exists('bank', $formDatas)) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 은행이 정의되지 않았습니다.");
        }
        // 기본값 세팅
        $formDatas['alias'] = $formDatas['alias'] ?? $clientEntity->getTitle();
        $formDatas['issue_at'] = array_key_exists('issue_at', $formDatas) ? $formDatas['issue_at'] : date('Y-m-d');
        return parent::withdrawal_process($clientEntity, $formDatas);
    }

    public function withdrawalByPayment(PaymentEntity $paymentEntity, array $formDatas = []): CommonEntity
    {
        $formDatas['bank'] = BANKS['결제차감'];
        return parent::withdrawalByPayment($paymentEntity, $formDatas);
    }
}

