<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    use WithoutModelEvents;

    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        // Create Admin User
        if (!\App\Models\User::where('email', 'admin@example.com')->exists()) {
            \App\Models\User::create([
                'name' => 'Admin User',
                'email' => 'admin@example.com',
                'password' => \Illuminate\Support\Facades\Hash::make('password'),
                'is_admin' => true,
            ]);
        }

        // Create Categories
        $categories = [
            'Electronics' => 'Gadgets and devices',
            'Clothing' => 'Apparel and more',
            'Door Lock' => 'Secure locks for your home',
            'Electrical' => 'Wires, switches, and components',
        ];

        foreach ($categories as $name => $description) {
            \App\Models\Category::firstOrCreate(
                ['slug' => \Illuminate\Support\Str::slug($name)],
                ['name' => $name, 'description' => $description]
            );
        }

        // Create Products
        $products = [
            ['category' => 'Electronics', 'name' => 'Smartphone', 'price' => 69999, 'stock' => 50],
            ['category' => 'Clothing', 'name' => 'T-Shirt', 'price' => 1999, 'stock' => 100],
            ['category' => 'Door Lock', 'name' => 'Smart Fingerprint Lock', 'price' => 8500, 'stock' => 20],
            ['category' => 'Door Lock', 'name' => 'Heavy Duty Deadbolt', 'price' => 2500, 'stock' => 30],
            ['category' => 'Electrical', 'name' => 'LED Ceiling Light', 'price' => 1200, 'stock' => 75],
            ['category' => 'Electrical', 'name' => 'Smart Wall Switch', 'price' => 950, 'stock' => 40],
        ];

        foreach ($products as $product) {
            $category = \App\Models\Category::where('name', $product['category'])->first();
            \App\Models\Product::firstOrCreate(
                ['slug' => \Illuminate\Support\Str::slug($product['name'])],
                [
                    'category_id' => $category->id,
                    'name' => $product['name'],
                    'description' => 'High quality ' . $product['name'],
                    'price' => $product['price'],
                    'stock' => $product['stock'],
                ]
            );
        }
    }
}
