<?php

use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Schedule;

Artisan::command('inspire', function () {
    $this->comment(Inspiring::quote());
})->purpose('Display an inspiring quote');

// There's no Redis/Supervisor in this environment, so queued jobs (e.g. RenderVideoCreativeJob)
// are processed by a short-lived `queue:work` invocation kicked off every minute by the scheduler
// instead of a long-running worker daemon. `withoutOverlapping()` means a still-running invocation
// (e.g. mid-Sora-render) blocks the next minute's attempt rather than starting a second worker.
//
// KNOWN LIMITATION (flagged, not fixed here — needs explicit sign-off per the AI-Assisted Coding
// Guidelines before changing, since it's cron/concurrency architecture): a single video render can
// hold this worker for several minutes, during which every other queued job in the app (image
// generation, ad copy, etc.) is blocked behind it — there's no separate queue/worker for slow video
// jobs. The withoutOverlapping() lock below is capped at 10 minutes so a killed worker can't wedge
// the queue for the default 24h, but jobs still queue up behind a single serial worker either way.
Schedule::command('queue:work --queue='.config('queue.connections.database.queue').' --stop-when-empty --max-time=50')
    ->everyMinute()
    ->withoutOverlapping(10)
    ->runInBackground();

// Daily refresh of platform OAuth access tokens expiring within 10 days (Meta 60-day, LinkedIn, Google)
Schedule::command('platforms:refresh-tokens --days=10')
    ->daily()
    ->withoutOverlapping(10)
    ->runInBackground();
Schedule::command('campaigns:monitor-adops --hourly')
    ->hourlyAt(25)
    ->withoutOverlapping(50);

Schedule::command('campaigns:monitor-adops --daily')
    ->dailyAt('06:20')
    ->withoutOverlapping(120);
