<?php
defined('_JEXEC') or die;

use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Event\SubscriberInterface;

use Joomla\Component\Scheduler\Administrator\Traits\TaskPluginTrait;
use Joomla\Component\Scheduler\Administrator\Event\ExecuteTaskEvent;
use Joomla\Component\Scheduler\Administrator\Task\Status;

use EIS\Component\EIS\Administrator\Service\DocumentScanner;

final class PlgTaskEisdocumentscan extends CMSPlugin implements SubscriberInterface
{
    use TaskPluginTrait;

    
    protected const TASKS_MAP = [
        'eisdocumentscan.documentscan' => [
            'langConstPrefix' => 'PLG_TASK_EISDOCUMENTSCAN_TASK_DOCUMENTSCAN',
            'method'          => 'documentscan',
            'form'            => 'documentscan',
        ],
    ];

    public static function getSubscribedEvents(): array
    {
        return [
            // Wichtig: auf Trait-Methoden mappen!
            'onTaskOptionsList'     => 'advertiseRoutines',
            'onExecuteTask'         => 'standardRoutineHandler',
            'onContentPrepareForm'  => 'enhanceTaskItemForm',
        ];
    }

    /**
     * Muss int Status zurückgeben
     */
    public function documentscan(ExecuteTaskEvent $event): int
    {
        try {
            DocumentScanner::run();

            $this->logTask('EIS Dokumentenscan erfolgreich', 'info');

            return Status::OK;
        } catch (\Throwable $e) {
            $this->logTask('EIS Dokumentenscan FEHLER: ' . $e->getMessage(), 'error');

            return Status::KNOCKOUT;
        }
    }
}
