<?php

namespace App\Livewire\CustomerPortal;

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

class WorkflowProgress extends Component
{
    public JobCard $jobCard;

    public array $progressSteps = [];

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

        $this->loadProgressSteps();
    }

    public function loadProgressSteps()
    {
        $currentStatus = $this->jobCard->status;

        $this->progressSteps = [
            [
                'step' => 1,
                'title' => 'Vehicle Reception',
                'description' => 'Your vehicle has been received and logged into our system',
                'status' => $this->getStepStatus('received', $currentStatus),
                'completed_at' => $this->jobCard->arrival_datetime,
                'icon' => 'truck',
                'details' => [
                    'Arrival Time' => $this->jobCard->arrival_datetime?->format('M j, Y g:i A'),
                    'Mileage' => number_format($this->jobCard->mileage_in).' miles',
                    'Fuel Level' => $this->jobCard->fuel_level_in.'%',
                    'Service Advisor' => $this->jobCard->serviceAdvisor?->name,
                ],
            ],
            [
                'step' => 2,
                'title' => 'Initial Inspection',
                'description' => 'Comprehensive vehicle inspection to document current condition',
                'status' => $this->getStepStatus('inspected', $currentStatus),
                'completed_at' => $this->jobCard->incomingInspection?->inspection_date,
                'icon' => 'clipboard-document-check',
                'details' => [
                    'Inspector' => $this->jobCard->incomingInspection?->inspector?->name,
                    'Overall Condition' => $this->jobCard->incomingInspection?->overall_condition,
                    'Inspection Date' => $this->jobCard->incomingInspection?->inspection_date?->format('M j, Y g:i A'),
                ],
            ],
            [
                'step' => 3,
                'title' => 'Service Assignment',
                'description' => 'Vehicle assigned to qualified service coordinator',
                'status' => $this->getStepStatus('assigned_for_diagnosis', $currentStatus),
                'completed_at' => $this->jobCard->diagnosis?->created_at,
                'icon' => 'user-group',
                'details' => [
                    'Service Coordinator' => $this->jobCard->diagnosis?->serviceCoordinator?->name,
                    'Assignment Date' => $this->jobCard->diagnosis?->created_at?->format('M j, Y g:i A'),
                ],
            ],
            [
                'step' => 4,
                'title' => 'Diagnosis',
                'description' => 'Comprehensive diagnostic assessment of reported issues',
                'status' => $this->getStepStatus('in_diagnosis', $currentStatus),
                'completed_at' => $this->jobCard->diagnosis?->completed_at,
                'icon' => 'wrench-screwdriver',
                'details' => [
                    'Diagnosis Status' => ucfirst(str_replace('_', ' ', $this->jobCard->diagnosis?->diagnosis_status ?? '')),
                    'Started' => $this->jobCard->diagnosis?->started_at?->format('M j, Y g:i A'),
                    'Completed' => $this->jobCard->diagnosis?->completed_at?->format('M j, Y g:i A'),
                ],
            ],
            [
                'step' => 5,
                'title' => 'Estimate Provided',
                'description' => 'Detailed repair estimate prepared and sent for approval',
                'status' => $this->getStepStatus('estimate_sent', $currentStatus),
                'completed_at' => $this->jobCard->estimates->where('status', 'sent')->first()?->created_at,
                'icon' => 'document-text',
                'details' => [
                    'Estimate Total' => '$'.number_format($this->jobCard->estimates->where('status', 'sent')->first()?->total_amount ?? 0, 2),
                    'Valid Until' => $this->jobCard->estimates->where('status', 'sent')->first()?->created_at?->addDays($this->jobCard->estimates->where('status', 'sent')->first()?->validity_period_days ?? 30)?->format('M j, Y'),
                ],
            ],
            [
                'step' => 6,
                'title' => 'Work Approved',
                'description' => 'Estimate approved, work authorization received',
                'status' => $this->getStepStatus('approved', $currentStatus),
                'completed_at' => $this->jobCard->estimates->where('status', 'approved')->first()?->customer_approved_at,
                'icon' => 'check-circle',
                'details' => [
                    'Approved At' => $this->jobCard->estimates->where('status', 'approved')->first()?->customer_approved_at?->format('M j, Y g:i A'),
                    'Approval Method' => ucfirst($this->jobCard->estimates->where('status', 'approved')->first()?->customer_approval_method ?? ''),
                ],
            ],
            [
                'step' => 7,
                'title' => 'Parts Procurement',
                'description' => 'Required parts sourced and prepared',
                'status' => $this->getStepStatus('parts_procurement', $currentStatus),
                'completed_at' => null, // Would need to track this separately
                'icon' => 'cog-6-tooth',
                'details' => [],
            ],
            [
                'step' => 8,
                'title' => 'Work in Progress',
                'description' => 'Repairs and services being performed by certified technicians',
                'status' => $this->getStepStatus('in_progress', $currentStatus),
                'completed_at' => $this->jobCard->workOrders->first()?->actual_start_time,
                'icon' => 'wrench',
                'details' => [
                    'Started' => $this->jobCard->workOrders->first()?->actual_start_time?->format('M j, Y g:i A'),
                    'Technician' => $this->jobCard->workOrders->first()?->assignedTechnician?->name,
                ],
            ],
            [
                'step' => 9,
                'title' => 'Quality Inspection',
                'description' => 'Final quality check and outgoing inspection',
                'status' => $this->getStepStatus('completed', $currentStatus),
                'completed_at' => $this->jobCard->outgoingInspection?->inspection_date,
                'icon' => 'shield-check',
                'details' => [
                    'Inspector' => $this->jobCard->outgoingInspection?->inspector?->name,
                    'Inspection Date' => $this->jobCard->outgoingInspection?->inspection_date?->format('M j, Y g:i A'),
                ],
            ],
            [
                'step' => 10,
                'title' => 'Ready for Pickup',
                'description' => 'Vehicle completed and ready for customer pickup',
                'status' => $this->getStepStatus('completed', $currentStatus),
                'completed_at' => $this->jobCard->completion_datetime,
                'icon' => 'truck',
                'details' => [
                    'Completion Time' => $this->jobCard->completion_datetime?->format('M j, Y g:i A'),
                    'Final Mileage' => number_format($this->jobCard->mileage_out).' miles',
                ],
            ],
        ];
    }

    private function getStepStatus(string $stepStatus, string $currentStatus): string
    {
        $statusOrder = [
            'received' => 1,
            'inspected' => 2,
            'assigned_for_diagnosis' => 3,
            'in_diagnosis' => 4,
            'estimate_sent' => 5,
            'approved' => 6,
            'parts_procurement' => 7,
            'in_progress' => 8,
            'completed' => 9,
            'delivered' => 10,
        ];

        $stepOrder = $statusOrder[$stepStatus] ?? 999;
        $currentOrder = $statusOrder[$currentStatus] ?? 0;

        if ($currentOrder >= $stepOrder) {
            return 'completed';
        } elseif ($currentOrder == $stepOrder - 1) {
            return 'current';
        } else {
            return 'pending';
        }
    }

    public function render()
    {
        return view('livewire.customer-portal.workflow-progress');
    }
}
