<?php
declare(strict_types=1);

namespace App\Modules\Seasonal\Models;

use App\Core\Model;
use App\Core\App;

class SeasonalMembership extends Model
{
    protected static string $table = 'seasonal_memberships';
    protected static string $primaryKey = 'id';
    protected static bool $timestamps = true;
    protected static bool $softDelete = true;
    protected static bool $dispatchEvents = true;

    protected static array $fillable = [
        'member_id', 'branch_id', 'start_date', 'end_date',
        'fee_amount', 'fee_receipt_number', 'status', 'carnet_marking', 'notes',
    ];

    public static function getForMember(int $memberId): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT s.*, b.name_ar as branch_name FROM seasonal_memberships s JOIN branches b ON b.id = s.branch_id WHERE s.member_id = ? AND s.is_archived = 0 ORDER BY s.start_date DESC",
            [$memberId]
        );
    }

    public static function getActive(): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT s.*, m.full_name_ar as member_name, m.membership_number, b.name_ar as branch_name
             FROM seasonal_memberships s
             JOIN members m ON m.id = s.member_id
             JOIN branches b ON b.id = s.branch_id
             WHERE s.is_archived = 0 AND s.status = 'active'
             ORDER BY s.end_date ASC"
        );
    }

    public function isExpired(): bool
    {
        return $this->end_date && $this->end_date < date('Y-m-d');
    }
}