<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Trip;
use App\Models\User;
use App\Models\TripRequest;
use Carbon\Carbon;

class TripSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // البحث عن السائق والراكب
        $driver = User::where('user_type', 'driver')->first();
        $passenger = User::where('user_type', 'passenger')->first();
        
        if (!$driver || !$passenger) {
            $this->command->info('لا يوجد سائق أو راكب، تم تخطي إنشاء الرحلات');
            return;
        }

        // البحث عن مركبة (إذا كانت موجودة)
        $vehicle = \App\Models\Vehicle::first();
        
        // إذا لم تكن هناك مركبات، قم بإنشاء مركبة افتراضية
        if (!$vehicle) {
            $this->command->info('لا توجد مركبات، يتم إنشاء مركبة افتراضية...');
            $vehicle = \App\Models\Vehicle::create([
                'plate_number' => 'افتراضي 0000',
                'brand' => 'تويوتا',
                'model' => 'كامري',
                'year' => 2020,
                'color' => 'أبيض',
                'vehicle_type' => 'sedan',
                'is_available' => true,
                'is_active' => true,
            ]);
            $this->command->info('تم إنشاء مركبة افتراضية: ' . $vehicle->plate_number);
        }

        // البحث عن طلب رحلة (إذا كان موجوداً)
        $tripRequest = TripRequest::first();

        $trips = [
            [
                'driver_id' => $driver->id,
                'passenger_id' => $passenger->id,
                'vehicle_id' => $vehicle->id,
                'request_id' => $tripRequest ? $tripRequest->id : null,
                'pickup_location' => 'Nassiriya General Hospital',
                'dropoff_location' => 'Central Market',
                'pickup_latitude' => 31.9550,
                'pickup_longitude' => 46.2580,
                'dropoff_latitude' => 31.9520,
                'dropoff_longitude' => 46.2560,
                'distance' => 2.5,
                'fare' => 5000,
                'status' => 'completed',
                'pickup_time' => Carbon::now()->subDays(2)->setTime(14, 30),
                'dropoff_time' => Carbon::now()->subDays(2)->setTime(14, 38),
                'notes' => 'رحلة سلسة، الراكب كان ودود',
                'driver_rating' => 4.8,
                'passenger_rating' => 4.9,
            ],
            [
                'driver_id' => $driver->id,
                'passenger_id' => $passenger->id,
                'vehicle_id' => $vehicle->id,
                'request_id' => null,
                'pickup_location' => 'Nassiriya University',
                'dropoff_location' => 'Gas Station Al-Rashid',
                'pickup_latitude' => 31.0500,
                'pickup_longitude' => 46.2600,
                'dropoff_latitude' => 31.9500,
                'dropoff_longitude' => 46.2550,
                'distance' => 3.2,
                'fare' => 6000,
                'status' => 'completed',
                'pickup_time' => Carbon::now()->subDays(1)->setTime(9, 0),
                'dropoff_time' => Carbon::now()->subDays(1)->setTime(9, 10),
                'notes' => 'من الجامعة إلى محطة البنزين',
                'driver_rating' => 4.5,
                'passenger_rating' => 4.8,
            ],
            [
                'driver_id' => $driver->id,
                'passenger_id' => $passenger->id,
                'vehicle_id' => $vehicle->id,
                'request_id' => null,
                'pickup_location' => 'Restaurant Al-Najaf',
                'dropoff_location' => 'Taxi Station',
                'pickup_latitude' => 31.9540,
                'pickup_longitude' => 46.2570,
                'dropoff_latitude' => 31.9530,
                'dropoff_longitude' => 46.2585,
                'distance' => 1.8,
                'fare' => 4000,
                'status' => 'active',
                'pickup_time' => Carbon::now()->subMinutes(30),
                'dropoff_time' => null,
                'notes' => 'رحلة قصيرة وسريعة',
                'driver_rating' => null,
                'passenger_rating' => null,
            ],
            [
                'driver_id' => $driver->id,
                'passenger_id' => $passenger->id,
                'vehicle_id' => $vehicle->id,
                'request_id' => null,
                'pickup_location' => 'Al-Shatra District',
                'dropoff_location' => 'Nassiriya City Center',
                'pickup_latitude' => 31.4000,
                'pickup_longitude' => 46.2000,
                'dropoff_latitude' => 31.9500,
                'dropoff_longitude' => 46.2580,
                'distance' => 8.2,
                'fare' => 12000,
                'status' => 'completed',
                'pickup_time' => Carbon::now()->subDays(5)->setTime(7, 0),
                'dropoff_time' => Carbon::now()->subDays(5)->setTime(7, 20),
                'notes' => 'رحلة طويلة من الشطرة، الراكب كان صبور',
                'driver_rating' => 5.0,
                'passenger_rating' => 5.0,
            ],
        ];

        foreach ($trips as $tripData) {
            Trip::create($tripData);
            $this->command->info('تم إنشاء رحلة: ' . $tripData['pickup_location'] . ' → ' . $tripData['dropoff_location'] . ' (' . $tripData['status'] . ')');
        }

        $this->command->info('تم إنشاء ' . count($trips) . ' رحلة بنجاح!');
    }
}
