<?php

namespace App\Services\Customer;

use App\Entities\Customer\ClientEntity;
use App\Entities\Customer\CouponEntity;
use App\Helpers\Customer\CouponHelper;
use App\Models\Customer\CouponModel;

class CouponService extends CustomerService
{
    public function __construct()
    {
        parent::__construct(new CouponModel(), new CouponHelper());
        $this->addClassName('Coupon');
    }
    public function getFormFields(): array
    {
        return [
            "clientinfo_uid",
            "title",
            "cnt",
            "status",
            "content"
        ];
    }
    public function getFormFilters(): array
    {
        return [
            "clientinfo_uid",
            "status",
        ];
    }
    public function getBatchjobFields(): array
    {
        return ['status'];
    }
    //고객쿠폰처리
    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()->setCoupon($formDatas['status'], $entity, intval($formDatas['cnt']));
    }
    //기본 기능부분
    public function create(array $formDatas): CouponEntity
    {
        // dd($formDatas);
        $this->setBalance($formDatas);
        $entity = parent::create($formDatas);
        //Log처리
        $this->getMylogService()->create(['title' => "[{$entity->getTitle()}] 쿠폰 처리", 'status' => $entity->getStatus()]);
        return $entity;
    }
}
