<?php

namespace Database\Seeders;

use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Product;
use App\Models\User;
use Illuminate\Database\Seeder;

class OrderSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $customer = User::where('role', 'customer')->first();
        $products = Product::take(5)->get();

        if (! $customer || $products->isEmpty()) {
            $this->command->warn('Please run UserSeeder and ProductSeeder first');

            return;
        }

        // Create sample orders with different statuses
        $orders = [
            [
                'status' => 'delivered',
                'payment_status' => 'paid',
                'confirmed_at' => now()->subDays(5),
                'shipped_at' => now()->subDays(3),
                'delivered_at' => now()->subDays(1),
            ],
            [
                'status' => 'shipped',
                'payment_status' => 'paid',
                'confirmed_at' => now()->subDays(2),
                'shipped_at' => now()->subDay(),
                'delivered_at' => null,
            ],
            [
                'status' => 'confirmed',
                'payment_status' => 'paid',
                'confirmed_at' => now()->subDay(),
                'shipped_at' => null,
                'delivered_at' => null,
            ],
            [
                'status' => 'pending',
                'payment_status' => 'pending',
                'confirmed_at' => null,
                'shipped_at' => null,
                'delivered_at' => null,
            ],
        ];

        foreach ($orders as $index => $orderData) {
            $order = Order::create([
                'user_id' => $customer->id,
                'customer_name' => $customer->name,
                'customer_phone' => $customer->phone,
                'customer_address' => 'House #123, Road #456, Dhanmondi, Dhaka-1205',
                'subtotal' => 0, // Will be calculated below
                'tax_amount' => 0,
                'shipping_amount' => $index === 0 ? 0 : 50, // Free shipping for first order
                'discount_amount' => $index === 0 ? 100 : 0, // Discount for first order
                'total_amount' => 0, // Will be calculated below
                'payment_method' => 'cash_on_delivery',
                'notes' => $index === 0 ? 'Please deliver before 6 PM' : null,
                ...$orderData,
            ]);

            // Add 2-4 random products to each order
            $orderProducts = $products->random(rand(2, 4));
            $subtotal = 0;

            foreach ($orderProducts as $product) {
                $quantity = rand(1, 3);
                $price = $product->price;

                OrderItem::create([
                    'order_id' => $order->id,
                    'product_id' => $product->id,
                    'product_name' => $product->name,
                    'quantity' => $quantity,
                    'price' => $price,
                    'total' => $price * $quantity,
                ]);

                $subtotal += $price * $quantity;
            }

            // Update order totals
            $total = $subtotal + $order->shipping_amount + $order->tax_amount - $order->discount_amount;
            $order->update([
                'subtotal' => $subtotal,
                'total_amount' => $total,
            ]);
        }

        $this->command->info('Created '.count($orders).' sample orders');
    }
}
