<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class SupplyChainDemoSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 1. Create Partners
        $supplier = \App\Models\Partner::create([
            'name' => 'Industrial Chemicals Ltd',
            'type' => 'supplier',
            'code' => 'SUP-001',
            'email' => 'sales@ind-chem.com',
            'phone' => '+1-555-0199',
            'address' => '123 Industrial Way, New Jersey',
            'tax_number' => 'VAT-US-12345',
            'status' => true,
        ]);

        $customer = \App\Models\Partner::create([
            'name' => 'Global Logistics Group',
            'type' => 'customer',
            'code' => 'CUS-001',
            'email' => 'orders@gl-logistics.com',
            'phone' => '+1-555-0200',
            'address' => '789 Export Ave, Port Newark',
            'tax_number' => 'VAT-US-67890',
            'status' => true,
        ]);

        // 2. Create a Purchase Order (Draft)
        $product = \App\Models\Product::first(); // Raw material
        if ($product) {
            $po = \App\Models\PurchaseOrder::create([
                'po_number' => 'PO-DEMO-001',
                'partner_id' => $supplier->id,
                'order_date' => now(),
                'expected_date' => now()->addDays(7),
                'total_amount' => 5000,
                'status' => 'draft',
                'notes' => 'Bulk ink purchase for testing.',
            ]);

            $po->items()->create([
                'product_id' => $product->id,
                'quantity' => 100,
                'unit_price' => 50,
                'total_price' => 5000,
            ]);
        }

        // 3. Create a Sales Order (Confirmed)
        $finishedGood = \App\Models\Product::where('sku', 'like', '%FG%')->first()
            ?? \App\Models\Product::orderBy('id', 'desc')->first();

        if ($finishedGood) {
            $so = \App\Models\SalesOrder::create([
                'so_number' => 'SO-DEMO-001',
                'partner_id' => $customer->id,
                'order_date' => now(),
                'delivery_date' => now()->addDays(3),
                'total_amount' => 12000,
                'status' => 'confirmed',
                'notes' => 'Urgent export order.',
            ]);

            $so->items()->create([
                'product_id' => $finishedGood->id,
                'quantity' => 50,
                'unit_price' => 240,
                'total_price' => 12000,
            ]);
        }
    }
}
