<?php

namespace App\ScheduledTasks\Actions;

use App\Jobs\RunScheduledSyncTask;
use App\ScheduledTasks\ActionResult;
use App\ScheduledTasks\Contracts\TaskAction;
use App\ScheduledTasks\TaskRunContext;

/**
 * System action wrapping the existing AutoTrader/DMS stock sync. Not user-selectable:
 * it exists only on the seeded system task, so a tenant can never compose their own
 * stock sync. Credentials stay in .env / platform config, never in task config.
 */
class RunStockSyncAction implements TaskAction
{
    public function key(): string
    {
        return 'stock-sync';
    }

    public function label(): string
    {
        return __('scheduled_tasks.actions.stock-sync');
    }

    public function available(): bool
    {
        return true;
    }

    public function userSelectable(): bool
    {
        return false;
    }

    public function configFields(): array
    {
        return [];
    }

    public function rules(array $config): array
    {
        return [];
    }

    public function execute(array $config, TaskRunContext $context): ActionResult
    {
        // Run synchronously within this task run so the outcome is captured. ignoreSchedule
        // bypasses the job's own time-window check (the scheduled task owns the timing);
        // full vs quick is left to the job's normal frequency/hour decision.
        dispatch_sync(new RunScheduledSyncTask(true));

        return ActionResult::completed('Stock sync dispatched');
    }

    public function queue(): string
    {
        return 'sync';
    }
}
