import 'package:dio/dio.dart';
import 'package:file_picker/file_picker.dart';
import 'package:qadirneyriz/config/config.dart';
import 'package:qadirneyriz/models/meetings/meetings_model.dart';
import 'package:qadirneyriz/models/meetings/one_meeting_model.dart';
import 'package:qadirneyriz/setting/setting.dart';
import 'package:qadirneyriz/utils/result/result.dart';

class MeetingsApi {
  Future<MeetingsModel> getMeetings({
    required int count,
    required int page,
    String? fromDate,
    String? toDate,
    int? location,
    int? subject,
    int? meetingManager,
    int? status,
  }) async {
    Map<String, String> headers = {
      'Accept': 'application/json',
    };
    String dataToken = setting.userLocalDb.getUser().token!;
    if (dataToken != '') {
      headers['Authorization'] = "Bearer $dataToken";
    }

    final String link = "${config.network.baseUrl}meetings";
    print(
        'Parameters: count: $count, page: $page, fromDate: $fromDate, toDate: $toDate, location: $location, subject: $subject, meetingManager: $meetingManager, status: $status');

    final response = await Dio().get(link,
        options: Options(
          headers: headers,
        ),
        queryParameters: {
          'count': count,
          'page': page,
          'date_meeting_az': fromDate,
          'date_meeting_ta': toDate,
          'location': location,
          'subject': subject,
          'meeting_manager': meetingManager,
          'status': status,
        });
    // print('${response.data} response.data');
    final MeetingsModel meetingsList = MeetingsModel.fromJson(response.data);

    return meetingsList;
  }

// one meeting
  Future<OneMeetingModel> getOneMeeting({required final int id}) async {
    Map<String, String> headers = {
      'Accept': 'application/json',
    };
    String dataToken = setting.userLocalDb.getUser().token!;
    if (dataToken != '') {
      headers['Authorization'] = "Bearer $dataToken";
    }

    final String link = "${config.network.baseUrl}meeting/$id";

    final response = await Dio().get(
      link,
      options: Options(
        headers: headers,
      ),
    );
    print('${response.data} response.data');
    final OneMeetingModel oneMeet = OneMeetingModel.fromJson(response.data);

    return oneMeet;
  }

// edit meeting
  Future<Result> editMeetingApi(
      {required int id,
      required int locationId,
      required int subjectId,
      required int managerId,
      required String fromHour,
      required String toHour,
      required String dateMeeting,
      required List<int> members}) async {
    try {
      Map<String, String> headers = {"Accept": "application/json"};
      String dataToken = setting.userLocalDb.getUser().token!;
      if (dataToken != '') {
        headers['Authorization'] = "Bearer $dataToken";
      }
      FormData? formData;

      formData = FormData.fromMap({
        'id': id,
        'locations_id': locationId,
        'subject_id': subjectId,
        'manager_id': managerId,
        'az_hour': fromHour,
        'ta_hour': toHour,
        'members[]': members,
        'date_meeting': dateMeeting,
      });
      print('${formData.fields} things to send');
      final res = await Dio().post(
          "${config.network.baseUrl}admin/edit-meeting",
          data: formData,
          options: Options(headers: headers));

      if (res.statusCode == 200 || res.statusCode == 201) {
        return Result(isOk: true, message: res.data['message']);
      }
    } on DioException catch (e) {
      print(e);
      return Result(
          isOk: false,
          errors: e.response!.data['errors'],
          message: e.response!.data['message']);
    }
    return const Result(isOk: false);
  }

// cancel meeting
  Future<Result> cancelMeetingApi({
    required int id,
  }) async {
    try {
      Map<String, String> headers = {"Accept": "application/json"};
      String dataToken = setting.userLocalDb.getUser().token!;
      if (dataToken != '') {
        headers['Authorization'] = "Bearer $dataToken";
      }
      FormData? formData;

      formData = FormData.fromMap({
        'meeting_id': id,
      });
      final res = await Dio().post(
          "${config.network.baseUrl}admin/cancel-meeting",
          data: formData,
          options: Options(headers: headers));

      if (res.statusCode == 200 || res.statusCode == 201) {
        return Result(isOk: true, message: res.data['message']);
      }
    } on DioException catch (e) {
      print(e);
      return Result(
          isOk: false,
          errors: e.response!.data['errors'],
          message: e.response!.data['message']);
    }
    return const Result(isOk: false);
  }

// accept meeting
  Future<Result> acceptMeetingApi({
    required int id,
  }) async {
    try {
      Map<String, String> headers = {"Accept": "application/json"};
      String dataToken = setting.userLocalDb.getUser().token!;
      if (dataToken != '') {
        headers['Authorization'] = "Bearer $dataToken";
      }
      FormData? formData;

      formData = FormData.fromMap({
        'meeting_id': id,
      });
      final res = await Dio().post(
          "${config.network.baseUrl}admin/accept-meeting",
          data: formData,
          options: Options(headers: headers));

      if (res.statusCode == 200 || res.statusCode == 201) {
        return Result(isOk: true, message: res.data['message']);
      }
    } on DioException catch (e) {
      print(e);
      return Result(
          isOk: false,
          errors: e.response!.data['errors'],
          message: e.response!.data['message']);
    }
    return const Result(isOk: false);
  }

// add meeting minutes
  Future<Result> addMeetingMinuteApi({
    required int id,
    required String description,
    required List<PlatformFile> meetingFiles, // List of PlatformFile
  }) async {
    try {
      Map<String, String> headers = {"Accept": "application/json"};
      String dataToken = setting.userLocalDb.getUser().token!;
      if (dataToken.isNotEmpty) {
        headers['Authorization'] = "Bearer $dataToken";
      }

      // Create FormData
      FormData formData = FormData();

      // Add id and description fields
      formData.fields.add(MapEntry('id', id.toString()));
      formData.fields.add(MapEntry('description', description));

      // Check if meetingFiles is not empty and add files to FormData
      if (meetingFiles.isNotEmpty) {
        for (var file in meetingFiles) {
          if (file.path != null) {
            // Ensure that the file path is not null
            formData.files.add(
              MapEntry(
                  'meeting_files[]', await MultipartFile.fromFile(file.path!)),
            );
          }
        }
      }

      // Send request
      final res = await Dio().post(
        "${config.network.baseUrl}admin/add-meeting-minutes",
        data: formData,
        options: Options(headers: headers),
      );

      // Check response status
      if (res.statusCode == 200 || res.statusCode == 201) {
        return Result(isOk: true, message: res.data['message']);
      }
    } on DioException catch (e) {
      print(e);
      return Result(
        isOk: false,
        errors: e.response?.data['errors'],
        message: e.response?.data['message'],
      );
    }
    return const Result(isOk: false);
  }
}
