<?php

namespace Modules\SystemCore\Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Modules\SystemCore\App\Models\CoreProduct;
use Modules\SystemCore\App\Models\CoreProductCategory;
use Modules\SystemCore\App\Models\CoreTaxConfiguration;

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

        // Truncate tables
        DB::table('core_products')->truncate();

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

        $categories = CoreProductCategory::all();
        $taxes = CoreTaxConfiguration::all();
        $defaultTax = $taxes->first();

        $products = [
            [
                'name' => 'Premium Laptop',
                'sku' => 'LAP-PRE-001',
                'barcode' => '1234567890123',
                'category_id' => $categories->where('name', 'Electronics')->first()?->id,
                'description' => 'High-performance laptop with 16GB RAM and 512GB SSD',
                'brand' => 'TechBrand',
                'unit_of_measure' => 'pcs',
                'base_price' => 1200.00,
                'cost_price' => 900.00,
                'selling_price' => 1299.99,
                'tax_id' => $defaultTax?->id,
                'weight' => 2.5,
                'weight_unit' => 'kg',
                'min_stock_level' => 5,
                'max_stock_level' => 50,
                'reorder_level' => 10,
                'status' => 'active',
                'is_active' => true,
                'is_trackable' => true,
            ],
            [
                'name' => 'Wireless Mouse',
                'sku' => 'MOU-WIR-001',
                'barcode' => '2234567890123',
                'category_id' => $categories->where('name', 'Electronics')->first()?->id,
                'description' => 'Ergonomic wireless mouse with USB receiver',
                'brand' => 'TechBrand',
                'unit_of_measure' => 'pcs',
                'base_price' => 25.00,
                'cost_price' => 15.00,
                'selling_price' => 29.99,
                'tax_id' => $defaultTax?->id,
                'weight' => 0.15,
                'weight_unit' => 'kg',
                'min_stock_level' => 20,
                'max_stock_level' => 200,
                'reorder_level' => 30,
                'status' => 'active',
                'is_active' => true,
                'is_trackable' => true,
            ],
            [
                'name' => 'Office Chair',
                'sku' => 'FUR-CHA-001',
                'barcode' => '3234567890123',
                'category_id' => $categories->where('name', 'Furniture')->first()?->id,
                'description' => 'Ergonomic office chair with lumbar support',
                'brand' => 'ComfortSeating',
                'unit_of_measure' => 'pcs',
                'base_price' => 250.00,
                'cost_price' => 180.00,
                'selling_price' => 299.99,
                'tax_id' => $defaultTax?->id,
                'weight' => 15.0,
                'weight_unit' => 'kg',
                'min_stock_level' => 10,
                'max_stock_level' => 50,
                'reorder_level' => 15,
                'status' => 'active',
                'is_active' => true,
                'is_trackable' => true,
            ],
            [
                'name' => 'Notebook A4',
                'sku' => 'STA-NOT-001',
                'barcode' => '4234567890123',
                'category_id' => $categories->where('name', 'Stationery')->first()?->id,
                'description' => 'A4 size ruled notebook, 200 pages',
                'brand' => 'PaperPro',
                'unit_of_measure' => 'pcs',
                'base_price' => 3.00,
                'cost_price' => 1.50,
                'selling_price' => 4.99,
                'tax_id' => $defaultTax?->id,
                'weight' => 0.3,
                'weight_unit' => 'kg',
                'min_stock_level' => 100,
                'max_stock_level' => 1000,
                'reorder_level' => 150,
                'status' => 'active',
                'is_active' => true,
                'is_trackable' => true,
            ],
            [
                'name' => 'Desk Lamp LED',
                'sku' => 'ELE-LAM-001',
                'barcode' => '5234567890123',
                'category_id' => $categories->where('name', 'Electronics')->first()?->id,
                'description' => 'LED desk lamp with adjustable brightness',
                'brand' => 'LightPro',
                'unit_of_measure' => 'pcs',
                'base_price' => 35.00,
                'cost_price' => 22.00,
                'selling_price' => 44.99,
                'tax_id' => $defaultTax?->id,
                'weight' => 1.2,
                'weight_unit' => 'kg',
                'min_stock_level' => 15,
                'max_stock_level' => 100,
                'reorder_level' => 20,
                'status' => 'active',
                'is_active' => true,
                'is_trackable' => true,
            ],
        ];

        foreach ($products as $productData) {
            CoreProduct::create($productData);
        }

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