<?php

namespace App\Traits;

trait HasAreaStatistics
{
    /**
     * Get the total ride count including all types.
     */
    public function getTotalRideCountAttribute(): int
    {
        return $this->ride_count ?? 0;
    }

    /**
     * Get the percentage of coasters among all rides.
     */
    public function getCoasterPercentageAttribute(): float
    {
        if ($this->ride_count === 0) {
            return 0;
        }

        return round(($this->coaster_count / $this->ride_count) * 100, 1);
    }

    /**
     * Get a summary of ride types distribution.
     *
     * @return array<string, int>
     */
    public function getRideDistributionAttribute(): array
    {
        return [
            'coasters' => $this->coaster_count ?? 0,
            'flat_rides' => $this->flat_ride_count ?? 0,
            'water_rides' => $this->water_ride_count ?? 0,
        ];
    }

    /**
     * Get the formatted daily capacity.
     */
    public function getFormattedDailyCapacityAttribute(): string
    {
        if (!$this->daily_capacity) {
            return 'Unknown capacity';
        }

        return number_format($this->daily_capacity) . ' riders/day';
    }

    /**
     * Get the formatted peak wait time.
     */
    public function getFormattedPeakWaitTimeAttribute(): string
    {
        if (!$this->peak_wait_time) {
            return 'Unknown wait time';
        }

        return $this->peak_wait_time . ' minutes';
    }

    /**
     * Get the rating display with stars.
     */
    public function getRatingDisplayAttribute(): string
    {
        if (!$this->average_rating) {
            return 'Not rated';
        }

        $stars = str_repeat('★', floor($this->average_rating));
        $stars .= str_repeat('☆', 5 - floor($this->average_rating));

        return $stars . ' (' . number_format($this->average_rating, 1) . ')';
    }

    /**
     * Get historical statistics summary.
     *
     * @return array<string, mixed>
     */
    public function getHistoricalStatsAttribute(): array
    {
        return [
            'total_operated' => $this->total_rides_operated,
            'retired_count' => $this->retired_rides_count,
            'last_addition' => $this->last_new_ride_added?->format('M Y') ?? 'Never',
            'retirement_rate' => $this->getRetirementRate(),
        ];
    }

    /**
     * Calculate the retirement rate (retired rides as percentage of total operated).
     */
    protected function getRetirementRate(): float
    {
        if ($this->total_rides_operated === 0) {
            return 0;
        }

        return round(($this->retired_rides_count / $this->total_rides_operated) * 100, 1);
    }

    /**
     * Update ride counts.
     *
     * @param array<string, int> $counts
     */
    public function updateRideCounts(array $counts): void
    {
        $this->update([
            'ride_count' => $counts['total'] ?? 0,
            'coaster_count' => $counts['coasters'] ?? 0,
            'flat_ride_count' => $counts['flat_rides'] ?? 0,
            'water_ride_count' => $counts['water_rides'] ?? 0,
        ]);
    }

    /**
     * Update visitor statistics.
     */
    public function updateVisitorStats(int $dailyCapacity, int $peakWaitTime, float $rating): void
    {
        $this->update([
            'daily_capacity' => $dailyCapacity,
            'peak_wait_time' => $peakWaitTime,
            'average_rating' => round($rating, 2),
        ]);
    }

    /**
     * Record a new ride addition.
     */
    public function recordNewRide(): void
    {
        $this->increment('total_rides_operated');
        $this->update(['last_new_ride_added' => now()]);
    }

    /**
     * Record a ride retirement.
     */
    public function recordRetirement(): void
    {
        $this->increment('retired_rides_count');
    }

    /**
     * Reset all statistics to zero.
     */
    public function resetStatistics(): void
    {
        $this->update([
            'ride_count' => 0,
            'coaster_count' => 0,
            'flat_ride_count' => 0,
            'water_ride_count' => 0,
            'daily_capacity' => null,
            'peak_wait_time' => null,
            'average_rating' => null,
            'total_rides_operated' => 0,
            'retired_rides_count' => 0,
            'last_new_ride_added' => null,
        ]);
    }
}