<?php
declare(strict_types=1);

namespace CronJobs;

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

class SeasonalExpiryJob
{
    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 s.id, s.member_id, m.full_name_ar FROM seasonal_memberships s JOIN members m ON m.id = s.member_id WHERE s.is_archived = 0 AND s.status = 'active' AND s.end_date < CURDATE()"
        );
        foreach ($expired as $s) {
            $this->db->update('seasonal_memberships', ['status' => 'expired', 'updated_at' => date('Y-m-d H:i:s')], '`id` = ?', [(int) $s['id']]);
            Logger::info("Seasonal membership expired: #{$s['id']} — member #{$s['member_id']}");
        }
        return ['processed' => count($expired)];
    }
}