<?php

namespace Modules\SystemCore\Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Modules\SystemCore\App\Enums\OrderStatus;
use Modules\SystemCore\App\Models\CoreCustomer;
use Modules\SystemCore\App\Models\CoreSalesOrder;

class SalesOrderSeeder extends Seeder
{
    public function run(): void
    {
        // Disable foreign key checks
        DB::statement('SET FOREIGN_KEY_CHECKS=0;');

        // Truncate tables (including line items)
        DB::table('core_sales_order_lines')->truncate();
        DB::table('core_sales_orders')->truncate();

        // Re-enable foreign key checks
        DB::statement('SET FOREIGN_KEY_CHECKS=1;');

        $customers = CoreCustomer::all();
        $users = User::limit(5)->get();

        if ($customers->isEmpty()) {
            $this->command->warn('No customers found. Please run CustomerSeeder first.');

            return;
        }

        if ($users->isEmpty()) {
            $this->command->warn('No users found. Cannot create sales orders without users.');

            return;
        }

        $statuses = [
            OrderStatus::Draft,
            OrderStatus::Pending,
            OrderStatus::Confirmed,
            OrderStatus::Processing,
            OrderStatus::Shipped,
            OrderStatus::Delivered,
            OrderStatus::Completed,
        ];

        $paymentStatuses = ['pending', 'partial', 'paid'];
        $paymentMethods = ['cash', 'bank_transfer', 'credit_card', 'check'];

        $orders = [];

        for ($i = 1; $i <= 10; $i++) {
            $customer = $customers->random();
            $user = $users->random();
            $status = $statuses[array_rand($statuses)];
            $paymentStatus = $paymentStatuses[array_rand($paymentStatuses)];

            // Random date within past 3 months
            $orderDate = now()->subDays(rand(1, 90));
            $deliveryDate = (clone $orderDate)->addDays(rand(3, 14));

            // Generate realistic amounts
            $subtotal = rand(500, 50000) + (rand(0, 99) / 100);
            $taxAmount = $subtotal * 0.15; // 15% tax
            $discountAmount = rand(0, 1) ? 0 : ($subtotal * rand(5, 15) / 100);
            $shippingCost = rand(0, 1) ? 0 : rand(10, 100);
            $totalAmount = $subtotal + $taxAmount - $discountAmount + $shippingCost;

            $orders[] = [
                'customer_id' => $customer->id,
                'user_id' => $user->id,
                'order_date' => $orderDate,
                'delivery_date' => $deliveryDate,
                'status' => $status,
                'subtotal' => $subtotal,
                'tax_amount' => $taxAmount,
                'discount_amount' => $discountAmount,
                'shipping_cost' => $shippingCost,
                'total_amount' => $totalAmount,
                'payment_status' => $paymentStatus,
                'payment_method' => $paymentMethods[array_rand($paymentMethods)],
                'notes' => "Sample sales order #{$i} for testing purposes",
                'created_at' => $orderDate,
                'updated_at' => $orderDate,
            ];
        }

        foreach ($orders as $orderData) {
            CoreSalesOrder::create($orderData);
        }

        $this->command->info('Sample sales orders created successfully.');
    }
}
