import '../models/ayuel_response.dart';

/// Service for handling quick voice command shortcuts
class VoiceCommandsService {
  /// Detects if the transcript matches a quick command pattern
  VoiceCommand? detectCommand(String transcript) {
    final lowerTranscript = transcript.toLowerCase().trim();

    // Check each command pattern
    for (final command in _commands) {
      if (command.matches(lowerTranscript)) {
        return command;
      }
    }

    return null;
  }

  /// Get a user-friendly list of available commands for help
  List<String> getAvailableCommands() {
    return _commands.map((cmd) => cmd.description).toList();
  }

  /// All available voice commands
  static final List<VoiceCommand> _commands = [
    // Development / Testing
    VoiceCommand(
      id: 'test_nudge',
      patterns: [RegExp(r'test\s*nudge'), RegExp(r'simulate\s*nudge')],
      description: 'Test Nudge → Trigger proactive check-in',
      execute: (t) => AyuelResponse(
        category: 'Test',
        title: 'Testing',
        suggestion: 'Triggering...',
        why: 'Test',
        actions: [],
      ),
    ),

    // Daily Brief
    VoiceCommand(
      id: 'daily_brief',
      patterns: [
        RegExp(r'morning\s*brief'),
        RegExp(r'start\s*my\s*day'),
        RegExp(r'daily\s*brief'),
      ],
      description: 'Morning Brief → Daily summary & motivation',
      execute: (t) => AyuelResponse(
        category: 'Day',
        title: 'Morning Brief',
        suggestion: 'Gathering your daily insights...',
        why: 'Setting intention for the day.',
        actions: [],
      ),
    ),

    // Timer commands
    VoiceCommand(
      id: 'quick_break',
      patterns: [
        RegExp(r'^(quick|short)\s*(break|rest)$'),
        RegExp(r'^break\s*5$'),
        RegExp(r'^5\s*min(ute)?\s*break$'),
      ],
      description: 'Quick break → Start 5-minute break',
      execute: (transcript) => AyuelResponse(
        category: 'Time',
        title: 'Quick Break',
        suggestion: 'Starting a 5-minute break timer for you.',
        why: 'Short breaks help maintain focus and energy.',
        actions: [
          AyuelAction(label: 'Start 5m timer', type: 'timer'),
          AyuelAction(label: 'Cancel', type: 'cancel'),
        ],
      ),
    ),

    VoiceCommand(
      id: 'longer_break',
      patterns: [
        RegExp(r'^(long|longer)\s*break$'),
        RegExp(r'^break\s*15$'),
        RegExp(r'^15\s*min(ute)?\s*break$'),
      ],
      description: 'Long break → Start 15-minute break',
      execute: (transcript) => AyuelResponse(
        category: 'Time',
        title: 'Longer Break',
        suggestion: 'Starting a 15-minute break timer. Time to recharge!',
        why: 'Longer breaks help with deeper restoration.',
        actions: [
          AyuelAction(label: 'Start 15m timer', type: 'timer'),
          AyuelAction(label: 'Cancel', type: 'cancel'),
        ],
      ),
    ),

    VoiceCommand(
      id: 'pomodoro',
      patterns: [
        RegExp(r'^pomodoro$'),
        RegExp(r'^start\s*pomodoro$'),
        RegExp(r'^work\s*timer$'),
      ],
      description: 'Pomodoro → Start 25-minute focus timer',
      execute: (transcript) => AyuelResponse(
        category: 'Time',
        title: 'Pomodoro Timer',
        suggestion: 'Starting a 25-minute Pomodoro focus session.',
        why: 'The Pomodoro technique helps maintain deep focus.',
        actions: [
          AyuelAction(label: 'Start 25m timer', type: 'timer'),
          AyuelAction(label: 'Cancel', type: 'cancel'),
        ],
      ),
    ),

    // Food/Meal commands
    VoiceCommand(
      id: 'lunch_ideas',
      patterns: [
        RegExp(r'^lunch(\s*ideas?)?$'),
        RegExp(r'^what\s*(should|can)\s*i\s*eat(\s*for\s*lunch)?$'),
        RegExp(r'^food$'),
      ],
      description: 'Lunch → Quick lunch suggestions',
      execute: (transcript) => AyuelResponse(
        category: 'Food',
        title: 'Lunch Suggestions',
        suggestion:
            'For a balanced lunch, try a protein bowl with grains, veggies, and lean protein. Or a hearty salad with nuts and seeds.',
        why: 'Balanced meals maintain steady energy through the afternoon.',
        actions: [
          AyuelAction(label: 'More ideas', type: 'regenerate'),
          AyuelAction(label: 'Done', type: 'cancel'),
        ],
      ),
    ),

    VoiceCommand(
      id: 'quick_snack',
      patterns: [
        RegExp(r'^(quick\s*)?snack$'),
        RegExp(r'^hungry$'),
        RegExp(r'^something\s*to\s*eat$'),
      ],
      description: 'Snack → Healthy snack ideas',
      execute: (transcript) => AyuelResponse(
        category: 'Food',
        title: 'Quick Snack',
        suggestion:
            'Try nuts, fruit, yogurt, or a protein bar. Keep it light and nutritious.',
        why: 'Healthy snacks maintain energy without the crash.',
        actions: [
          AyuelAction(label: 'Other options', type: 'regenerate'),
          AyuelAction(label: 'Done', type: 'cancel'),
        ],
      ),
    ),

    // Energy/Status commands
    VoiceCommand(
      id: 'set_energy_low',
      patterns: [
        RegExp(r'^(set\s*)?energy\s*low$'),
        RegExp(r"^i'm\s*tired$"),
        RegExp(r'^im\s*tired$'),
        RegExp(r'^(feeling\s*)?low\s*energy$'),
      ],
      description: 'Energy low → Set energy to low',
      execute: (transcript) => AyuelResponse(
        category: 'General',
        title: 'Energy Level Set',
        suggestion:
            "Got it, setting your energy to Low. I'll keep suggestions gentle.",
        why: 'When energy is low, smaller steps work better.',
        actions: [AyuelAction(label: 'Continue', type: 'cancel')],
      ),
    ),

    VoiceCommand(
      id: 'set_energy_high',
      patterns: [
        RegExp(r'^(set\s*)?energy\s*high$'),
        RegExp(r"^i'm\s*energized$"),
        RegExp(r'^im\s*energized$'),
        RegExp(r'^(feeling\s*)?high\s*energy$'),
      ],
      description: 'Energy high → Set energy to high',
      execute: (transcript) => AyuelResponse(
        category: 'General',
        title: 'Energy Level Set',
        suggestion:
            "Awesome! Setting your energy to High. Let's make the most of it!",
        why: 'High energy is perfect for tackling bigger tasks.',
        actions: [AyuelAction(label: 'Continue', type: 'cancel')],
      ),
    ),

    // Self-care commands
    VoiceCommand(
      id: 'need_break',
      patterns: [
        RegExp(r'^(i\s*)?need\s*(a\s*)?break$'),
        RegExp(r'^overwhelmed$'),
        RegExp(r'^(too\s*)?stressed$'),
      ],
      description: 'Need break → Self-care suggestion',
      execute: (transcript) => AyuelResponse(
        category: 'Self-care',
        title: 'Take Care of Yourself',
        suggestion:
            'It sounds like you need a pause. Try: deep breathing for 2 minutes, a short walk, or step away from screens.',
        why: 'Recognizing when you need a break is important self-awareness.',
        actions: [
          AyuelAction(label: 'Start 5m break', type: 'timer'),
          AyuelAction(label: 'Other ideas', type: 'regenerate'),
        ],
      ),
    ),

    VoiceCommand(
      id: 'help',
      patterns: [
        RegExp(r'^help$'),
        RegExp(r'^what\s*can\s*you\s*do$'),
        RegExp(r'^commands?$'),
      ],
      description: 'Help → Show available commands',
      execute: (transcript) => AyuelResponse(
        category: 'General',
        title: 'Voice Commands',
        suggestion:
            'Try these quick commands:\n• "Quick break" - 5min timer\n• "Lunch" - meal suggestions\n• "Snack" - quick snack ideas\n• "Pomodoro" - 25min focus\n• "Energy low/high" - set energy\n• "Need break" - self-care suggestions',
        why: 'Quick commands make common tasks faster.',
        actions: [AyuelAction(label: 'Got it', type: 'cancel')],
      ),
    ),

    // Movement commands
    VoiceCommand(
      id: 'stretch',
      patterns: [
        RegExp(r'^stretch(ing)?$'),
        RegExp(r'^(need\s*to\s*)?move$'),
        RegExp(r'^exercise$'),
      ],
      description: 'Stretch → Quick movement suggestions',
      execute: (transcript) => AyuelResponse(
        category: 'Movement',
        title: 'Time to Move',
        suggestion:
            'Try: neck rolls, shoulder shrugs, standing and reaching for the ceiling, or a quick walk around.',
        why: 'Regular movement prevents stiffness and boosts energy.',
        actions: [
          AyuelAction(label: 'Start 5m break', type: 'timer'),
          AyuelAction(label: 'Done', type: 'cancel'),
        ],
      ),
    ),
  ];
}

/// Represents a voice command with patterns and execution logic
class VoiceCommand {
  final String id;
  final List<RegExp> patterns;
  final String description;
  final AyuelResponse Function(String transcript) execute;

  VoiceCommand({
    required this.id,
    required this.patterns,
    required this.description,
    required this.execute,
  });

  /// Check if the transcript matches any of this command's patterns
  bool matches(String transcript) {
    return patterns.any((pattern) => pattern.hasMatch(transcript));
  }
}
