package auth

import "strings"

const (
	ModuleUser                     = "user"
	ModuleRole                     = "role"
	ModuleHelicopter               = "helicopter"
	ModuleHelicopterUsage          = "helicopter_usage"
	ModuleReserveAC                = "reserve_ac"
	ModuleComplaint                = "complaint"
	ModuleActionSignoff            = "action_signoff"
	ModuleMCF                      = "mcf"
	ModuleEASARelease              = "easa_release"
	ModuleFlightInspection         = "flight_inspection"
	ModuleAfterFlight              = "after_flight"
	ModuleFlight                   = "flight"
	ModuleFlightPrepCheck          = "flight_prep_check"
	ModuleDutyRoster               = "duty_roster"
	ModuleDUL                      = "dul"
	ModuleHEMSOperation            = "hems_operation"
	ModuleHEMSBase                 = "hems_base"
	ModuleOperation                = "operation"
	ModuleOperationCategory        = "operation_category"
	ModuleForcesPresent            = "forces_present"
	ModuleAirRescuerChecklist      = "air_rescuer_checklist"
	ModuleMedicine                 = "medicine"
	ModulePatientData              = "patient_data"
	ModuleInsurancePatientData     = "insurance_patient_data"
	ModuleHealthInsuranceCompanies = "health_insurance_companies"
	ModuleVocation                 = "vocation"
	ModuleBase                     = "base"
	ModuleFacility                 = "facility"
	ModuleCountry                  = "country"
	ModuleFederalState             = "federal_state"
	ModuleICAO                     = "icao"
	ModuleNoICAOCode               = "no_icao_code"
	ModuleOPC                      = "opc"
	ModuleMasterSettings           = "master_settings"
	ModuleFileManager              = "file_manager"
	ModuleFilesystem               = "filesystem"
	ModuleAudit                    = "audit"
)

type ModuleInfo struct {
	Key   string
	Label string
}

var ModuleRegistry = []ModuleInfo{
	{ModuleUser, "User"},
	{ModuleRole, "Role"},
	{ModuleHelicopter, "Helicopter"},
	{ModuleHelicopterUsage, "Helicopter Usage"},
	{ModuleReserveAC, "Reserve AC"},
	{ModuleComplaint, "Complaint"},
	{ModuleActionSignoff, "Action Sign-off"},
	{ModuleMCF, "MCF"},
	{ModuleEASARelease, "EASA Release"},
	{ModuleFlightInspection, "Flight Inspection"},
	{ModuleAfterFlight, "After Flight Inspection"},
	{ModuleFlight, "Flight"},
	{ModuleFlightPrepCheck, "Flight Prep Check"},
	{ModuleDutyRoster, "Duty Roster"},
	{ModuleDUL, "DUL"},
	{ModuleHEMSOperation, "HEMS Operation"},
	{ModuleHEMSBase, "HEMS Base"},
	{ModuleOperation, "Operation"},
	{ModuleOperationCategory, "Operation Category"},
	{ModuleForcesPresent, "Forces Present"},
	{ModuleAirRescuerChecklist, "Air Rescuer Checklist"},
	{ModuleMedicine, "Medicine"},
	{ModulePatientData, "Patient Data"},
	{ModuleInsurancePatientData, "Insurance Patient Data"},
	{ModuleHealthInsuranceCompanies, "Health Insurance Companies"},
	{ModuleVocation, "Vocation"},
	{ModuleBase, "Base"},
	{ModuleFacility, "Facility"},
	{ModuleCountry, "Country"},
	{ModuleFederalState, "Federal State"},
	{ModuleICAO, "ICAO"},
	{ModuleNoICAOCode, "No ICAO Code"},
	{ModuleOPC, "OPC"},
	{ModuleMasterSettings, "Master Settings"},
	{ModuleFileManager, "File Manager"},
	{ModuleFilesystem, "Filesystem"},
	{ModuleAudit, "Audit"},
}

var moduleOrder = func() map[string]int {
	m := make(map[string]int, len(ModuleRegistry))
	for i := range ModuleRegistry {
		m[ModuleRegistry[i].Key] = i
	}
	return m
}()

// ModuleLabel returns the display label for a module key, falling back to the
// key itself when the module is not registered.
func ModuleLabel(key string) string {
	if i, ok := moduleOrder[key]; ok {
		return ModuleRegistry[i].Label
	}
	return key
}

// IsRegisteredModule reports whether key is a known module in ModuleRegistry.
func IsRegisteredModule(key string) bool {
	_, ok := moduleOrder[key]
	return ok
}

// ModuleSortIndex returns a module's position in ModuleRegistry, or a large
// value (sorting unknown modules last) when it is not registered.
func ModuleSortIndex(key string) int {
	if idx, ok := moduleOrder[key]; ok {
		return idx
	}
	return len(ModuleRegistry)
}

// ModuleForKey derives the module from a permission key by taking the segment
// before the first dot, e.g. "dul.create" -> "dul", "role.permission.read" -> "role".
func ModuleForKey(permissionKey string) string {
	if i := strings.Index(permissionKey, "."); i >= 0 {
		return permissionKey[:i]
	}
	return permissionKey
}
