<?php

namespace App\Livewire\Inspections;

use App\Models\JobCard;
use App\Models\VehicleInspection;
use Livewire\Component;
use Livewire\WithFileUploads;

class Create extends Component
{
    use WithFileUploads;

    public JobCard $jobCard;
    public $type; // 'incoming' or 'outgoing'
    
    public $current_mileage = '';
    public $fuel_level = '';
    public $overall_condition = '';
    public $recommendations = '';
    public $damage_notes = '';
    public $cleanliness_rating = 5;
    public $quality_rating = '';
    public $follow_up_required = false;
    public $notes = '';
    public $photos = [];
    public $videos = [];
    
    // Inspection checklist items
    public $checklist = [
        'exterior' => [
            'body_condition' => '',
            'paint_condition' => '',
            'lights_working' => '',
            'mirrors_intact' => '',
            'windshield_condition' => '',
        ],
        'interior' => [
            'seats_condition' => '',
            'dashboard_condition' => '',
            'electronics_working' => '',
            'upholstery_condition' => '',
        ],
        'mechanical' => [
            'engine_condition' => '',
            'transmission_condition' => '',
            'brakes_condition' => '',
            'suspension_condition' => '',
            'tires_condition' => '',
        ],
        'fluids' => [
            'oil_level' => '',
            'coolant_level' => '',
            'brake_fluid_level' => '',
            'power_steering_fluid' => '',
        ]
    ];

    protected $rules = [
        'current_mileage' => 'required|numeric|min:0',
        'fuel_level' => 'required|string',
        'overall_condition' => 'required|in:excellent,good,fair,poor,damaged',
        'cleanliness_rating' => 'required|integer|min:1|max:10',
    ];

    public function mount(JobCard $jobCard, $type)
    {
        $this->jobCard = $jobCard->load(['customer', 'vehicle']);
        $this->type = $type;
        $this->current_mileage = $jobCard->vehicle->current_mileage ?? '';
        
        if ($type === 'outgoing') {
            $this->rules['quality_rating'] = 'required|integer|min:1|max:10';
        }
    }

    public function save()
    {
        $this->validate();

        // Handle file uploads
        $photoUrls = [];
        foreach ($this->photos as $photo) {
            $photoUrls[] = $photo->store('inspections', 'public');
        }

        $videoUrls = [];
        foreach ($this->videos as $video) {
            $videoUrls[] = $video->store('inspections', 'public');
        }

        $inspection = VehicleInspection::create([
            'job_card_id' => $this->jobCard->id,
            'vehicle_id' => $this->jobCard->vehicle_id,
            'inspector_id' => auth()->id(),
            'inspection_type' => $this->type,
            'current_mileage' => $this->current_mileage,
            'fuel_level' => $this->fuel_level,
            'inspection_checklist' => $this->checklist,
            'photos' => $photoUrls,
            'videos' => $videoUrls,
            'overall_condition' => $this->overall_condition,
            'recommendations' => $this->recommendations,
            'damage_notes' => $this->damage_notes,
            'cleanliness_rating' => $this->cleanliness_rating,
            'quality_rating' => $this->quality_rating,
            'follow_up_required' => $this->follow_up_required,
            'notes' => $this->notes,
            'inspection_date' => now(),
        ]);

        // Update job card status based on inspection type
        if ($this->type === 'incoming') {
            $this->jobCard->update([
                'status' => 'inspection_completed',
                'mileage_in' => $this->current_mileage,
                'fuel_level_in' => $this->fuel_level,
            ]);
        } else {
            $this->jobCard->update([
                'status' => 'quality_check_completed',
                'mileage_out' => $this->current_mileage,
                'fuel_level_out' => $this->fuel_level,
            ]);
        }

        session()->flash('message', ucfirst($this->type) . ' inspection completed successfully!');
        return redirect()->route('inspections.show', $inspection);
    }

    public function render()
    {
        return view('livewire.inspections.create');
    }
}
