import 'package:cross_file/cross_file.dart';
import 'package:dio/dio.dart';
import 'package:hoshan/core/services/api/dio_service.dart';
import 'package:hoshan/data/model/ticket_model.dart';

class TicketRepository {
  static final DioService _dioService = DioService();

  static Future<TicketModel> getTickets() async {
    try {
      Response response =
          await _dioService.sendRequest().get(DioService.ticket);
      return TicketModel.fromJson(response.data);
    } catch (ex) {
      rethrow;
    }
  }

  static Future<Response> deleteTickets(int id) async {
    try {
      Response response =
          await _dioService.sendRequest().delete(DioService.deleteTicket(id));
      return response;
    } catch (ex) {
      rethrow;
    }
  }

  static Future<Ticket> sendTickets({required String text, XFile? file}) async {
    try {
      FormData formDatBody = FormData();

      formDatBody.fields.add(MapEntry('text', text));

      if (file != null) {
        MultipartFile multipartFile = await DioService.getMultipartFile(file);

        formDatBody.files.add(MapEntry('file', multipartFile));
      }

      Response response = await _dioService
          .sendRequest()
          .post(DioService.ticket, data: formDatBody);
      return Ticket.fromJson(response.data['ticket']);
    } catch (ex) {
      rethrow;
    }
  }
}
