<?php

namespace Modules\Vrc\Database\Seeders;

use Illuminate\Database\Seeder;
use Modules\Vrc\Entities\VrcVehicleMake;
use Modules\Vrc\Entities\VrcVehicleModel;

/**
 * Seeds common vehicle makes + a few popular models per business (idempotent).
 * MENA-weighted line-up. Phase 2.
 */
class VrcVehicleMakeSeeder extends Seeder
{
    /** make (en, ar) => [models...] */
    protected array $catalog = [
        'Toyota'     => ['ar' => 'تويوتا',     'models' => ['Corolla', 'Camry', 'Hilux', 'Land Cruiser', 'Yaris', 'Fortuner']],
        'Hyundai'    => ['ar' => 'هيونداي',    'models' => ['Elantra', 'Accent', 'Tucson', 'Sonata', 'Creta']],
        'Kia'        => ['ar' => 'كيا',         'models' => ['Cerato', 'Sportage', 'Rio', 'Sorento', 'Picanto']],
        'Nissan'     => ['ar' => 'نيسان',       'models' => ['Sunny', 'Sentra', 'Patrol', 'X-Trail', 'Qashqai']],
        'Mitsubishi' => ['ar' => 'ميتسوبيشي',  'models' => ['Lancer', 'Pajero', 'L200', 'Attrage']],
        'Chevrolet'  => ['ar' => 'شيفروليه',   'models' => ['Optra', 'Aveo', 'Captiva', 'Cruze']],
        'Volkswagen' => ['ar' => 'فولكس فاجن', 'models' => ['Passat', 'Golf', 'Tiguan', 'Polo']],
        'BMW'        => ['ar' => 'بي إم دبليو', 'models' => ['3 Series', '5 Series', 'X5', 'X3']],
        'Mercedes-Benz' => ['ar' => 'مرسيدس', 'models' => ['C-Class', 'E-Class', 'S-Class', 'GLC']],
        'Honda'      => ['ar' => 'هوندا',       'models' => ['Civic', 'Accord', 'CR-V', 'City']],
        'Ford'       => ['ar' => 'فورد',        'models' => ['Focus', 'Fiesta', 'Explorer', 'Ranger']],
        'Renault'    => ['ar' => 'رينو',        'models' => ['Logan', 'Megane', 'Duster', 'Sandero']],
    ];

    public function run(): void
    {
        $businessIds = \App\Business::pluck('id');
        if ($businessIds->isEmpty()) {
            $businessIds = collect([request()->session()->get('user.business_id')])->filter();
        }

        foreach ($businessIds as $businessId) {
            foreach ($this->catalog as $makeName => $info) {
                $make = VrcVehicleMake::firstOrCreate(
                    ['business_id' => $businessId, 'name' => $makeName],
                    ['name_ar' => $info['ar'], 'is_active' => true]
                );

                foreach ($info['models'] as $modelName) {
                    VrcVehicleModel::firstOrCreate(
                        ['make_id' => $make->id, 'name' => $modelName],
                        ['business_id' => $businessId, 'is_active' => true]
                    );
                }
            }
        }
    }
}
