<?php

namespace App\Livewire;

use App\Models\ParkArea;
use Livewire\Component;

class AreaStatisticsComponent extends Component
{
    public ParkArea $area;
    public bool $showDetails = false;
    public bool $showHistorical = false;

    public function mount(ParkArea $area): void
    {
        $this->area = $area;
    }

    public function toggleDetails(): void
    {
        $this->showDetails = !$this->showDetails;
    }

    public function toggleHistorical(): void
    {
        $this->showHistorical = !$this->showHistorical;
    }

    /**
     * Get the ride type distribution as percentages.
     *
     * @return array<string, float>
     */
    protected function getRidePercentages(): array
    {
        if ($this->area->ride_count === 0) {
            return [
                'coasters' => 0,
                'flat_rides' => 0,
                'water_rides' => 0,
            ];
        }

        return [
            'coasters' => round(($this->area->coaster_count / $this->area->ride_count) * 100, 1),
            'flat_rides' => round(($this->area->flat_ride_count / $this->area->ride_count) * 100, 1),
            'water_rides' => round(($this->area->water_ride_count / $this->area->ride_count) * 100, 1),
        ];
    }

    public function render()
    {
        return view('livewire.area-statistics-component', [
            'rideDistribution' => $this->area->ride_distribution,
            'ridePercentages' => $this->getRidePercentages(),
            'historicalStats' => $this->area->historical_stats,
        ]);
    }
}