<?php

namespace App\Services\Achievements\Checkers;

use App\Models\User;
use App\Models\UserFlight;
use Illuminate\Database\Eloquent\Collection;

class GeneralFlyingChecker extends BaseChecker
{
    public function check(): void
    {
        /**
         * @var $flights Collection<int, UserFlight>
         */
        $flights = $this->flights();
        $count   = $flights->count();

        // --- Boolean achievements ---

        $this->awardIf($count >= 1, 'general_flying.first_flight');

        $this->awardIf(
            $flights->contains(fn ($f) => $f->isDomestic()),
            'general_flying.domestic_flight'
        );

        $this->awardIf(
            $flights->contains(fn ($f) => $f->isInternational()),
            'general_flying.international_flight'
        );

        $this->awardIf(
            $flights->contains(fn ($f) => $f->flightClass->internal_name === 'business'),
            'general_flying.business_class'
        );

        $this->awardIf(
            $flights->contains(fn ($f) =>  $f->flightClass->internal_name === 'first'),
            'general_flying.first_class'
        );

        $this->awardIf(
            $flights->contains(fn ($f) =>  $f->flightClass->internal_name === 'premium_economy'),
            'general_flying.premium_economy'
        );

        $this->awardIf(
            $flights->contains(fn ($f) => in_array($f->flightClass->internal_name, ['business', 'first'])),
            'general_flying.business_or_first'
        );

        $this->awardIf(
            $flights->contains(fn ($f) => $f->flightClass->internal_name === 'private'),
            'general_flying.fly_private'
        );

        $this->awardIf(
            $flights->contains(fn ($f) => $f->flightClass->internal_name === 'general_aviation'),
            'general_flying.general_aviation'
        );

        $this->awardIf(
            $flights->filter(fn (UserFlight $f) => $f->isDomestic())
                ->map(fn (UserFlight $f) => $f->departureAirport->region->country_id)
                ->unique()
                ->count() >= 2,
            'general_flying.domestic_two_countries'
        );

        // --- Progressive achievements ---

        $totalDistance = $flights->sum('distance');

        $this->awardProgress((int) $totalDistance, 'general_flying.circumference_of_the_earth');
        $this->awardProgress((int) $totalDistance, 'general_flying.to_the_moon');
        $this->awardProgress((int) $totalDistance, 'general_flying.gigametre');

        $this->awardProgress($count,'general_flying.10_flights');
        $this->awardProgress($count,'general_flying.50_flights');
        $this->awardProgress($count,'general_flying.100_flights');
        $this->awardProgress($count,'general_flying.500_flights');
        $this->awardProgress($count,'general_flying.1000_flights');
    }
}
