<?php

namespace App\Notifications;

use App\Models\JobCard;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class WorkflowStatusNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(
        private JobCard $jobCard,
        private string $event
    ) {}

    public function via(object $notifiable): array
    {
        return ['mail', 'database'];
    }

    public function toMail(object $notifiable): MailMessage
    {
        $messages = $this->getEventMessages();
        
        return (new MailMessage)
            ->subject($messages['subject'])
            ->greeting("Hello {$notifiable->name},")
            ->line($messages['message'])
            ->line("**Job Number:** {$this->jobCard->job_number}")
            ->line("**Vehicle:** {$this->jobCard->vehicle->year} {$this->jobCard->vehicle->make} {$this->jobCard->vehicle->model}")
            ->line("**Customer:** {$this->jobCard->customer->name}")
            ->when($messages['action_url'], function ($mail) use ($messages) {
                return $mail->action($messages['action_text'], $messages['action_url']);
            })
            ->line('Thank you for your attention to this matter.');
    }

    public function toArray(object $notifiable): array
    {
        $messages = $this->getEventMessages();
        
        return [
            'job_card_id' => $this->jobCard->id,
            'job_number' => $this->jobCard->job_number,
            'event' => $this->event,
            'message' => $messages['message'],
            'customer_name' => $this->jobCard->customer->name,
            'vehicle_info' => "{$this->jobCard->vehicle->year} {$this->jobCard->vehicle->make} {$this->jobCard->vehicle->model}",
        ];
    }

    private function getEventMessages(): array
    {
        return match($this->event) {
            'job_assigned' => [
                'subject' => "Job Assignment - {$this->jobCard->job_number}",
                'message' => "A new job has been assigned to you for diagnosis and service coordination.",
                'action_text' => 'View Job Card',
                'action_url' => route('job-cards.show', $this->jobCard),
            ],
            'estimate_approved' => [
                'subject' => "Estimate Approved - {$this->jobCard->job_number}",
                'message' => "The customer has approved the repair estimate. You can now proceed with the work order creation.",
                'action_text' => 'View Estimate',
                'action_url' => route('estimates.index'),
            ],
            'parts_procurement_needed' => [
                'subject' => "Parts Procurement Required - {$this->jobCard->job_number}",
                'message' => "Parts procurement is required for this approved job. Please review the estimate and arrange for parts ordering.",
                'action_text' => 'View Parts Requirements',
                'action_url' => route('inventory.parts.index'),
            ],
            'work_order_created' => [
                'subject' => "Work Order Created - {$this->jobCard->job_number}",
                'message' => "A work order has been created and is ready for assignment to technicians.",
                'action_text' => 'View Work Order',
                'action_url' => route('work-orders.index'),
            ],
            'quality_inspection_required' => [
                'subject' => "Quality Inspection Required - {$this->jobCard->job_number}",
                'message' => "Work has been completed and the vehicle is ready for quality inspection.",
                'action_text' => 'Perform Inspection',
                'action_url' => route('inspections.index'),
            ],
            'vehicle_ready' => [
                'subject' => "Vehicle Ready for Pickup - {$this->jobCard->job_number}",
                'message' => "The vehicle has passed quality inspection and is ready for customer pickup.",
                'action_text' => 'View Job Details',
                'action_url' => route('job-cards.show', $this->jobCard),
            ],
            default => [
                'subject' => "Job Update - {$this->jobCard->job_number}",
                'message' => "There has been an update to this job card.",
                'action_text' => 'View Job Card',
                'action_url' => route('job-cards.show', $this->jobCard),
            ],
        };
    }
}