<?php

namespace Database\Seeders;

use App\Models\Car;
use App\Models\Rental\MaintenanceRecord;
use Illuminate\Database\Seeder;
use Carbon\Carbon;

class MaintenanceSeeder extends Seeder
{
    public function run(): void
    {
        $cars = Car::take(5)->get();

        $maintenanceData = [
            [
                'description' => 'تغيير الزيت والفلاتر',
                'cost' => 250,
                'maintenance_date' => Carbon::now()->subDays(30),
                'next_due_km' => 5000,
                'next_due_date' => Carbon::now()->addMonths(6),
            ],
            [
                'description' => 'فحص وصيانة الفرامل',
                'cost' => 1200,
                'maintenance_date' => Carbon::now()->subDays(20),
                'next_due_km' => 20000,
                'next_due_date' => Carbon::now()->addMonths(12),
            ],
            [
                'description' => 'تغيير إطارات السيارة',
                'cost' => 3500,
                'maintenance_date' => Carbon::now()->subDays(15),
                'next_due_km' => 40000,
                'next_due_date' => Carbon::now()->addMonths(18),
            ],
            [
                'description' => 'فحص نظام التكييف وإعادة شحن',
                'cost' => 600,
                'maintenance_date' => Carbon::now()->subDays(10),
                'next_due_km' => 10000,
                'next_due_date' => Carbon::now()->addMonths(12),
            ],
            [
                'description' => 'فحص شامل وصيانة دورية',
                'cost' => 800,
                'maintenance_date' => Carbon::now()->subDays(5),
                'next_due_km' => 8000,
                'next_due_date' => Carbon::now()->addMonths(3),
            ],
            [
                'description' => 'تغيير بطارية السيارة',
                'cost' => 950,
                'maintenance_date' => Carbon::now()->subDays(25),
                'next_due_km' => 50000,
                'next_due_date' => Carbon::now()->addYears(3),
            ],
            [
                'description' => 'صيانة نظام التعليق',
                'cost' => 2500,
                'maintenance_date' => Carbon::now()->subDays(35),
                'next_due_km' => 30000,
                'next_due_date' => Carbon::now()->addMonths(12),
            ],
            [
                'description' => 'فحص وتصليح الإضاءة',
                'cost' => 400,
                'maintenance_date' => Carbon::now()->subDays(12),
                'next_due_km' => 5000,
                'next_due_date' => Carbon::now()->addMonths(6),
            ],
        ];

        $index = 0;
        foreach ($cars as $car) {
            for ($i = 0; $i < 3; $i++) {
                if (isset($maintenanceData[$index % count($maintenanceData)])) {
                    $data = $maintenanceData[$index % count($maintenanceData)];
                    MaintenanceRecord::create([
                        'branch_id' => $car->branch_id,
                        'car_id' => $car->id,
                        'description' => $data['description'],
                        'cost' => $data['cost'],
                        'maintenance_date' => $data['maintenance_date']->copy()->subDays($i * 15),
                        'next_due_km' => $data['next_due_km'],
                        'next_due_date' => $data['next_due_date']->copy()->addDays($i * 30),
                    ]);
                    $index++;
                }
            }
        }
    }
}
