<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Services\InstallerService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;

/**
 * Admin-only maintenance page: manual "purge old snapshots" action, plus the
 * ready-to-copy external-cron URLs for hosts with no cron access at all
 * (see TaskController for the token-protected endpoints those URLs hit).
 */
class MaintenanceController extends Controller
{
    public function index(InstallerService $installer)
    {
        $lock = $installer->lockData();

        return view('maintenance.index', [
            'tasksToken' => $lock['tasks_token'] ?? null,
            'appUrl' => rtrim($lock['app_url'] ?? url('/'), '/'),
        ]);
    }

    public function purgeSnapshots(Request $request)
    {
        $data = $request->validate([
            'days' => ['required', 'integer', 'min:1', 'max:3650'],
        ]);

        Artisan::call('attendance:purge-snapshots', ['--days' => $data['days']]);

        return back()->with('status', trim(Artisan::output()) ?: 'Purge ran.');
    }
}
