<?php

namespace Database\Seeders;

use App\Models\User;
use App\Models\Customer;
use App\Models\Vehicle;
use App\Models\Technician;
use App\Models\ServiceOrder;
use App\Models\Part;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        // Create admin user (only if it doesn't exist)
        User::firstOrCreate(
            ['email' => 'admin@carrepairs.com'],
            [
                'name' => 'Shop Manager',
                'email_verified_at' => now(),
                'password' => bcrypt('password'),
            ]
        );

        // Create additional users
        User::factory(3)->create();

        // Create technicians
        Technician::factory(8)->create();

        // Create customers with vehicles
        Customer::factory(50)
            ->has(Vehicle::factory()->count(rand(1, 3)))
            ->create();

        // Create common parts
        $commonParts = [
            ['name' => 'Engine Oil Filter', 'part_number' => 'OF-001', 'category' => 'engine', 'cost_price' => 12.50, 'sell_price' => 18.99],
            ['name' => 'Air Filter', 'part_number' => 'AF-001', 'category' => 'engine', 'cost_price' => 8.75, 'sell_price' => 14.99],
            ['name' => 'Brake Pads - Front', 'part_number' => 'BP-F001', 'category' => 'brakes', 'cost_price' => 45.00, 'sell_price' => 79.99],
            ['name' => 'Brake Pads - Rear', 'part_number' => 'BP-R001', 'category' => 'brakes', 'cost_price' => 38.00, 'sell_price' => 69.99],
            ['name' => 'Spark Plugs (Set of 4)', 'part_number' => 'SP-001', 'category' => 'engine', 'cost_price' => 25.00, 'sell_price' => 44.99],
            ['name' => 'Cabin Air Filter', 'part_number' => 'CAF-001', 'category' => 'hvac', 'cost_price' => 15.00, 'sell_price' => 24.99],
            ['name' => 'Windshield Wipers', 'part_number' => 'WW-001', 'category' => 'exterior', 'cost_price' => 12.00, 'sell_price' => 19.99],
            ['name' => 'Coolant (1 Gallon)', 'part_number' => 'CL-001', 'category' => 'cooling', 'cost_price' => 8.50, 'sell_price' => 16.99],
            ['name' => 'Transmission Fluid', 'part_number' => 'TF-001', 'category' => 'transmission', 'cost_price' => 22.00, 'sell_price' => 34.99],
            ['name' => 'Battery', 'part_number' => 'BAT-001', 'category' => 'electrical', 'cost_price' => 85.00, 'sell_price' => 149.99],
        ];

        foreach ($commonParts as $part) {
            Part::firstOrCreate(
                ['part_number' => $part['part_number']],
                array_merge($part, [
                    'description' => "High quality {$part['name']} for various vehicle makes and models",
                    'manufacturer' => 'OEM',
                    'quantity_on_hand' => rand(10, 100),
                    'minimum_stock_level' => 5,
                    'location' => 'A' . rand(1, 5) . '-' . rand(1, 10),
                    'status' => 'active',
                ])
            );
        }

        // Create some service orders with realistic data
        $vehicles = Vehicle::all();
        $technicians = Technician::where('status', 'active')->get();

        foreach ($vehicles->random(min(30, $vehicles->count())) as $vehicle) {
            ServiceOrder::factory()->create([
                'customer_id' => $vehicle->customer_id,
                'vehicle_id' => $vehicle->id,
                'assigned_technician_id' => $technicians->random()->id,
                'status' => collect(['pending', 'in_progress', 'completed', 'waiting_parts'])->random(),
            ]);
        }

        $this->command->info('Database seeded successfully with car repair shop data!');
    }
}
