<?php

namespace App\Livewire;

use App\Models\Ride;
use Livewire\Component;

class RideDetailComponent extends Component
{
    /** @var Ride */
    public Ride $ride;

    /** @var bool */
    public bool $showCoasterStats = false;

    public function mount(Ride $ride): void
    {
        $this->ride = $ride->load([
            'park',
            'parkArea',
            'manufacturer',
            'designer',
            'rideModel',
            'coasterStats',
        ]);

        $this->showCoasterStats = $ride->category === 'RC' && $ride->coasterStats !== null;
    }

    public function toggleCoasterStats(): void
    {
        $this->showCoasterStats = !$this->showCoasterStats;
    }

    public function render()
    {
        return view('livewire.ride-detail');
    }

    /**
     * Get the opening year of the ride.
     */
    public function getOpeningYearAttribute(): ?string
    {
        return $this->ride->opening_date?->format('Y');
    }

    /**
     * Get a formatted date range for the ride's operation.
     */
    public function getOperatingPeriodAttribute(): string
    {
        $start = $this->ride->opening_date?->format('Y');
        $end = $this->ride->closing_date?->format('Y');

        if (!$start) {
            return 'Unknown dates';
        }

        return $end ? "{$start} - {$end}" : "{$start} - Present";
    }

    /**
     * Get the ride's status badge color classes.
     */
    public function getStatusColorClasses(): string
    {
        return match($this->ride->status) {
            'OPERATING' => 'bg-green-100 text-green-800',
            'CLOSED_TEMP' => 'bg-yellow-100 text-yellow-800',
            'SBNO' => 'bg-red-100 text-red-800',
            'CLOSING' => 'bg-orange-100 text-orange-800',
            'CLOSED_PERM' => 'bg-gray-100 text-gray-800',
            'UNDER_CONSTRUCTION' => 'bg-blue-100 text-blue-800',
            'DEMOLISHED' => 'bg-gray-100 text-gray-800',
            'RELOCATED' => 'bg-purple-100 text-purple-800',
            default => 'bg-gray-100 text-gray-800',
        };
    }

    /**
     * Format a measurement value with units.
     */
    public function formatMeasurement(?float $value, string $unit): string
    {
        return $value !== null ? number_format($value, 2) . ' ' . $unit : 'N/A';
    }
}