<?php
declare(strict_types=1);

namespace CronJobs;

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

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