<?php

namespace Database\Seeders;

use App\Enums\OrderStatus;
use App\Models\Invoice;
use App\Models\Order;
use Illuminate\Database\Seeder;

class InvoiceSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $orders = Order::all()->where('status', OrderStatus::INVOICED);

        for ($i = count($orders); $i > 4; $i -= 5) {
            $invoice = Invoice::factory()->create();

            for ($o = 0; $o < 5; $o++) {
                $invoice->orders()->save(Order::findOrFail($i + $o));
                $invoice->save();
            }

            $invoice->calculateTotals();
        }
    }
}
