<?php
declare(strict_types=1);

return function (\App\Core\Database $db): void {
    // Sports members: update membership_type for members with active sports records
    $db->query(
        "UPDATE members m
         INNER JOIN sports_members sm ON sm.member_id = m.id
         SET m.membership_type = 'sports',
             m.member_category = 'sports_member',
             m.updated_at = NOW()
         WHERE m.membership_type = 'working'
           AND m.is_archived = 0
           AND sm.status = 'active'",
        []
    );

    // Seasonal members: only migrate members who have ONLY seasonal membership
    // (never had a working membership_fee payment — meaning they were created specifically as seasonal)
    $db->query(
        "UPDATE members m
         INNER JOIN seasonal_memberships sm ON sm.member_id = m.id AND sm.person_type = 'member'
         LEFT JOIN payments p ON p.member_id = m.id
             AND p.payment_type = 'membership_fee'
             AND p.is_voided = 0
         SET m.membership_type = 'seasonal',
             m.member_category = 'seasonal_member',
             m.updated_at = NOW()
         WHERE m.membership_type = 'working'
           AND m.is_archived = 0
           AND sm.status = 'active'
           AND p.id IS NULL",
        []
    );

    // Foreign and Honorary are already correctly typed by their respective controllers
    // No action needed for them
};
