<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\Appointment;
use App\Models\Customer;
use App\Models\Vehicle;
use App\Models\Technician;
use Carbon\Carbon;

class AppointmentSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Get existing data
        $customers = Customer::limit(5)->get();
        $vehicles = Vehicle::limit(5)->get();
        $technicians = Technician::limit(5)->get();

        if ($customers->isEmpty() || $vehicles->isEmpty() || $technicians->isEmpty()) {
            $this->command->warn('Not enough seed data. Please run customer, vehicle, and technician seeders first.');
            return;
        }

        // Clear existing appointments
        Appointment::truncate();

        $appointments = [
            // Today's appointments
            [
                'customer_id' => $customers[0]->id,
                'vehicle_id' => $vehicles[0]->id,
                'assigned_technician_id' => $technicians[0]->id,
                'scheduled_datetime' => Carbon::today()->setTime(10, 0),
                'estimated_duration_minutes' => 60,
                'appointment_type' => 'maintenance',
                'service_requested' => 'Oil Change & Filter',
                'status' => 'confirmed',
                'customer_notes' => 'Regular maintenance appointment',
            ],
            [
                'customer_id' => $customers[1]->id,
                'vehicle_id' => $vehicles[1]->id,
                'assigned_technician_id' => $technicians[1]->id,
                'scheduled_datetime' => Carbon::today()->setTime(14, 30),
                'estimated_duration_minutes' => 120,
                'appointment_type' => 'repair',
                'service_requested' => 'Brake Inspection & Repair',
                'status' => 'scheduled',
                'customer_notes' => 'Customer reports squeaking brakes',
            ],
            
            // Tomorrow's appointments
            [
                'customer_id' => $customers[2]->id,
                'vehicle_id' => $vehicles[2]->id,
                'assigned_technician_id' => $technicians[2]->id,
                'scheduled_datetime' => Carbon::tomorrow()->setTime(9, 0),
                'estimated_duration_minutes' => 90,
                'appointment_type' => 'inspection',
                'service_requested' => 'Annual State Inspection',
                'status' => 'confirmed',
                'customer_notes' => 'Annual inspection due',
            ],
            [
                'customer_id' => $customers[0]->id,
                'vehicle_id' => $vehicles[3]->id,
                'assigned_technician_id' => $technicians[0]->id,
                'scheduled_datetime' => Carbon::tomorrow()->setTime(15, 0),
                'estimated_duration_minutes' => 180,
                'appointment_type' => 'repair',
                'service_requested' => 'Engine Diagnostic',
                'status' => 'scheduled',
                'customer_notes' => 'Check engine light is on',
            ],

            // This week
            [
                'customer_id' => $customers[3]->id,
                'vehicle_id' => $vehicles[4]->id,
                'assigned_technician_id' => $technicians[3]->id,
                'scheduled_datetime' => Carbon::now()->addDays(2)->setTime(11, 0),
                'estimated_duration_minutes' => 60,
                'appointment_type' => 'maintenance',
                'service_requested' => 'Tire Rotation',
                'status' => 'confirmed',
                'customer_notes' => 'Rotate tires and check alignment',
            ],
            [
                'customer_id' => $customers[4]->id,
                'vehicle_id' => $vehicles[0]->id,
                'assigned_technician_id' => $technicians[4]->id,
                'scheduled_datetime' => Carbon::now()->addDays(3)->setTime(13, 30),
                'estimated_duration_minutes' => 150,
                'appointment_type' => 'repair',
                'service_requested' => 'AC System Repair',
                'status' => 'scheduled',
                'customer_notes' => 'AC not cooling properly',
            ],

            // Next week
            [
                'customer_id' => $customers[1]->id,
                'vehicle_id' => $vehicles[2]->id,
                'assigned_technician_id' => $technicians[1]->id,
                'scheduled_datetime' => Carbon::now()->addWeek()->setTime(10, 30),
                'estimated_duration_minutes' => 90,
                'appointment_type' => 'maintenance',
                'service_requested' => 'Transmission Service',
                'status' => 'confirmed',
                'customer_notes' => 'Regular transmission fluid change',
            ],
            [
                'customer_id' => $customers[2]->id,
                'vehicle_id' => $vehicles[1]->id,
                'assigned_technician_id' => $technicians[2]->id,
                'scheduled_datetime' => Carbon::now()->addWeek()->addDay()->setTime(14, 0),
                'estimated_duration_minutes' => 120,
                'appointment_type' => 'estimate',
                'service_requested' => 'Body Work Estimate',
                'status' => 'scheduled',
                'customer_notes' => 'Minor fender damage estimate',
            ],
        ];

        foreach ($appointments as $appointmentData) {
            Appointment::create($appointmentData);
        }

        $this->command->info('Created ' . count($appointments) . ' sample appointments');
    }
}
