import 'package:social_app/core/api/i_api_client.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 {
  final IApiClient? _apiClient;
  final MockCalendarService _mock = MockCalendarService();

  CalendarService({IApiClient? apiClient}) : _apiClient = apiClient;

  List<ScheduleItemModel> getEventsForDay(DateTime date) {
    if (_apiClient != null) {
      throw UnimplementedError('Real API not implemented');
    }
    return _mock.getEventsForDay(date);
  }

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

  ScheduleItemModel? getEventById(String id) {
    if (_apiClient != null) {
      throw UnimplementedError('Real API not implemented');
    }
    return _mock.getEventById(id);
  }

  void addEvent(ScheduleItemModel event) {
    if (_apiClient != null) {
      throw UnimplementedError('Real API not implemented');
    }
    _mock.addEvent(event);
  }

  void updateEvent(ScheduleItemModel event) {
    if (_apiClient != null) {
      throw UnimplementedError('Real API not implemented');
    }
    _mock.updateEvent(event);
  }

  void deleteEvent(String id) {
    if (_apiClient != null) {
      throw UnimplementedError('Real API not implemented');
    }
    _mock.deleteEvent(id);
  }
}
