<?php
declare(strict_types=1);

namespace CronJobs;

use App\Core\Database;
use App\Core\Logger;

class FormExpiryJob
{
    private Database $db;
    public function __construct(Database $db) { $this->db = $db; }
    public function shouldRun(): bool { return true; }

    public function run(): array
    {
        $expired = $this->db->select(
            "SELECT id, form_number FROM form_submissions WHERE status IN ('submitted','under_review') AND expires_at IS NOT NULL AND expires_at < NOW()"
        );
        foreach ($expired as $form) {
            $this->db->update('form_submissions', ['status' => 'expired', 'updated_at' => date('Y-m-d H:i:s')], '`id` = ?', [(int) $form['id']]);
            Logger::info("Form expired: #{$form['id']} — {$form['form_number']}");
        }
        return ['processed' => count($expired)];
    }
}