import 'dart:math';
import 'package:uuid/uuid.dart';
import '../models/cycle_entry.dart';
import '../models/user_profile.dart';

class MockDataService {
  final Random _random = Random();
  final Uuid _uuid = const Uuid();

  UserProfile generateMockWifeProfile() {
    return UserProfile(
      id: _uuid.v4(),
      name: 'Sarah',
      relationshipStatus: RelationshipStatus.married,
      averageCycleLength: 29,
      averagePeriodLength: 5,
      lastPeriodStartDate: DateTime.now().subtract(const Duration(days: 10)),
      favoriteFoods: ['Chocolate', 'Ice Cream', 'Berries'],
      isDataShared: true,
      createdAt: DateTime.now(),
      updatedAt: DateTime.now(),
    );
  }

  List<CycleEntry> generateMockCycleEntries({
    int days = 90,
    int cycleLength = 28,
    int periodLength = 5,
  }) {
    final List<CycleEntry> entries = [];
    final DateTime today = DateTime.now();

    for (int i = 0; i < days; i++) {
      final DateTime date = today.subtract(Duration(days: i));
      final int dayOfCycle = (cycleLength - (i % cycleLength)) % cycleLength;

      bool isPeriodDay = dayOfCycle < periodLength;
      FlowIntensity? flow;
      if (isPeriodDay) {
        if (dayOfCycle < 2) {
          flow = FlowIntensity.heavy;
        } else if (dayOfCycle < 4) {
          flow = FlowIntensity.medium;
        } else {
          flow = FlowIntensity.light;
        }
      }

      final entry = CycleEntry(
        id: _uuid.v4(),
        date: date,
        isPeriodDay: isPeriodDay,
        flowIntensity: flow,
        mood: MoodLevel.values[_random.nextInt(MoodLevel.values.length)],
        energyLevel: _random.nextInt(5) + 1,
        crampIntensity: isPeriodDay && _random.nextBool() ? _random.nextInt(4) + 1 : 0,
        hasHeadache: !isPeriodDay && _random.nextDouble() < 0.2,
        hasBloating: !isPeriodDay && _random.nextDouble() < 0.3,
        hasBreastTenderness: dayOfCycle > 20 && _random.nextDouble() < 0.4,
        hasFatigue: _random.nextDouble() < 0.3,
        hasAcne: dayOfCycle > 18 && _random.nextDouble() < 0.25,
        hasLowerBackPain: isPeriodDay && _random.nextDouble() < 0.4,
        stressLevel: _random.nextInt(5) + 1,
        notes: _getNoteForDay(dayOfCycle, cycleLength),
        husbandNotes: _getHusbandNoteForDay(dayOfCycle),
        createdAt: date,
        updatedAt: date,
      );
      entries.add(entry);
    }
    return entries.reversed.toList();
  }

  String? _getNoteForDay(int dayOfCycle, int cycleLength) {
    if (_random.nextDouble() < 0.3) { // 30% chance of having a note
      if (dayOfCycle < 5) {
        return "Feeling a bit tired and crampy today. Taking it easy.";
      } else if (dayOfCycle > 10 && dayOfCycle < 16) {
        return "Feeling energetic and positive! Productive day at work.";
      } else if (dayOfCycle > cycleLength - 7) {
        return "A bit irritable today, craving some chocolate.";
      } else {
        return "Just a regular day. Nothing much to report.";
      }
    }
    return null;
  }

  String? _getHusbandNoteForDay(int dayOfCycle) {
    if (_random.nextDouble() < 0.2) { // 20% chance of husband note
        if (dayOfCycle < 5) {
        return "She seems to be in a bit of pain. I'll make her some tea.";
      } else if (dayOfCycle > 22) {
        return "She mentioned feeling a little down. Extra hugs tonight.";
      }
    }
    return null;
  }
}