<?php

namespace Database\Seeders;

use App\Models\Car;
use App\Models\Branch;
use App\Models\Rental\VehicleInsurance;
use App\Models\Rental\InsuranceCompany;
use Illuminate\Database\Seeder;
use Carbon\Carbon;

class VehicleInsuranceSeeder extends Seeder
{
    public function run(): void
    {
        $cars = Car::take(6)->get();
        $companies = InsuranceCompany::all();

        if ($companies->isEmpty()) {
            return;
        }

        $insuranceData = [
            [
                'policy_number' => 'POL001',
                'insurance_type' => 'comprehensive',
                'coverage_amount' => 500000,
                'deductible_amount' => 2500,
                'start_date' => Carbon::now()->subMonths(3),
                'end_date' => Carbon::now()->addMonths(9),
                'premium_amount' => 5000,
                'status' => 'active',
            ],
            [
                'policy_number' => 'POL002',
                'insurance_type' => 'third_party',
                'coverage_amount' => 300000,
                'deductible_amount' => 5000,
                'start_date' => Carbon::now()->subMonths(6),
                'end_date' => Carbon::now()->addMonths(6),
                'premium_amount' => 2500,
                'status' => 'active',
            ],
            [
                'policy_number' => 'POL003',
                'insurance_type' => 'comprehensive',
                'coverage_amount' => 600000,
                'deductible_amount' => 3000,
                'start_date' => Carbon::now()->subMonths(2),
                'end_date' => Carbon::now()->addMonths(10),
                'premium_amount' => 6000,
                'status' => 'active',
            ],
            [
                'policy_number' => 'POL004',
                'insurance_type' => 'third_party',
                'coverage_amount' => 250000,
                'deductible_amount' => 5000,
                'start_date' => Carbon::now()->subMonths(4),
                'end_date' => Carbon::now()->addMonths(8),
                'premium_amount' => 2000,
                'status' => 'active',
            ],
            [
                'policy_number' => 'POL005',
                'insurance_type' => 'comprehensive',
                'coverage_amount' => 700000,
                'deductible_amount' => 2000,
                'start_date' => Carbon::now()->subMonths(1),
                'end_date' => Carbon::now()->addMonths(11),
                'premium_amount' => 7000,
                'status' => 'active',
            ],
            [
                'policy_number' => 'POL006',
                'insurance_type' => 'third_party',
                'coverage_amount' => 400000,
                'deductible_amount' => 4000,
                'start_date' => Carbon::now()->subMonths(5),
                'end_date' => Carbon::now()->addMonths(7),
                'premium_amount' => 3000,
                'status' => 'expired',
            ],
        ];

        $companyIndex = 0;
        foreach ($cars as $index => $car) {
            if (isset($insuranceData[$index])) {
                $data = $insuranceData[$index];
                VehicleInsurance::create([
                    'car_id' => $car->id,
                    'insurance_company_id' => $companies[$companyIndex % $companies->count()]->id,
                    'policy_number' => $data['policy_number'],
                    'insurance_type' => $data['insurance_type'],
                    'coverage_amount' => $data['coverage_amount'],
                    'deductible_amount' => $data['deductible_amount'],
                    'start_date' => $data['start_date'],
                    'end_date' => $data['end_date'],
                    'premium_amount' => $data['premium_amount'],
                    'payment_status' => 'paid',
                    'status' => $data['status'],
                ]);
                $companyIndex++;
            }
        }
    }
}
