<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
use App\Models\Category;
use App\Models\Brand;
use App\Models\Product;
use App\Models\Setting;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        // 1. Seed Users
        $admin = User::firstOrCreate(
            ['email' => 'admin@textile.com'],
            [
                'name' => 'Super Admin',
                'first_name' => 'Super',
                'last_name' => 'Admin',
                'username' => 'superadmin',
                'phone' => '9876543210',
                'password' => Hash::make('password123'),
                'role' => 'super_admin',
                'status' => 'active',
            ]
        );

        $regionalManager = User::firstOrCreate(
            ['email' => 'regional@textile.com'],
            [
                'name' => 'Regional Manager',
                'first_name' => 'Regional',
                'last_name' => 'Supervisor',
                'username' => 'regional',
                'phone' => '9876543213',
                'password' => Hash::make('password123'),
                'role' => 'regional_manager',
                'status' => 'active',
            ]
        );

        $manager = User::firstOrCreate(
            ['email' => 'manager@textile.com'],
            [
                'name' => 'Store Manager',
                'first_name' => 'Store',
                'last_name' => 'Manager',
                'username' => 'manager',
                'phone' => '9876543211',
                'password' => Hash::make('password123'),
                'role' => 'store_manager',
                'status' => 'active',
            ]
        );

        $cashier = User::firstOrCreate(
            ['email' => 'cashier@textile.com'],
            [
                'name' => 'Rahul Sharma',
                'first_name' => 'Rahul',
                'last_name' => 'Sharma',
                'username' => 'cashier',
                'phone' => '9876543212',
                'password' => Hash::make('password123'),
                'role' => 'cashier',
                'status' => 'active',
            ]
        );

        // 2. Seed Men's Textile Categories
        $categoriesList = [
            'Shirts',
            'T-Shirts',
            'Jeans',
            'Trousers',
            'Shorts',
            'Hoodies',
            'Jackets',
            'Innerwear',
            'Belts',
            'Wallets',
            'Shoes',
        ];

        $categoryMap = [];
        foreach ($categoriesList as $catName) {
            $cat = Category::firstOrCreate(['name' => $catName], ['status' => 'active']);
            $categoryMap[$catName] = $cat->id;
        }

        // 3. Seed Textile Brands
        $brandsList = [
            'Raymond',
            'Peter England',
            'Levis',
            'Allen Solly',
            'Van Heusen',
            'Manyavar',
            'Nike',
            'Woodland',
            'Park Avenue',
        ];

        $brandMap = [];
        foreach ($brandsList as $bName) {
            $b = Brand::firstOrCreate(['name' => $bName], ['status' => 'active']);
            $brandMap[$bName] = $b->id;
        }

        // 4. Seed Products
        $productsData = [
            [
                'barcode' => '890123456701',
                'sku' => 'SHIRT-RAY-01',
                'name' => 'Raymond Premium Cotton Slim Fit Shirt',
                'category_id' => $categoryMap['Shirts'],
                'brand_id' => $brandMap['Raymond'],
                'size' => 'L',
                'color' => 'White',
                'cost_price' => 1200.00,
                'selling_price' => 1999.00,
                'gst_rate' => 18.00,
                'current_stock' => 45,
                'min_stock_alert' => 10,
                'image' => 'https://images.unsplash.com/photo-1602810318383-e386cc2a3ccf?w=500&auto=format&fit=crop&q=60',
            ],
            [
                'barcode' => '890123456702',
                'sku' => 'TSHIRT-LEV-02',
                'name' => "Levi's Classic Crewneck Graphic Tee",
                'category_id' => $categoryMap['T-Shirts'],
                'brand_id' => $brandMap['Levis'],
                'size' => 'M',
                'color' => 'Black',
                'cost_price' => 600.00,
                'selling_price' => 1299.00,
                'gst_rate' => 18.00,
                'current_stock' => 60,
                'min_stock_alert' => 15,
                'image' => 'https://images.unsplash.com/photo-1521572267360-ee0c2909d518?w=500&auto=format&fit=crop&q=60',
            ],
            [
                'barcode' => '890123456703',
                'sku' => 'JEAN-LEV-03',
                'name' => "Levi's 511 Slim Fit Stretchable Denim Jeans",
                'category_id' => $categoryMap['Jeans'],
                'brand_id' => $brandMap['Levis'],
                'size' => '32',
                'color' => 'Dark Indigo',
                'cost_price' => 1800.00,
                'selling_price' => 2999.00,
                'gst_rate' => 18.00,
                'current_stock' => 30,
                'min_stock_alert' => 8,
                'image' => 'https://images.unsplash.com/photo-1542272604-780c36856842?w=500&auto=format&fit=crop&q=60',
            ],
            [
                'barcode' => '890123456704',
                'sku' => 'TROUSER-ALL-04',
                'name' => 'Allen Solly Formal Chino Trousers',
                'category_id' => $categoryMap['Trousers'],
                'brand_id' => $brandMap['Allen Solly'],
                'size' => '34',
                'color' => 'Khaki Beige',
                'cost_price' => 1400.00,
                'selling_price' => 2299.00,
                'gst_rate' => 18.00,
                'current_stock' => 25,
                'min_stock_alert' => 5,
                'image' => 'https://images.unsplash.com/photo-1473966968600-fa801b869a1a?w=500&auto=format&fit=crop&q=60',
            ],
            [
                'barcode' => '890123456705',
                'sku' => 'JACKET-WOOD-05',
                'name' => 'Woodland Rugged Leather Outdoor Jacket',
                'category_id' => $categoryMap['Jackets'],
                'brand_id' => $brandMap['Woodland'],
                'size' => 'XL',
                'color' => 'Brown',
                'cost_price' => 3500.00,
                'selling_price' => 5999.00,
                'gst_rate' => 18.00,
                'current_stock' => 12,
                'min_stock_alert' => 3,
                'image' => 'https://images.unsplash.com/photo-1551028719-00167b16eac5?w=500&auto=format&fit=crop&q=60',
            ],
            [
                'barcode' => '890123456706',
                'sku' => 'SHOE-NIKE-06',
                'name' => 'Nike Revolution 6 Running Shoes',
                'category_id' => $categoryMap['Shoes'],
                'brand_id' => $brandMap['Nike'],
                'size' => '42',
                'color' => 'Grey / Red',
                'cost_price' => 2200.00,
                'selling_price' => 3695.00,
                'gst_rate' => 18.00,
                'current_stock' => 20,
                'min_stock_alert' => 5,
                'image' => 'https://images.unsplash.com/photo-1542291026-7eec264c27ff?w=500&auto=format&fit=crop&q=60',
            ],
            [
                'barcode' => '890123456707',
                'sku' => 'BELT-PARK-07',
                'name' => 'Park Avenue Genuine Italian Leather Belt',
                'category_id' => $categoryMap['Belts'],
                'brand_id' => $brandMap['Park Avenue'],
                'size' => 'Free Size',
                'color' => 'Black',
                'cost_price' => 450.00,
                'selling_price' => 899.00,
                'gst_rate' => 18.00,
                'current_stock' => 50,
                'min_stock_alert' => 10,
                'image' => 'https://images.unsplash.com/photo-1624222247344-550fb60583dc?w=500&auto=format&fit=crop&q=60',
            ],
            [
                'barcode' => '890123456708',
                'sku' => 'HOODIE-PETER-08',
                'name' => 'Peter England Fleece Warm Hoodie',
                'category_id' => $categoryMap['Hoodies'],
                'brand_id' => $brandMap['Peter England'],
                'size' => 'L',
                'color' => 'Navy Blue',
                'cost_price' => 1100.00,
                'selling_price' => 1899.00,
                'gst_rate' => 18.00,
                'current_stock' => 18,
                'min_stock_alert' => 4,
                'image' => 'https://images.unsplash.com/photo-1556905055-8f358a7a47b2?w=500&auto=format&fit=crop&q=60',
            ]
        ];

        foreach ($productsData as $pData) {
            Product::firstOrCreate(['barcode' => $pData['barcode']], $pData);
        }

        // 5. Seed Settings
        $settingsData = [
            'company_name' => "Dreamway Men's Textile & Fashion",
            'company_logo' => 'https://images.unsplash.com/photo-1558769132-cb1aea458c5e?w=120&auto=format&fit=crop&q=60',
            'gst_number' => '33AAACD1234E1Z5',
            'phone' => '+91 98765 43210',
            'address' => '123 Commercial Street, Textile Hub, Chennai, TN',
            'invoice_prefix' => 'TXT-',
            'receipt_footer' => 'Thank you for shopping with Dreamway Textile! Items exchanged within 7 days with original tag.',
        ];

        foreach ($settingsData as $key => $val) {
            Setting::updateOrCreate(['key' => $key], ['value' => $val]);
        }
    }
}
