<?php

namespace App\Services\Customer;

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

class PointService extends CustomerService
{
    public function __construct()
    {
        parent::__construct(new PointModel(), new PointHelper());
        $this->addClassName('Point');
    }
    public function getFormFields(): array
    {
        return [
            "clientinfo_uid",
            "title",
            "amount",
            "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()->setPoint($formDatas['status'], $entity, intval($formDatas['amount']));
    }
    //기본 기능부분
    public function create(array $formDatas): PointEntity
    {
        $this->setBalance($formDatas);
        $entity =  parent::create($formDatas);
        //Log처리
        $this->getMylogService()->create(['title' => "[{$entity->getTitle()}] 포인트 처리", 'status' => $entity->getStatus()]);
        return $entity;
    }
}
