<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Item;
use App\Models\ItemCategory;
use App\Models\Contact;
use App\Models\Uom;
use App\Models\Bom;
use App\Models\BomItem;
use App\Models\ItemSupplier;

class ProductManagementSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Get or create UOMs
        $pcsUom = Uom::firstOrCreate(['code' => 'PCS'], [
            'name' => 'Pieces',
            'factor_to_base' => 1.000000,
        ]);

        $kgUom = Uom::firstOrCreate(['code' => 'KG'], [
            'name' => 'Kilograms',
            'factor_to_base' => 1.000000,
        ]);

        // Get or create categories
        $rawMaterialCategory = ItemCategory::firstOrCreate(['name' => 'Raw Materials']);

        $finishedGoodsCategory = ItemCategory::firstOrCreate(['name' => 'Finished Goods']);

        // Create Raw Materials (RM)
        $copperRM = Item::firstOrCreate(['sku' => 'RM-COPPER-001'], [
            'name' => 'Copper Wire',
            'type' => 'RM',
            'item_category_id' => $rawMaterialCategory->id,
            'uom_id' => $kgUom->id,
            'unit_of_measure' => 'KG',
            'reorder_level' => 100,
            'status' => 'active',
            'is_active' => true,
        ]);

        $danaRM = Item::firstOrCreate(['sku' => 'RM-DANA-001'], [
            'name' => 'Dana (Insulation Material)',
            'type' => 'RM',
            'item_category_id' => $rawMaterialCategory->id,
            'uom_id' => $kgUom->id,
            'unit_of_measure' => 'KG',
            'reorder_level' => 50,
            'status' => 'active',
            'is_active' => true,
        ]);

        // Create Finished Good (FG)
        $cableFG = Item::firstOrCreate(['sku' => 'FG-CABLE-001'], [
            'name' => 'Electrical Cable',
            'type' => 'FG',
            'item_category_id' => $finishedGoodsCategory->id,
            'uom_id' => $pcsUom->id,
            'unit_of_measure' => 'PCS',
            'reorder_level' => 10,
            'status' => 'active',
            'is_active' => true,
        ]);

        // Create vendors/suppliers
        $copperSupplier = Contact::firstOrCreate(['email' => 'copper@supplier.com'], [
            'name' => 'Copper Supplier Co.',
            'contact_person' => 'John Copper',
            'phone' => '+92-300-1234567',
            'type' => 'vendor',
            'is_active' => true,
        ]);

        $danaSupplier = Contact::firstOrCreate(['email' => 'dana@supplier.com'], [
            'name' => 'Dana Materials Ltd.',
            'contact_person' => 'Sarah Dana',
            'phone' => '+92-300-7654321',
            'type' => 'vendor',
            'is_active' => true,
        ]);

        // Create item-supplier relationships
        ItemSupplier::firstOrCreate([
            'item_id' => $copperRM->id,
            'contact_id' => $copperSupplier->id,
        ], [
            'is_primary' => true,
            'lead_time_days' => 7,
            'min_order_qty' => 50.0000,
            'last_price' => 850.0000,
        ]);

        ItemSupplier::firstOrCreate([
            'item_id' => $danaRM->id,
            'contact_id' => $danaSupplier->id,
        ], [
            'is_primary' => true,
            'lead_time_days' => 5,
            'min_order_qty' => 25.0000,
            'last_price' => 1200.0000,
        ]);

        // Create BOM for the finished good
        $bom = Bom::firstOrCreate([
            'item_id' => $cableFG->id,
            'version' => 1,
        ], [
            'description' => 'Standard electrical cable BOM',
            'is_active' => true,
        ]);

        // Create BOM items (components)
        BomItem::firstOrCreate([
            'bom_id' => $bom->id,
            'component_item_id' => $copperRM->id,
        ], [
            'qty_per' => 0.500000, // 0.5 KG copper per cable
            'scrap_pct' => 2.50,   // 2.5% scrap
        ]);

        BomItem::firstOrCreate([
            'bom_id' => $bom->id,
            'component_item_id' => $danaRM->id,
        ], [
            'qty_per' => 0.250000, // 0.25 KG dana per cable
            'scrap_pct' => 1.00,   // 1% scrap
        ]);

        $this->command->info('Product Management data seeded successfully!');
        $this->command->info('Created:');
        $this->command->info('- 2 Raw Materials (Copper Wire, Dana)');
        $this->command->info('- 1 Finished Good (Electrical Cable)');
        $this->command->info('- 2 Vendors with primary supplier relationships');
        $this->command->info('- 1 BOM with 2 components');
    }
}
