import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:marco/helpers/services/app_logger.dart';
import 'package:marco/helpers/services/api_service.dart';
import 'package:marco/model/project_model.dart';
import 'package:marco/model/dailyTaskPlanning/daily_task_model.dart';

class DailyTaskController extends GetxController {
  List<ProjectModel> projects = [];
  String? selectedProjectId;

  DateTime? startDateTask;
  DateTime? endDateTask;

  List<TaskModel> dailyTasks = [];
  final RxSet<String> expandedDates = <String>{}.obs;

  void toggleDate(String dateKey) {
    if (expandedDates.contains(dateKey)) {
      expandedDates.remove(dateKey);
    } else {
      expandedDates.add(dateKey);
    }
  }

  RxBool isLoading = true.obs;
  RxBool isLoadingMore = false.obs;
  Map<String, List<TaskModel>> groupedDailyTasks = {};
  // Pagination
  int currentPage = 1;
  int pageSize = 20;
  bool hasMore = true;
  @override
  void onInit() {
    super.onInit();
    _initializeDefaults();
  }

  void _initializeDefaults() {
    _setDefaultDateRange();
  }

  void _setDefaultDateRange() {
    final today = DateTime.now();
    startDateTask = today.subtract(const Duration(days: 7));
    endDateTask = today;

    logSafe(
      "Default date range set: $startDateTask to $endDateTask",
      level: LogLevel.info,
    );
  }

  Future<void> fetchTaskData(
    String projectId, {
    List<String>? serviceIds,
    int pageNumber = 1,
    int pageSize = 20,
    bool isLoadMore = false,
  }) async {
    if (!isLoadMore) {
      isLoading.value = true;
      currentPage = 1;
      hasMore = true;
      groupedDailyTasks.clear();
      dailyTasks.clear();
    } else {
      isLoadingMore.value = true;
    }

    final response = await ApiService.getDailyTasks(
      projectId,
      dateFrom: startDateTask,
      dateTo: endDateTask,
      serviceIds: serviceIds,
      pageNumber: pageNumber,
      pageSize: pageSize,
    );

    if (response != null && response.isNotEmpty) {
      for (var taskJson in response) {
        final task = TaskModel.fromJson(taskJson);
        final assignmentDateKey =
            task.assignmentDate.toIso8601String().split('T')[0];
        groupedDailyTasks.putIfAbsent(assignmentDateKey, () => []).add(task);
      }
      dailyTasks = groupedDailyTasks.values.expand((list) => list).toList();
      currentPage = pageNumber;
    } else {
      hasMore = false;
    }

    isLoading.value = false;
    isLoadingMore.value = false;

    update();
  }

  Future<void> selectDateRangeForTaskData(
    BuildContext context,
    DailyTaskController controller,
  ) async {
    final picked = await showDateRangePicker(
      context: context,
      firstDate: DateTime(2022),
      lastDate: DateTime.now(),
      initialDateRange: DateTimeRange(
        start:
            startDateTask ?? DateTime.now().subtract(const Duration(days: 7)),
        end: endDateTask ?? DateTime.now(),
      ),
    );

    if (picked == null) {
      logSafe("Date range picker cancelled by user.", level: LogLevel.debug);
      return;
    }

    startDateTask = picked.start;
    endDateTask = picked.end;

    logSafe(
      "Date range selected: $startDateTask to $endDateTask",
      level: LogLevel.info,
    );

    // ✅ Add null check before calling fetchTaskData
    final projectId = controller.selectedProjectId;
    if (projectId != null && projectId.isNotEmpty) {
      await controller.fetchTaskData(projectId);
    } else {
      logSafe("Project ID is null or empty, skipping fetchTaskData",
          level: LogLevel.warning);
    }
  }

  void refreshTasksFromNotification({
    required String projectId,
    required String taskAllocationId,
  }) async {
    // re-fetch tasks
    await fetchTaskData(projectId);

    update(); // rebuilds UI
  }
}
