<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Role;
use App\Models\Permission;
use App\Models\Category;
use App\Models\Product;
use App\Models\MasterStock;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        // 1. Create Permissions
        $permissions = [
            ['name' => 'Manage Inventory', 'slug' => 'manage-inventory'],
            ['name' => 'Manage Users', 'slug' => 'manage-users'],
            ['name' => 'Manage Orders', 'slug' => 'manage-orders'],
        ];

        foreach ($permissions as $perm) {
            Permission::firstOrCreate(['slug' => $perm['slug']], $perm);
        }

        // 2. Create Super Admin Role & Assign Permissions
        $adminRole = Role::firstOrCreate(
            ['slug' => 'super-admin'],
            ['name' => 'Super Admin']
        );
        $adminRole->permissions()->sync(Permission::pluck('id'));

        // 3. Create Admin User & Assign Role (Agar pehle se nahi hai toh)
        $admin = User::firstOrCreate(
            ['email' => 'admin@admin.com'],
            [
                'name' => 'Super Admin',
                'password' => Hash::make('password'),
                'phone' => '9876543210',
                'is_active' => true,
            ]
        );
        $admin->roles()->sync([$adminRole->id]);

        // 4. Create Categories
        $cat1 = Category::firstOrCreate(['slug' => 'tv-mainboards'], ['name' => 'TV Mainboards', 'appliance_type' => 'Television']);
        $cat2 = Category::firstOrCreate(['slug' => 'ac-compressors'], ['name' => 'AC Compressors', 'appliance_type' => 'Air Conditioner']);

        // 5. Create Products & Master Stocks
        // Product 1
        $prod1 = Product::firstOrCreate(
            ['part_code' => 'TCL-MB-55P615'],
            [
                'category_id' => $cat1->id,
                'name' => 'TCL 55-inch 4K Main Motherboard',
                'compatible_models' => 'TCL-55P615, TCL-55P715',
                'description' => 'Original mainboard for TCL 55-inch smart TVs.',
                'price' => 4500.00,
            ]
        );
        MasterStock::firstOrCreate(['product_id' => $prod1->id], [
            'available_quantity' => 15,
            'in_transit_quantity' => 2,
            'total_physical_quantity' => 17,
        ]);

        // Product 2
        $prod2 = Product::firstOrCreate(
            ['part_code' => 'LG-COMP-15TON'],
            [
                'category_id' => $cat2->id,
                'name' => 'LG 1.5 Ton Dual Inverter Compressor',
                'compatible_models' => 'LG-JS-Q18, LG-KS-Q18',
                'description' => 'Genuine replacement compressor for LG split ACs.',
                'price' => 8500.00,
            ]
        );
        MasterStock::firstOrCreate(['product_id' => $prod2->id], [
            'available_quantity' => 0, // Testing Out of Stock
            'in_transit_quantity' => 5,
            'total_physical_quantity' => 5,
        ]);
    }
}