<?php

namespace App\Settings;

class SettingsRegistry
{

    public static function categories(): array
    {
        return [
            'Units of Measurement' => 'Select either metric or incorrect units.',
            'FlightsGoneBy Settings' => 'Settings for Site Behaviour',
            'AI Generated Content'   => 'Airline tail logos and liveries are AI generated with human cleanup. If you would rather not see any AI, then our blank aircraft templates are human created.',
            'Account & Privacy'   => 'Everything to do with your account.',
            'Notifications'   => 'What you want to hear about (or not!)',
        ];
    }

    public static function schema(): array
    {
        return [
            [
                'key'     => 'distance_unit',
                'type'    => 'select',
                'label'   => 'Distance Units',
                'category' => 'FlightsGoneBy Settings',
                'default' => 'km',
                'options' => [
                    ['value' => 'km', 'label' => 'Kilometres (km)'],
                    ['value' => 'mi', 'label' => 'Miles (mi)'],
                    ['value' => 'nm', 'label' => 'Nautical miles (nm)'],
                ],
            ],
            [
                'category' => 'Account & Privacy',
                'key'     => 'profile_privacy',
                'type'    => 'select',
                'label'   => 'Account Privacy',
                'default' => 'public',
                'options' => [
                    ['value' => 'public', 'label' => 'Public Profile Viewable By Everyone'],
                    ['value' => 'private', 'label' => 'Private Profile Viewable Only By You and Your Approved Followers'],
                ]
            ],
            [
                'key'     => 'default_login_page',
                'type'    => 'select',
                'label'   => 'Default Page',
                'category' => 'FlightsGoneBy Settings',
                'default' => 'feed_first',
                'options' => [
                    ['value' => 'feed_first', 'label' => 'My Feed if Following People, My Profile if Not'],
                    ['value' => 'profile', 'label' => 'My Profile'],
                    ['value' => 'feed', 'label' => 'My Feed'],
                    ['value' => 'dashboard', 'label' => 'My Dashboard'],
                ],
            ],
            [
                'key'     => 'default_profile_view',
                'type'    => 'select',
                'label'   => 'Default View When Loading a Profile',
                'category' => 'FlightsGoneBy Settings',
                'default' => 'departure-board',
                'options' => [
                    ['value' => 'departure-board', 'label' => 'Departure Board'],
                    ['value' => 'map', 'label' => 'Map'],
                    ['value' => 'boarding-passes', 'label' => 'Boarding Passes'],
                    ['value' => 'achievements', 'label' => 'Achievements'],
                ],
            ],
            [
                'key'     => 'show_map_legend',
                'type'    => 'checkbox',
                'label'   => 'Expand Map Legend By Default',
                'category' => 'FlightsGoneBy Settings',
                'default' => true,
            ],
            [
                'key'     => 'hide_map_filters',
                'type'    => 'checkbox',
                'label'   => 'Hide Map Filters By Default',
                'category' => 'FlightsGoneBy Settings',
                'default' => true,
            ],
            [
                'key'     => 'hide_impossible_achievements',
                'type'    => 'checkbox',
                'label'   => 'Hide Impossible Achievements By Default',
                'category' => 'FlightsGoneBy Settings',
                'default' => true,
            ],
            [
                'category' => 'AI Generated Content',
                'key'     => 'ai_liveries',
                'type'    => 'checkbox',
                'label'   => 'Show AI Generated Livery Images',
                'default' => true,
            ],
            [
                'category' => 'AI Generated Content',
                'key'     => 'ai_tail_logos',
                'type'    => 'checkbox',
                'label'   => 'Show AI Generated Tail Logos',
                'default' => true,
            ],
            [
                'key'      => 'departure_board_columns',
                'category' => 'FlightsGoneBy Settings',
                'type'     => 'multiselect',
                'label'    => 'Which columns to show on the Departure Board',
                'default'  => ['airline', 'flight_number', 'departure_airport', 'arrival_airport', 'departure_date', 'departure_time', 'arrival_time', 'duration', 'distance', 'aircraft', 'registration', 'class_seat_combined'],
                'options'  => [
                    ['value' => 'airline', 'label' => 'Airline Logo w/ Info Popup'],
                    ['value' => 'airline.name', 'label' => 'Airline Name'],
                    ['value' => 'airline.iata_code', 'label' => 'Airline IATA Code'],
                    ['value' => 'airline.icao_code', 'label' => 'Airline ICAO Code'],
                    ['value' => 'flight_number', 'label' => 'Flight Number'],
                    ['value' => 'departure_airport', 'label' => 'From'],
                    ['value' => 'arrival_airport', 'label' => 'To'],
                    ['value' => 'departure_date', 'label' => 'Departure Date'],
                    ['value' => 'departure_time', 'label' => 'Departure Time'],
                    ['value' => 'arrival_time', 'label' => 'Arrival Time'],
                    ['value' => 'duration', 'label' => 'Duration'],
                    ['value' => 'distance', 'label' => 'Distance'],
                    ['value' => 'aircraft', 'label' => 'Aircraft Designator w/ Info Popup'],
                    ['value' => 'registration', 'label' => 'Aircraft Registration'],
                    ['value' => 'aircraft.manufacturer_code', 'label' => 'Aircraft Manufacturer'],
                    ['value' => 'aircraft.model_full_name', 'label' => 'Aircraft Model'],
                    ['value' => 'flight_class', 'label' => 'Class'],
                    ['value' => 'seat_type_id', 'label' => 'Seat Type'],
                    ['value' => 'seat_number', 'label' => 'Seat Number'],
                    ['value' => 'flight_reason_id', 'label' => 'Reason'],

                    ['value' => 'aircraft.display_name_short', 'label' => 'Aircraft Full Name'],
                    ['value' => 'class_seat_combined', 'label' => 'Class/Seat Type/Seat Number Combined'],
                ],
            ],
            [
                'category' => 'Notifications',
                'key'     => 'notify_on_new_follower',
                'type'    => 'checkbox',
                'label'   => 'Notify Me When I Receive a New Follower',
                'default' => true,
            ],
            [
                'category' => 'Notifications',
                'key'     => 'notify_on_historic_flight_added',
                'type'    => 'checkbox',
                'label'   => 'Notify Me When Someone I Follow Logs a Historic Flight',
                'default' => true,
            ],
            [
                'category' => 'Notifications',
                'key'     => 'notify_on_upcoming_flight_added',
                'type'    => 'checkbox',
                'label'   => 'Notify Me When Someone I Follow Logs an Upcoming Flight',
                'default' => true,
            ],
            [
                'category' => 'Notifications',
                'key'     => 'notify_on_flight_cancellation',
                'type'    => 'checkbox',
                'label'   => 'Notify Me When Someone I Follow Cancels an Upcoming Flight',
                'default' => false,
            ],
            [
                'category' => 'Notifications',
                'key'     => 'notify_on_flight_departure',
                'type'    => 'checkbox',
                'label'   => 'Notify Me When Someone I Follow Departs',
                'default' => true,
            ],
           [
                'category' => 'Notifications',
                'key'     => 'notify_on_flight_arrival',
                'type'    => 'checkbox',
                'label'   => 'Notify Me When Someone I Follow Arrives',
                'default' => true,
            ],
        ];
    }

    public static function defaults(): array
    {
        return collect(static::schema())
            ->pluck('default', 'key')
            ->toArray();
    }

    public static function validationRules(): array
    {
        $rules = [];
        foreach (static::schema() as $field) {
            $key = "settings.{$field['key']}";
            $rules[$key] = match ($field['type']) {
                'select'      => ['sometimes', 'required', 'string', 'in:' . implode(',', array_column($field['options'], 'value'))],
                'checkbox'    => ['sometimes', 'boolean'],
                'text'        => ['sometimes', 'nullable', 'string', 'max:255'],
                'multiselect' => ['sometimes', 'nullable', 'array'],
                default       => ['sometimes', 'nullable'],
            };

            if ($field['type'] === 'multiselect') {
                $rules["{$key}.*"] = ['string'];
            }
        }
        return $rules;
    }
}
