<?php

namespace App\Services\Customer;

use App\Entities\Customer\PointEntity;
use App\Models\Customer\PointModel;
use App\Entities\Customer\ClientEntity;

class PointService extends CustomerService
{
    public function __construct()
    {
        parent::__construct(new PointModel());
        $this->addClassName('Point');
    }
    public function getFormFields(): array
    {
        return [
            'fields' => [
                "clientinfo_uid",
                "title",
                "amount",
                "status",
            ],
            'filters' => [
                "clientinfo_uid",
                "status",
            ],
        ];
    }
    public function getIndexFields(): array
    {
        return [
            'fields' => [
                "clientinfo_uid",
                "title",
                "amount",
                "status",
            ],
            'filters' => [
                "clientinfo_uid",
                "status",
            ],
        ];
    }
    public function getBatchjobFields(): array
    {
        return ['status'];
    }
    //기본 기능부분

    private function setBalance(array $formDatas): void
    {
        //point_balance 체크
        $entity = $this->getClientService()->getEntity($formDatas['clientinfo_uid']);
        if (!$entity) {
            throw new \Exception("{$formDatas['clientinfo_uid']}에 대한 고객정보를 찾을수 없습니다.");
        }
        $amount = intval($formDatas['amount']);
        if ($formDatas['status'] === PointEntity::DEFAULT_STATUS) { //입금, 쿠폰추가
            $entity = $this->getClientService()->deposit($entity, 'point_balance',   $amount);
        } else { // 출금, 쿠폰사용
            $entity = $this->getClientService()->withdrawal($entity, 'point_balance',   $amount);
        }
    }
    public function create(array $formDatas): PointEntity
    {
        $this->setBalance($formDatas);
        return parent::create($formDatas);
    }
}
