<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Supplier;
use App\Models\Company;
use App\Models\Category;
use App\Models\Product;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        $this->createCategories();
        $this->createSampleSuppliers();
        $this->createSampleCompany();
    }

    private function createCategories(): void
    {
        $categories = [
            [
                'name' => 'PPE & Safety',
                'slug' => 'ppe-safety',
                'description' => 'Personal Protective Equipment and Safety Gear',
                'icon' => 'shield',
                'children' => [
                    ['name' => 'Safety Helmets', 'slug' => 'safety-helmets'],
                    ['name' => 'Safety Glasses', 'slug' => 'safety-glasses'],
                    ['name' => 'Gloves', 'slug' => 'gloves'],
                    ['name' => 'Safety Boots', 'slug' => 'safety-boots'],
                    ['name' => 'High Visibility Clothing', 'slug' => 'high-visibility-clothing'],
                    ['name' => 'Respirators & Masks', 'slug' => 'respirators-masks'],
                ],
            ],
            [
                'name' => 'Office Stationery',
                'slug' => 'office-stationery',
                'description' => 'Office supplies and stationery items',
                'icon' => 'pencil',
                'children' => [
                    ['name' => 'Pens & Pencils', 'slug' => 'pens-pencils'],
                    ['name' => 'Paper & Notebooks', 'slug' => 'paper-notebooks'],
                    ['name' => 'Filing & Storage', 'slug' => 'filing-storage'],
                    ['name' => 'Desk Accessories', 'slug' => 'desk-accessories'],
                    ['name' => 'Staplers & Punches', 'slug' => 'staplers-punches'],
                ],
            ],
            [
                'name' => 'Office Furniture',
                'slug' => 'office-furniture',
                'description' => 'Desks, chairs, and office furniture',
                'icon' => 'armchair',
                'children' => [
                    ['name' => 'Office Chairs', 'slug' => 'office-chairs'],
                    ['name' => 'Desks', 'slug' => 'desks'],
                    ['name' => 'Storage Cabinets', 'slug' => 'storage-cabinets'],
                    ['name' => 'Meeting Tables', 'slug' => 'meeting-tables'],
                ],
            ],
            [
                'name' => 'IT Equipment',
                'slug' => 'it-equipment',
                'description' => 'Computers, peripherals, and IT accessories',
                'icon' => 'laptop',
                'children' => [
                    ['name' => 'Laptops', 'slug' => 'laptops'],
                    ['name' => 'Monitors', 'slug' => 'monitors'],
                    ['name' => 'Keyboards & Mice', 'slug' => 'keyboards-mice'],
                    ['name' => 'Printers', 'slug' => 'printers'],
                    ['name' => 'Networking', 'slug' => 'networking'],
                ],
            ],
            [
                'name' => 'Cleaning Supplies',
                'slug' => 'cleaning-supplies',
                'description' => 'Cleaning products and janitorial supplies',
                'icon' => 'spray-can',
                'children' => [
                    ['name' => 'Cleaning Chemicals', 'slug' => 'cleaning-chemicals'],
                    ['name' => 'Cleaning Tools', 'slug' => 'cleaning-tools'],
                    ['name' => 'Paper Products', 'slug' => 'paper-products'],
                    ['name' => 'Waste Management', 'slug' => 'waste-management'],
                ],
            ],
            [
                'name' => 'Tools & Hardware',
                'slug' => 'tools-hardware',
                'description' => 'Hand tools, power tools, and hardware',
                'icon' => 'wrench',
                'children' => [
                    ['name' => 'Hand Tools', 'slug' => 'hand-tools'],
                    ['name' => 'Power Tools', 'slug' => 'power-tools'],
                    ['name' => 'Fasteners', 'slug' => 'fasteners'],
                    ['name' => 'Measuring Tools', 'slug' => 'measuring-tools'],
                ],
            ],
            [
                'name' => 'Electrical',
                'slug' => 'electrical',
                'description' => 'Electrical supplies and equipment',
                'icon' => 'zap',
                'children' => [
                    ['name' => 'Cables & Wiring', 'slug' => 'cables-wiring'],
                    ['name' => 'Switches & Sockets', 'slug' => 'switches-sockets'],
                    ['name' => 'Lighting', 'slug' => 'lighting'],
                    ['name' => 'Circuit Breakers', 'slug' => 'circuit-breakers'],
                ],
            ],
            [
                'name' => 'Packaging',
                'slug' => 'packaging',
                'description' => 'Packaging materials and supplies',
                'icon' => 'package',
                'children' => [
                    ['name' => 'Boxes & Cartons', 'slug' => 'boxes-cartons'],
                    ['name' => 'Tape & Adhesives', 'slug' => 'tape-adhesives'],
                    ['name' => 'Bubble Wrap & Foam', 'slug' => 'bubble-wrap-foam'],
                    ['name' => 'Strapping', 'slug' => 'strapping'],
                ],
            ],
        ];

        $order = 1;
        foreach ($categories as $categoryData) {
            $children = $categoryData['children'] ?? [];
            unset($categoryData['children']);
            
            $categoryData['display_order'] = $order++;
            $category = Category::create($categoryData);

            $childOrder = 1;
            foreach ($children as $childData) {
                $childData['parent_id'] = $category->id;
                $childData['display_order'] = $childOrder++;
                Category::create($childData);
            }
        }
    }

    private function createSampleSuppliers(): void
    {
        $suppliers = [
            [
                'user' => [
                    'name' => 'SafetyFirst Supplies',
                    'email' => 'supplier@safetyfirst.co.za',
                    'password' => Hash::make('password123'),
                    'user_type' => 'supplier',
                ],
                'supplier' => [
                    'company_name' => 'SafetyFirst Supplies (Pty) Ltd',
                    'registration_number' => '2020/123456/07',
                    'vat_number' => '4123456789',
                    'contact_name' => 'John Smith',
                    'contact_phone' => '+27 11 123 4567',
                    'contact_email' => 'sales@safetyfirst.co.za',
                    'address' => '123 Industrial Road',
                    'city' => 'Johannesburg',
                    'province' => 'Gauteng',
                    'postal_code' => '2000',
                    'description' => 'Leading supplier of PPE and safety equipment in South Africa',
                    'delivery_self' => true,
                    'delivery_platform' => true,
                    'delivery_collection' => true,
                    'registration_fee_paid' => true,
                    'is_verified' => true,
                    'is_featured' => true,
                    'rating' => 4.5,
                ],
                'products' => [
                    ['name' => 'Hard Hat - Yellow', 'category' => 'safety-helmets', 'price' => 89.99, 'compare_price' => 120.00, 'stock' => 500, 'featured' => true],
                    ['name' => 'Hard Hat - White', 'category' => 'safety-helmets', 'price' => 89.99, 'stock' => 350],
                    ['name' => 'Safety Glasses - Clear', 'category' => 'safety-glasses', 'price' => 45.00, 'stock' => 1000, 'featured' => true],
                    ['name' => 'Safety Glasses - Tinted', 'category' => 'safety-glasses', 'price' => 55.00, 'stock' => 800],
                    ['name' => 'Leather Work Gloves', 'category' => 'gloves', 'price' => 75.00, 'stock' => 600],
                    ['name' => 'Nitrile Gloves (Box of 100)', 'category' => 'gloves', 'price' => 189.00, 'compare_price' => 220.00, 'stock' => 400, 'featured' => true],
                    ['name' => 'Steel Toe Safety Boots', 'category' => 'safety-boots', 'price' => 599.00, 'compare_price' => 750.00, 'stock' => 200, 'featured' => true],
                    ['name' => 'Hi-Vis Vest - Orange', 'category' => 'high-visibility-clothing', 'price' => 65.00, 'stock' => 800],
                    ['name' => 'Hi-Vis Vest - Yellow', 'category' => 'high-visibility-clothing', 'price' => 65.00, 'stock' => 750],
                    ['name' => 'N95 Respirator Mask (Pack of 10)', 'category' => 'respirators-masks', 'price' => 149.00, 'stock' => 500],
                ],
            ],
            [
                'user' => [
                    'name' => 'Office Pro Supplies',
                    'email' => 'supplier@officepro.co.za',
                    'password' => Hash::make('password123'),
                    'user_type' => 'supplier',
                ],
                'supplier' => [
                    'company_name' => 'Office Pro Supplies',
                    'registration_number' => '2018/789012/07',
                    'vat_number' => '4987654321',
                    'contact_name' => 'Sarah Johnson',
                    'contact_phone' => '+27 21 987 6543',
                    'contact_email' => 'orders@officepro.co.za',
                    'address' => '456 Business Park',
                    'city' => 'Cape Town',
                    'province' => 'Western Cape',
                    'postal_code' => '8001',
                    'description' => 'Your one-stop shop for office supplies and stationery',
                    'delivery_self' => true,
                    'delivery_platform' => false,
                    'delivery_collection' => true,
                    'registration_fee_paid' => true,
                    'is_verified' => true,
                    'rating' => 4.2,
                ],
                'products' => [
                    ['name' => 'Ballpoint Pens (Pack of 50)', 'category' => 'pens-pencils', 'price' => 89.00, 'stock' => 1000, 'featured' => true],
                    ['name' => 'A4 Copy Paper (5 Reams)', 'category' => 'paper-notebooks', 'price' => 299.00, 'compare_price' => 350.00, 'stock' => 500, 'featured' => true],
                    ['name' => 'Lever Arch Files (Pack of 10)', 'category' => 'filing-storage', 'price' => 189.00, 'stock' => 300],
                    ['name' => 'Desk Organizer Set', 'category' => 'desk-accessories', 'price' => 149.00, 'stock' => 200],
                    ['name' => 'Heavy Duty Stapler', 'category' => 'staplers-punches', 'price' => 125.00, 'stock' => 400],
                    ['name' => 'Ergonomic Office Chair', 'category' => 'office-chairs', 'price' => 2499.00, 'compare_price' => 2999.00, 'stock' => 50, 'featured' => true],
                    ['name' => 'Executive Desk', 'category' => 'desks', 'price' => 4500.00, 'stock' => 20],
                    ['name' => '3-Drawer Filing Cabinet', 'category' => 'storage-cabinets', 'price' => 1899.00, 'stock' => 40],
                ],
            ],
            [
                'user' => [
                    'name' => 'TechWorld Solutions',
                    'email' => 'supplier@techworld.co.za',
                    'password' => Hash::make('password123'),
                    'user_type' => 'supplier',
                ],
                'supplier' => [
                    'company_name' => 'TechWorld Solutions (Pty) Ltd',
                    'registration_number' => '2019/456789/07',
                    'vat_number' => '4567891234',
                    'contact_name' => 'Mike Peters',
                    'contact_phone' => '+27 12 345 6789',
                    'contact_email' => 'sales@techworld.co.za',
                    'address' => '789 Tech Hub',
                    'city' => 'Pretoria',
                    'province' => 'Gauteng',
                    'postal_code' => '0001',
                    'description' => 'Premium IT equipment and technology solutions',
                    'delivery_self' => true,
                    'delivery_platform' => true,
                    'delivery_collection' => true,
                    'registration_fee_paid' => true,
                    'is_verified' => true,
                    'is_featured' => true,
                    'rating' => 4.7,
                ],
                'products' => [
                    ['name' => 'Business Laptop 15.6"', 'category' => 'laptops', 'price' => 12999.00, 'compare_price' => 14999.00, 'stock' => 30, 'featured' => true],
                    ['name' => '24" Full HD Monitor', 'category' => 'monitors', 'price' => 2499.00, 'stock' => 100, 'featured' => true],
                    ['name' => '27" 4K Monitor', 'category' => 'monitors', 'price' => 5999.00, 'stock' => 50],
                    ['name' => 'Wireless Keyboard & Mouse Combo', 'category' => 'keyboards-mice', 'price' => 599.00, 'stock' => 200],
                    ['name' => 'Multifunction Laser Printer', 'category' => 'printers', 'price' => 4999.00, 'compare_price' => 5999.00, 'stock' => 40, 'featured' => true],
                    ['name' => 'Gigabit Network Switch 8-Port', 'category' => 'networking', 'price' => 899.00, 'stock' => 80],
                ],
            ],
            [
                'user' => [
                    'name' => 'CleanCo Supplies',
                    'email' => 'supplier@cleanco.co.za',
                    'password' => Hash::make('password123'),
                    'user_type' => 'supplier',
                ],
                'supplier' => [
                    'company_name' => 'CleanCo Supplies',
                    'registration_number' => '2017/321654/07',
                    'vat_number' => '4321654987',
                    'contact_name' => 'Lisa Brown',
                    'contact_phone' => '+27 31 456 7890',
                    'contact_email' => 'orders@cleanco.co.za',
                    'address' => '321 Industrial Estate',
                    'city' => 'Durban',
                    'province' => 'KwaZulu-Natal',
                    'postal_code' => '4001',
                    'description' => 'Professional cleaning supplies and janitorial products',
                    'delivery_self' => true,
                    'delivery_platform' => false,
                    'delivery_collection' => true,
                    'registration_fee_paid' => true,
                    'is_verified' => true,
                    'rating' => 4.0,
                ],
                'products' => [
                    ['name' => 'All-Purpose Cleaner 5L', 'category' => 'cleaning-chemicals', 'price' => 89.00, 'stock' => 500, 'featured' => true],
                    ['name' => 'Floor Polish 5L', 'category' => 'cleaning-chemicals', 'price' => 149.00, 'stock' => 300],
                    ['name' => 'Industrial Mop & Bucket Set', 'category' => 'cleaning-tools', 'price' => 299.00, 'stock' => 150],
                    ['name' => 'Toilet Paper (48 Rolls)', 'category' => 'paper-products', 'price' => 189.00, 'compare_price' => 220.00, 'stock' => 400, 'featured' => true],
                    ['name' => 'Hand Towels (16 Packs)', 'category' => 'paper-products', 'price' => 249.00, 'stock' => 350],
                    ['name' => '120L Refuse Bags (50 Pack)', 'category' => 'waste-management', 'price' => 129.00, 'stock' => 600],
                ],
            ],
        ];

        foreach ($suppliers as $supplierData) {
            $user = User::create($supplierData['user']);
            
            $supplierInfo = $supplierData['supplier'];
            $supplierInfo['user_id'] = $user->id;
            $supplier = Supplier::create($supplierInfo);

            foreach ($supplierData['products'] as $productData) {
                $category = Category::where('slug', $productData['category'])->first();
                
                Product::create([
                    'supplier_id' => $supplier->id,
                    'category_id' => $category ? $category->id : null,
                    'name' => $productData['name'],
                    'description' => 'High quality ' . strtolower($productData['name']) . ' for professional use.',
                    'sku' => strtoupper(Str::random(8)),
                    'price' => $productData['price'],
                    'compare_price' => $productData['compare_price'] ?? null,
                    'stock_quantity' => $productData['stock'],
                    'min_order_quantity' => 1,
                    'unit' => 'each',
                    'is_featured' => $productData['featured'] ?? false,
                    'is_active' => true,
                    'rating' => rand(35, 50) / 10,
                    'total_reviews' => rand(5, 100),
                ]);
            }
        }
    }

    private function createSampleCompany(): void
    {
        $user = User::create([
            'name' => 'ABC Construction',
            'email' => 'buyer@abcconstruction.co.za',
            'password' => Hash::make('password123'),
            'user_type' => 'company',
        ]);

        Company::create([
            'user_id' => $user->id,
            'company_name' => 'ABC Construction (Pty) Ltd',
            'registration_number' => '2015/987654/07',
            'vat_number' => '4111222333',
            'contact_name' => 'David Wilson',
            'contact_phone' => '+27 11 555 1234',
            'contact_email' => 'procurement@abcconstruction.co.za',
            'billing_address' => '100 Construction Way, Sandton',
            'delivery_address' => '100 Construction Way, Sandton',
            'city' => 'Johannesburg',
            'province' => 'Gauteng',
            'postal_code' => '2196',
            'payment_terms_approved' => true,
            'credit_limit' => 100000.00,
            'is_verified' => true,
        ]);
    }
}
