import '../../tasks/domain/effort_level.dart';
import '../../tasks/domain/frequency.dart';

/// A bundled task template with German name, interval, and effort level.
class TaskTemplate {
  final String name;
  final String? description;
  final IntervalType intervalType;
  final int intervalDays;
  final EffortLevel effortLevel;

  const TaskTemplate({
    required this.name,
    this.description,
    required this.intervalType,
    this.intervalDays = 1,
    required this.effortLevel,
  });
}

/// German-language task templates for all 14 room types.
///
/// Each room type has 3-6 realistic household tasks with appropriate
/// frequencies and effort levels.
const Map<String, List<TaskTemplate>> roomTemplates = {
  'kueche': [
    TaskTemplate(
      name: 'Abspuelen',
      intervalType: IntervalType.daily,
      effortLevel: EffortLevel.low,
    ),
    TaskTemplate(
      name: 'Herd reinigen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Kuehlschrank reinigen',
      intervalType: IntervalType.monthly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Backofen reinigen',
      intervalType: IntervalType.monthly,
      effortLevel: EffortLevel.high,
    ),
    TaskTemplate(
      name: 'Muell rausbringen',
      intervalType: IntervalType.everyNDays,
      intervalDays: 2,
      effortLevel: EffortLevel.low,
    ),
  ],
  'badezimmer': [
    TaskTemplate(
      name: 'Toilette putzen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Spiegel reinigen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.low,
    ),
    TaskTemplate(
      name: 'Dusche reinigen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Waschbecken reinigen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.low,
    ),
    TaskTemplate(
      name: 'Badezimmerboden wischen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.medium,
    ),
  ],
  'schlafzimmer': [
    TaskTemplate(
      name: 'Bettwasche wechseln',
      intervalType: IntervalType.biweekly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Staubsaugen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Staub wischen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.low,
    ),
    TaskTemplate(
      name: 'Fenster putzen',
      intervalType: IntervalType.monthly,
      effortLevel: EffortLevel.medium,
    ),
  ],
  'wohnzimmer': [
    TaskTemplate(
      name: 'Staubsaugen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Staub wischen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.low,
    ),
    TaskTemplate(
      name: 'Kissen aufschuetteln',
      intervalType: IntervalType.daily,
      effortLevel: EffortLevel.low,
    ),
    TaskTemplate(
      name: 'Fenster putzen',
      intervalType: IntervalType.monthly,
      effortLevel: EffortLevel.medium,
    ),
  ],
  'flur': [
    TaskTemplate(
      name: 'Boden wischen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Schuhe aufraumen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.low,
    ),
    TaskTemplate(
      name: 'Garderobe aufraeumen',
      intervalType: IntervalType.monthly,
      effortLevel: EffortLevel.low,
    ),
  ],
  'buero': [
    TaskTemplate(
      name: 'Schreibtisch aufraeumen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.low,
    ),
    TaskTemplate(
      name: 'Staubsaugen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Bildschirm reinigen',
      intervalType: IntervalType.monthly,
      effortLevel: EffortLevel.low,
    ),
    TaskTemplate(
      name: 'Papierkorb leeren',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.low,
    ),
  ],
  'garage': [
    TaskTemplate(
      name: 'Boden fegen',
      intervalType: IntervalType.monthly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Aufraeumen',
      intervalType: IntervalType.quarterly,
      effortLevel: EffortLevel.high,
    ),
    TaskTemplate(
      name: 'Werkzeug sortieren',
      intervalType: IntervalType.quarterly,
      effortLevel: EffortLevel.medium,
    ),
  ],
  'balkon': [
    TaskTemplate(
      name: 'Boden fegen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.low,
    ),
    TaskTemplate(
      name: 'Pflanzen giessen',
      intervalType: IntervalType.everyNDays,
      intervalDays: 2,
      effortLevel: EffortLevel.low,
    ),
    TaskTemplate(
      name: 'Gelaender reinigen',
      intervalType: IntervalType.monthly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Moebel reinigen',
      intervalType: IntervalType.monthly,
      effortLevel: EffortLevel.medium,
    ),
  ],
  'waschkueche': [
    TaskTemplate(
      name: 'Waschmaschine reinigen',
      intervalType: IntervalType.monthly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Trockner reinigen',
      intervalType: IntervalType.monthly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Boden wischen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Waschmittel auffuellen',
      intervalType: IntervalType.monthly,
      effortLevel: EffortLevel.low,
    ),
  ],
  'keller': [
    TaskTemplate(
      name: 'Staubsaugen',
      intervalType: IntervalType.monthly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Aufraeumen',
      intervalType: IntervalType.quarterly,
      effortLevel: EffortLevel.high,
    ),
    TaskTemplate(
      name: 'Luftentfeuchter pruefen',
      intervalType: IntervalType.monthly,
      effortLevel: EffortLevel.low,
    ),
  ],
  'kinderzimmer': [
    TaskTemplate(
      name: 'Spielzeug aufraeumen',
      intervalType: IntervalType.daily,
      effortLevel: EffortLevel.low,
    ),
    TaskTemplate(
      name: 'Staubsaugen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Bettwasche wechseln',
      intervalType: IntervalType.biweekly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Staub wischen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.low,
    ),
  ],
  'gaestezimmer': [
    TaskTemplate(
      name: 'Staubsaugen',
      intervalType: IntervalType.biweekly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Bettwasche wechseln',
      intervalType: IntervalType.monthly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Staub wischen',
      intervalType: IntervalType.biweekly,
      effortLevel: EffortLevel.low,
    ),
    TaskTemplate(
      name: 'Lueften',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.low,
    ),
  ],
  'esszimmer': [
    TaskTemplate(
      name: 'Tisch abwischen',
      intervalType: IntervalType.daily,
      effortLevel: EffortLevel.low,
    ),
    TaskTemplate(
      name: 'Staubsaugen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Stuehle reinigen',
      intervalType: IntervalType.monthly,
      effortLevel: EffortLevel.medium,
    ),
  ],
  'garten': [
    TaskTemplate(
      name: 'Rasen maehen',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.high,
    ),
    TaskTemplate(
      name: 'Unkraut jaeten',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Hecke schneiden',
      intervalType: IntervalType.quarterly,
      effortLevel: EffortLevel.high,
    ),
    TaskTemplate(
      name: 'Laub harken',
      intervalType: IntervalType.weekly,
      effortLevel: EffortLevel.medium,
    ),
    TaskTemplate(
      name: 'Pflanzen giessen',
      intervalType: IntervalType.everyNDays,
      intervalDays: 2,
      effortLevel: EffortLevel.low,
    ),
  ],
};

/// Aliases for room type detection.
const Map<String, List<String>> _aliases = {
  'badezimmer': ['bad', 'wc', 'toilette'],
  'buero': ['arbeitszimmer', 'office'],
  'waschkueche': ['waschraum'],
  'garten': ['terrasse', 'aussenbereich', 'hof'],
  'gaestezimmer': ['gaeste'],
  'kinderzimmer': ['kinder'],
  'schlafzimmer': ['schlafraum'],
  'kueche': ['kitchen'],
  'esszimmer': ['essbereich'],
};

/// Detect a room type from a user-entered room name.
///
/// Performs case-insensitive matching against room type keys and their
/// aliases. Returns the room type key (e.g. "kueche") or null if no match.
String? detectRoomType(String roomName) {
  final lower = roomName.toLowerCase().trim();

  // Check direct room type keys first
  for (final type in roomTemplates.keys) {
    if (lower.contains(type)) {
      return type;
    }
  }

  // Check aliases
  for (final entry in _aliases.entries) {
    for (final alias in entry.value) {
      if (lower.contains(alias)) {
        return entry.key;
      }
    }
  }

  return null;
}
