<?php

namespace App\Services\Customer\Wallet;

use App\Entities\CommonEntity;
use App\Entities\Customer\ClientEntity;
use App\Entities\PaymentEntity;
use App\Models\CommonModel;
use App\Services\Customer\CustomerService;
use RuntimeException;

abstract class WalletService extends CustomerService
{
    protected function __construct(CommonModel $model)
    {
        parent::__construct($model);
        $this->addClassPaths('Wallet');
    }
    //기본기능
    //생성
    protected function create_process(array $formDatas): CommonEntity
    {
        if (!array_key_exists('clientinfo_uid', $formDatas)) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 고객정보가 정의되지 않았습니다.");
        }
        if (!array_key_exists('status', $formDatas)) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 상태정보가 정의되지 않았습니다.");
        }
        //고객 정보
        $clientEntity = service('customer_clientservice')->getEntity($formDatas['clientinfo_uid']);
        if (!$clientEntity instanceof ClientEntity) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$formDatas['clientinfo_uid']}에 해당하는 고객정보를 찾을수 없습니다.");
        }
        switch ($formDatas['status']) {
            case STATUS['DEPOSIT']:
                $formDatas = $this->deposit_process($clientEntity, $formDatas);
                break;
            case STATUS['WITHDRAWAL']:
                $formDatas = $this->withdrawal_process($clientEntity, $formDatas);
                break;
            default:
                throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 정의되지 않은 상태정보입니다.");
        }
        $entity = parent::create_process($formDatas);
        if (!$entity) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 고객의 Wallet정보 생성에 실패하였습니다.");
        };
        return $entity;
    }
    //입금,쿠폰 충전 처리
    protected function deposit_process(ClientEntity $clientEntity, array $formDatas): array
    {
        $formDatas['issue_at'] = array_key_exists('issue_at', $formDatas) ? $formDatas['issue_at'] : date('Y-m-d');
        return $formDatas;
    }
    //출금,쿠폰 사용 처리
    protected function withdrawal_process(ClientEntity $clientEntity, array $formDatas): array
    {
        $formDatas['issue_at'] = array_key_exists('issue_at', $formDatas) ? $formDatas['issue_at'] : date('Y-m-d');
        return $formDatas;
    }
    //결제관련 출금 처리(결제를 통해서만 호출되므로 action_init_process로 fields,rules 초기화 필요)
    final public function withdrawalByPayment(PaymentEntity $paymentEntity): void
    {
        //고객 정보
        $clientEntity = service('customer_clientservice')->getEntity($paymentEntity->getClientInfoUid());
        if (!$clientEntity instanceof ClientEntity) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$paymentEntity->getClientInfoUid()}에 해당하는 고객정보를 찾을수 없습니다.");
        }
        $formDatas = [
            'clientinfo_uid' => $paymentEntity->getClientInfoUid(),
            'title'         =>  $paymentEntity->getTitle(),
            'amount'         => $paymentEntity->getAmount(),
            'status'         => STATUS['WITHDRAWAL'],
        ];
        $this->create_process($formDatas);
    }
}
