<?php

namespace Modules\Vrc\Database\Seeders;

use Illuminate\Database\Seeder;
use Modules\Vrc\Entities\VrcInspectionTemplate;
use Modules\Vrc\Entities\VrcInspectionTemplateItem;

/**
 * Seeds a default multi-point inspection template per business (idempotent).
 * Phase 5.
 */
class VrcInspectionTemplateSeeder extends Seeder
{
    protected array $items = [
        ['Exterior', 'Body & paint condition', 'حالة الهيكل والطلاء'],
        ['Exterior', 'Lights & indicators', 'الإضاءة والإشارات'],
        ['Exterior', 'Tyres & tread depth', 'الإطارات وعمق المداس'],
        ['Brakes', 'Brake pads', 'تيل الفرامل'],
        ['Brakes', 'Brake fluid', 'زيت الفرامل'],
        ['Engine', 'Engine oil level', 'مستوى زيت المحرك'],
        ['Engine', 'Coolant level', 'مستوى سائل التبريد'],
        ['Engine', 'Battery health', 'حالة البطارية'],
        ['Interior', 'AC operation', 'عمل التكييف'],
        ['Interior', 'Wipers & washers', 'المساحات والرشاشات'],
    ];

    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) {
            $template = VrcInspectionTemplate::firstOrCreate(
                ['business_id' => $businessId, 'name' => 'General multi-point inspection'],
                ['name_ar' => 'فحص متعدد النقاط', 'is_active' => true]
            );

            if ($template->items()->count() === 0) {
                foreach ($this->items as $i => [$section, $label, $labelAr]) {
                    VrcInspectionTemplateItem::create([
                        'business_id' => $businessId,
                        'template_id' => $template->id,
                        'section'     => $section,
                        'label'       => $label,
                        'label_ar'    => $labelAr,
                        'input_type'  => 'status',
                        'sort_order'  => $i,
                    ]);
                }
            }
        }
    }
}
