<?php

namespace App\Services;

use App\Entities\MyLogEntity;
use App\Models\MyLogModel;
use App\Libraries\LogCollector;
use App\Services\UserService;

class MyLogService extends CommonService
{
    private $_userService = null;
    public function __construct()
    {
        parent::__construct(new MyLogModel());
        $this->addClassName('MyLog');
    }
    public function getFormFields(): array
    {
        return [
            'fields' => [
                "user_uid",
                "title",
                "content",
                "status",
            ],
            'filters' => [
                'user_uid',
                'status'
            ],
        ];
    }
    public function getIndexFields(): array
    {
        return [
            'fields' => [
                'user_uid',
                'title',
                'status',
                'created_at'
            ],
            'filters' => [
                'user_uid',
                'status'
            ],
        ];
    }
    public function getBatchjobFields(): array
    {
        return ['status'];
    }
    public function getUserService(): UserService
    {
        if (!$this->_userService) {
            $this->_userService = new UserService();
        }
        return $this->_userService;
    }
    //기본 기능부분
    //FieldForm관련용
    public function getFormFieldOption(string $field, array $options = []): array
    {
        switch ($field) {
            case 'user_uid':
                $options = $this->getUserService()->getEntities();
                break;
            default:
                $options = parent::getFormFieldOption($field, $options);
                break;
        }
        return $options;
    }
    public function save(string $class, string $method, string $title, int $user_uid): MyLogEntity
    {
        $formDatas = [
            'user_uid' => $user_uid,
            'title' => sprintf("%s->%s %s", $class, $method, $title),
            'content' => LogCollector::dump(),
        ];
        // dd($formDatas);
        LogCollector::clear();
        return $this->create($formDatas);
    }
    //List 검색용
    public function index_condition_filterWord(string $word): void
    {
        $this->getModel()->orLike($this->getModel()::TABLE . "." . $this->getModel()::TITLE, $word, 'both');
        $this->getModel()->orLike($this->getModel()::TABLE . '.content', $word, 'both');
    }
}
