import 'package:social_app/core/config/env.dart';
import '../models/schedule_item_model.dart';

class MockCalendarService {
  static final MockCalendarService _instance = MockCalendarService._internal();
  factory MockCalendarService() => _instance;

  final List<ScheduleItemModel> _events = [];

  MockCalendarService._internal();

  List<ScheduleItemModel> get events => List.unmodifiable(_events);

  List<ScheduleItemModel> getEventsForDay(DateTime date) {
    final dateOnly = DateTime(date.year, date.month, date.day);
    return _events.where((event) {
      final eventDate = DateTime(
        event.startAt.year,
        event.startAt.month,
        event.startAt.day,
      );
      return eventDate == dateOnly && event.status == ScheduleStatus.active;
    }).toList()..sort((a, b) => a.startAt.compareTo(b.startAt));
  }

  List<ScheduleItemModel> getEventsForRange(DateTime start, DateTime end) {
    return _events.where((event) {
      return event.startAt.isAfter(start.subtract(const Duration(days: 1))) &&
          event.startAt.isBefore(end.add(const Duration(days: 1))) &&
          event.status == ScheduleStatus.active;
    }).toList()..sort((a, b) => a.startAt.compareTo(b.startAt));
  }

  ScheduleItemModel? getEventById(String id) {
    try {
      return _events.firstWhere((e) => e.id == id);
    } catch (_) {
      return null;
    }
  }

  void addEvent(ScheduleItemModel event) {
    _events.add(event);
  }

  void updateEvent(ScheduleItemModel event) {
    final index = _events.indexWhere((e) => e.id == event.id);
    if (index >= 0) {
      _events[index] = event;
    }
  }

  void deleteEvent(String id) {
    _events.removeWhere((e) => e.id == id);
  }
}

class CalendarService {
  static final CalendarService _instance = CalendarService._internal();
  factory CalendarService() => _instance;
  CalendarService._internal();

  MockCalendarService get _mock => MockCalendarService();

  List<ScheduleItemModel> getEventsForDay(DateTime date) {
    if (Env.isMockApi) {
      return _mock.getEventsForDay(date);
    }
    throw UnimplementedError('Real API not implemented');
  }

  List<ScheduleItemModel> getEventsForRange(DateTime start, DateTime end) {
    if (Env.isMockApi) {
      return _mock.getEventsForRange(start, end);
    }
    throw UnimplementedError('Real API not implemented');
  }

  ScheduleItemModel? getEventById(String id) {
    if (Env.isMockApi) {
      return _mock.getEventById(id);
    }
    throw UnimplementedError('Real API not implemented');
  }

  void addEvent(ScheduleItemModel event) {
    if (Env.isMockApi) {
      _mock.addEvent(event);
      return;
    }
    throw UnimplementedError('Real API not implemented');
  }

  void updateEvent(ScheduleItemModel event) {
    if (Env.isMockApi) {
      _mock.updateEvent(event);
      return;
    }
    throw UnimplementedError('Real API not implemented');
  }

  void deleteEvent(String id) {
    if (Env.isMockApi) {
      _mock.deleteEvent(id);
      return;
    }
    throw UnimplementedError('Real API not implemented');
  }
}
