<?php

namespace Database\Seeders;

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

class MemberDueSeeder extends Seeder
{
    public function run(): void
    {
        $bendahara = User::role('bendahara')->first();

        $members = User::whereDoesntHave('roles', fn ($q) => $q->whereIn('name', ['super_admin', 'bendahara']))
            ->get();

        $periods = [
            now()->subMonths(3)->format('Y-m'),
            now()->subMonths(2)->format('Y-m'),
            now()->subMonth()->format('Y-m'),
            now()->format('Y-m'),
        ];

        foreach ($members as $i => $user) {
            foreach ($periods as $j => $period) {
                MemberDue::firstOrCreate(
                    ['user_id' => $user->id, 'period' => $period],
                    [
                        'amount'     => 10000,
                        'status'     => ($i + $j) % 3 === 0 ? 'belum' : 'lunas',
                        'created_by' => $bendahara?->id,
                    ]
                );
            }
        }
    }
}
