package storage

// TransferRule represents a minimum connection time rule.
type TransferRule struct {
	RuleKey                string `json:"rule_key"`
	MinTransferTimeMinutes int    `json:"min_transfer_time_minutes"`
}

// TransferRuleMap is a lookup map for MCT values.
type TransferRuleMap map[string]int

// MinTransferTime returns the minimum connection time in seconds for a given rule key.
// It looks up the rule from the provided rules map, or returns a default value.
func MinTransferTime(ruleKey string, rules TransferRuleMap, defaultMCT int) int {
	// Try exact match first
	if minutes, ok := rules[ruleKey]; ok {
		return minutes * 60 // convert minutes to seconds
	}

	// Try base key matches (e.g., "airport_internal" matches "airport_internal_through")
	baseKey := ExtractBaseKey(ruleKey)
	if minutes, ok := rules[baseKey]; ok {
		return minutes * 60
	}

	// Return default MCT
	return defaultMCT
}

// ExtractBaseKey extracts the base rule key from a full rule key.
// e.g., "airport_internal_through" -> "airport_internal"
func ExtractBaseKey(ruleKey string) string {
	// Remove the suffix: through, separate, small, million_plus
	switch ruleKey {
	case "airport_internal_through", "airport_internal_separate":
		return "airport_internal"
	case "airport_to_city_small", "airport_to_city_million_plus":
		return "airport_to_city"
	default:
		return ruleKey
	}
}

// DefaultMCT is the default minimum connection time in seconds (30 minutes).
const DefaultMCT = 1800 // 30 minutes
