<?php

namespace Modules\Vrc\Database\Seeders;

use Illuminate\Database\Seeder;
use Modules\Vrc\Entities\VrcBay;
use Modules\Vrc\Entities\VrcService;
use Modules\Vrc\Entities\VrcServiceCategory;

/**
 * Seeds a starter service catalog (categories + common labor operations) and
 * a few workshop bays per business (idempotent). Phase 3.
 */
class VrcCatalogSeeder extends Seeder
{
    protected array $catalog = [
        'Maintenance' => [
            'ar' => 'صيانة',
            'services' => [
                ['Oil & filter change', 'تغيير زيت وفلتر', 0.5, 250],
                ['Brake pad replacement', 'تغيير تيل الفرامل', 1.0, 400],
                ['Tyre rotation', 'تدوير الإطارات', 0.5, 100],
                ['AC service', 'صيانة التكييف', 1.5, 600],
            ],
        ],
        'Diagnostics' => [
            'ar' => 'تشخيص',
            'services' => [
                ['Engine diagnostic scan', 'فحص كمبيوتر المحرك', 0.5, 150],
                ['Electrical fault diagnosis', 'تشخيص أعطال كهربائية', 1.0, 200],
            ],
        ],
        'Repair' => [
            'ar' => 'إصلاح',
            'services' => [
                ['Battery replacement', 'تغيير البطارية', 0.3, 120],
                ['Suspension repair', 'إصلاح نظام التعليق', 2.0, 800],
            ],
        ],
    ];

    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 $catName => $info) {
                $cat = VrcServiceCategory::firstOrCreate(
                    ['business_id' => $businessId, 'name' => $catName],
                    ['name_ar' => $info['ar'], 'is_active' => true]
                );

                foreach ($info['services'] as [$name, $nameAr, $hours, $price]) {
                    VrcService::firstOrCreate(
                        ['business_id' => $businessId, 'name' => $name],
                        [
                            'category_id'         => $cat->id,
                            'name_ar'             => $nameAr,
                            'default_labor_hours' => $hours,
                            'default_price'       => $price,
                            'taxable'             => true,
                            'is_active'           => true,
                        ]
                    );
                }
            }

            foreach ([['Bay 1', 'lift'], ['Bay 2', 'bay'], ['Wash Bay', 'wash'], ['Diagnostic Bay', 'diagnostic']] as [$name, $type]) {
                VrcBay::firstOrCreate(
                    ['business_id' => $businessId, 'name' => $name],
                    ['type' => $type, 'is_active' => true]
                );
            }
        }
    }
}
