<?php

namespace App\Services;

use App\Models\MylogModel;
use App\Libraries\PipelineStepInterface;
use App\Libraries\OperationContext;
use App\Helpers\MylogHelper;
use App\Forms\MylogForm;
use App\Entities\MylogEntity;
use App\DTOs\MylogDTO;

class MylogService extends CommonService implements PipelineStepInterface
{
    private $_form = null;
    private $_helper = null;
    public function __construct(MylogModel $model)
    {
        parent::__construct($model);
        $this->addClassPaths('Mylog');
    }
    protected function getDTOClass(): string
    {
        return MylogDTO::class;
    }
    public function createDTO(array $formDatas): MylogDTO
    {
        return new MylogDTO($formDatas);
    }
    public function getEntityClass(): string
    {
        return MylogEntity::class;
    }
    public function getFormService(): MylogForm
    {
        if ($this->_form === null) {
            $this->_form = new MylogForm();
            $this->_form->setAttributes([
                'pk_field'         => $this->model->getPKField(),
                'title_field'      => $this->model->getTitleField(),
                'table'            => $this->model->getTable(),
                'useAutoIncrement' => $this->model->useAutoIncrement(),
                'class_path' => $this->getClassPaths(false)
            ]);
        }
        return $this->_form;
    }
    public function action_init_process(string $action, array $formDatas = []): void
    {
        $fields = ['title', 'content'];
        $filters = ['user_uid'];
        $indexFilter     = $filters;
        $batchjobFilters = $filters;
        switch ($action) {
            case 'create':
            case 'create_form':
            case 'modify':
            case 'modify_form':
                break;
            case 'view':
                $fields = [...$fields, 'status', 'created_at'];
                break;
            case 'index':
            case 'download':
                $fields = [...$fields, 'status', 'created_at'];
                break;
        }
        $this->getFormService()->setFormFields($fields);
        $this->getFormService()->setFormRules($action, $fields);
        $this->getFormService()->setFormFilters($filters);
        $this->getFormService()->setFormOptions($action, $filters, $formDatas);
        $this->getFormService()->setIndexFilters($indexFilter);
        $this->getFormService()->setBatchjobFilters($batchjobFilters);
    }
    public function getHelper(): MylogHelper
    {
        if ($this->_helper === null) {
            $this->_helper = new MylogHelper();
            $this->_helper->setAttributes([
                'pk_field'         => $this->model->getPKField(),
                'title_field'      => $this->model->getTitleField(),
                'table'            => $this->model->getTable(),
                'useAutoIncrement' => $this->model->useAutoIncrement(),
                'class_path' => $this->getClassPaths(false)
            ]);
        }
        return $this->_helper;
    }
    /**
     * LogService는 START/SUCCESS/FAILURE 로그를 기록합니다.
     * LogService는 파이프라인의 시작과 끝에서만 Executor에 의해 직접 호출됩니다.
     */
    public function log(OperationContext $context, string $status): void
    {
        if ($status === STATUS['FAILED'] && $context->error) {
            $message = "[{$context->action}_{$status}] Error: {$context->error->getMessage()}";
        } else {
            $message = "[{$context->action}_{$status}] Steps Executed: " . count($context->pipelineDatas);
        }
        log_message('debug', $message);
        $formDatas = [];
        $formDatas['user'] = $context->auth->getUID();
        $this->create(new MylogDTO());
    }

    // PipelineStep 구현은 이 예시에서는 사용하지 않습니다. (로그는 Executor가 감쌈)
    public function handle(OperationContext $context): OperationContext
    {
        return $context;
    }

    //기본 기능부분
    protected function getEntity_process(mixed $entity): MylogEntity
    {
        return $entity;
    }
    protected function create_process(array $formDatas): MylogEntity
    {
        return parent::create_process($formDatas);
    }
    protected function modify_process($entity, array $formDatas): MylogEntity
    {
        return parent::modify_process($entity, $formDatas);
    }
}
