<?php

namespace App;

use App\Facades\Settings;

enum VehicleType: string
{
    case CAR = 'car';
    case LCV = 'lcv';
    case MOTORCYCLE = 'motorcycle';
    case CARAVAN = 'caravan';
    case MOTORHOME = 'motorhome';
    case CAMPERVAN = 'campervan';

    /**
     * Vehicle types enabled for the current tenant via the `enabled-vehicle-types`
     * setting, in the configured order, falling back to Car + LCV.
     *
     * @return array<int, self>
     */
    public static function enabled(): array
    {
        $defaults = [self::CAR, self::LCV];
        $defaultValues = array_map(fn (self $type) => $type->value, $defaults);

        $configured = Settings::get('enabled-vehicle-types', $defaultValues);
        if (!is_array($configured)) {
            $configured = $defaultValues;
        }

        $byValue = [];
        foreach (self::cases() as $case) {
            $byValue[$case->value] = $case;
        }

        $enabled = [];
        foreach ($configured as $value) {
            if (isset($byValue[$value])) {
                $enabled[] = $byValue[$value];
            }
        }

        return $enabled ?: $defaults;
    }
}
