<?php

namespace Database\Seeders;

use App\Models\Membership;
use App\Models\User;
use Illuminate\Database\Seeder;

class MembershipSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $users = User::all();

        // Assign memberships to about 60% of users
        $usersWithMemberships = $users->random((int) ceil($users->count() * 0.6));
        $userMembershipUpdates = [];

        // Prepare membership data
        $activeMemberships = [];
        $expiredMemberships = [];
        $canceledMemberships = [];

        foreach ($usersWithMemberships as $user) {
            // 70% chance of active membership
            if (fake()->boolean(70)) {
                $activeMemberships[] = $user->id;
            }
            // 15% chance of expired membership
            elseif (fake()->boolean(50)) {
                $expiredMemberships[] = $user->id;
            }
            // 15% chance of canceled membership
            else {
                $canceledMemberships[] = $user->id;
            }
        }

        // Bulk create memberships by type
        if (! empty($activeMemberships)) {
            $createdActive = Membership::factory()
                ->count(count($activeMemberships))
                ->active()
                ->sequence(fn ($sequence) => ['user_id' => $activeMemberships[$sequence->index]])
                ->create();

            // Collect user updates for current_membership_id
            foreach ($createdActive as $index => $membership) {
                $userMembershipUpdates[$activeMemberships[$index]] = $membership->id;
            }
        }

        if (! empty($expiredMemberships)) {
            Membership::factory()
                ->count(count($expiredMemberships))
                ->expired()
                ->sequence(fn ($sequence) => ['user_id' => $expiredMemberships[$sequence->index]])
                ->create();
        }

        if (! empty($canceledMemberships)) {
            Membership::factory()
                ->count(count($canceledMemberships))
                ->canceled()
                ->sequence(fn ($sequence) => ['user_id' => $canceledMemberships[$sequence->index]])
                ->create();
        }

        // Update users with current membership
        foreach ($userMembershipUpdates as $userId => $membershipId) {
            User::where('id', $userId)->update(['current_membership_id' => $membershipId]);
        }

        // Give some users multiple memberships (historical + current)
        $historyUserIds = $usersWithMemberships
            ->random((int) min(5, $usersWithMemberships->count()))
            ->pluck('id')
            ->toArray();

        if (! empty($historyUserIds)) {
            Membership::factory()
                ->count(count($historyUserIds))
                ->expired()
                ->sequence(fn ($sequence) => ['user_id' => $historyUserIds[$sequence->index]])
                ->create();
        }
    }
}
