<?php

namespace App\Livewire\JobCards;

use App\Models\JobCard;
use App\Models\User;
use Livewire\Component;

class Show extends Component
{
    public JobCard $jobCard;

    public $availableTechnicians = [];

    public $selectedTechnicianId = null;

    public $showAssignmentModal = false;

    public function mount(JobCard $jobCard)
    {
        $this->jobCard = $jobCard->load([
            'customer',
            'vehicle',
            'serviceAdvisor',
            'branch',
            'incomingInspection',
            'outgoingInspection',
            'diagnosis',
            'estimates',
            'workOrders',
            'timesheets',
        ]);

        // Load available technicians for assignment
        $this->availableTechnicians = User::where('status', 'active')
            ->whereHas('roles', function ($query) {
                $query->whereIn('name', ['technician', 'service_coordinator']);
            })
            ->get(['id', 'name', 'email']);
    }

    public function assignForDiagnosis()
    {
        if (! $this->selectedTechnicianId) {
            session()->flash('error', 'Please select a technician for diagnosis assignment.');

            return;
        }

        if ($this->jobCard->status !== JobCard::STATUS_INSPECTED) {
            session()->flash('error', 'Job card must be inspected before assignment for diagnosis.');

            return;
        }

        try {
            $this->jobCard->update([
                'status' => JobCard::STATUS_ASSIGNED_FOR_DIAGNOSIS,
                'service_advisor_id' => $this->selectedTechnicianId,
            ]);

            $this->showAssignmentModal = false;
            $this->selectedTechnicianId = null;

            session()->flash('success', 'Job card assigned for diagnosis successfully.');

            // Refresh the job card data
            $this->jobCard->refresh();

        } catch (\Exception $e) {
            session()->flash('error', 'Failed to assign job card: '.$e->getMessage());
        }
    }

    public function startDiagnosis()
    {
        if ($this->jobCard->status !== JobCard::STATUS_ASSIGNED_FOR_DIAGNOSIS) {
            session()->flash('error', 'Job card must be assigned for diagnosis first.');

            return;
        }

        try {
            $this->jobCard->update([
                'status' => JobCard::STATUS_IN_DIAGNOSIS,
            ]);

            session()->flash('success', 'Diagnosis started. You can now create a diagnosis record.');

            // Refresh the job card data
            $this->jobCard->refresh();

            // Redirect to diagnosis creation
            return redirect()->route('diagnosis.create', $this->jobCard);

        } catch (\Exception $e) {
            session()->flash('error', 'Failed to start diagnosis: '.$e->getMessage());
        }
    }

    public function openAssignmentModal()
    {
        $this->showAssignmentModal = true;
    }

    public function closeAssignmentModal()
    {
        $this->showAssignmentModal = false;
        $this->selectedTechnicianId = null;
    }

    public function render()
    {
        return view('livewire.job-cards.show');
    }
}
