import 'package:get/get.dart';
import 'package:marco/helpers/services/app_logger.dart';
import 'package:marco/helpers/services/api_service.dart';
import 'package:marco/helpers/widgets/my_form_validator.dart';
import 'package:marco/helpers/widgets/my_snackbar.dart';
import 'package:marco/model/project_model.dart';
import 'package:marco/model/dailyTaskPlanning/daily_task_planning_model.dart';
import 'package:marco/model/employees/employee_model.dart';

class DailyTaskPlanningController extends GetxController {
  List<ProjectModel> projects = [];
  List<EmployeeModel> employees = [];
  List<TaskPlanningDetailsModel> dailyTasks = [];

  RxMap<String, RxBool> uploadingStates = <String, RxBool>{}.obs;
  RxList<EmployeeModel> selectedEmployees = <EmployeeModel>[].obs;

  MyFormValidator basicValidator = MyFormValidator();
  List<Map<String, dynamic>> roles = [];
  RxBool isAssigningTask = false.obs;

  RxnString selectedRoleId = RxnString();
  RxBool isLoading = false.obs;

  @override
  void onInit() {
    super.onInit();
    fetchRoles();
    _initializeDefaults();
  }

  void _initializeDefaults() {
    fetchProjects();
  }

  String? formFieldValidator(String? value, {required String fieldType}) {
    if (value == null || value.trim().isEmpty) {
      return 'This field is required';
    }
    if (fieldType == "target" && int.tryParse(value.trim()) == null) {
      return 'Please enter a valid number';
    }
    if (fieldType == "description" && value.trim().length < 5) {
      return 'Description must be at least 5 characters';
    }
    return null;
  }

  void updateSelectedEmployees() {
    final selected =
        employees.where((e) => uploadingStates[e.id]?.value == true).toList();
    selectedEmployees.value = selected;
    logSafe(
      "Updated selected employees",
      level: LogLevel.debug,
    );
  }

  void onRoleSelected(String? roleId) {
    selectedRoleId.value = roleId;
    logSafe(
      "Role selected",
      level: LogLevel.info,
    );
  }

  Future<void> fetchRoles() async {
    logSafe("Fetching roles...", level: LogLevel.info);
    final result = await ApiService.getRoles();
    if (result != null) {
      roles = List<Map<String, dynamic>>.from(result);
      logSafe("Roles fetched successfully", level: LogLevel.info);
      update();
    } else {
      logSafe("Failed to fetch roles", level: LogLevel.error);
    }
  }

  Future<bool> assignDailyTask({
    required String workItemId,
    required int plannedTask,
    required String description,
    required List<String> taskTeam,
    DateTime? assignmentDate,
  }) async {
    isAssigningTask.value = true;
    logSafe("Starting assign task...", level: LogLevel.info);

    final response = await ApiService.assignDailyTask(
      workItemId: workItemId,
      plannedTask: plannedTask,
      description: description,
      taskTeam: taskTeam,
      assignmentDate: assignmentDate,
    );

    isAssigningTask.value = false; 

    if (response == true) {
      logSafe("Task assigned successfully", level: LogLevel.info);
      showAppSnackbar(
        title: "Success",
        message: "Task assigned successfully!",
        type: SnackbarType.success,
      );
      return true;
    } else {
      logSafe("Failed to assign task", level: LogLevel.error);
      showAppSnackbar(
        title: "Error",
        message: "Failed to assign task.",
        type: SnackbarType.error,
      );
      return false;
    }
  }

  Future<void> fetchProjects() async {
    isLoading.value = true;
    try {
      final response = await ApiService.getProjects();
      if (response?.isEmpty ?? true) {
        logSafe("No project data found or API call failed",
            level: LogLevel.warning);
        return;
      }

      projects = response!.map((json) => ProjectModel.fromJson(json)).toList();
      logSafe("Projects fetched: ${projects.length} projects loaded",
          level: LogLevel.info);
      update();
    } catch (e, stack) {
      logSafe("Error fetching projects",
          level: LogLevel.error, error: e, stackTrace: stack);
    } finally {
      isLoading.value = false;
    }
  }

  Future<void> fetchTaskData(String? projectId) async {
    if (projectId == null) {
      logSafe("Project ID is null", level: LogLevel.warning);
      return;
    }

    isLoading.value = true;
    try {
      final response = await ApiService.getDailyTasksDetails(projectId);
      final data = response?['data'];
      if (data != null) {
        dailyTasks = [TaskPlanningDetailsModel.fromJson(data)];
        logSafe(
          "Daily task Planning Details fetched",
          level: LogLevel.info,
        );
      } else {
        logSafe("Data field is null", level: LogLevel.warning);
      }
    } catch (e, stack) {
      logSafe("Error fetching daily task data",
          level: LogLevel.error, error: e, stackTrace: stack);
    } finally {
      isLoading.value = false;
      update();
    }
  }

  Future<void> fetchEmployeesByProject(String? projectId) async {
    if (projectId == null || projectId.isEmpty) {
      logSafe("Project ID is required but was null or empty",
          level: LogLevel.error);
      return;
    }

    isLoading.value = true;
    try {
      final response = await ApiService.getAllEmployeesByProject(projectId);
      if (response != null && response.isNotEmpty) {
        employees =
            response.map((json) => EmployeeModel.fromJson(json)).toList();
        for (var emp in employees) {
          uploadingStates[emp.id] = false.obs;
        }
        logSafe(
          "Employees fetched: ${employees.length} for project $projectId",
          level: LogLevel.info,
        );
      } else {
        employees = [];
        logSafe(
          "No employees found for project $projectId",
          level: LogLevel.warning,
        );
      }
    } catch (e, stack) {
      logSafe(
        "Error fetching employees for project $projectId",
        level: LogLevel.error,
        error: e,
        stackTrace: stack,
      );
    } finally {
      isLoading.value = false;
      update();
    }
  }
}
