import 'dart:convert';

import 'package:tm_app/core/utils/result.dart';
import 'package:tm_app/data/services/api/tenders_api.dart';
import 'package:tm_app/data/services/model/feedback_response/feedback_response.dart';
import 'package:tm_app/data/services/model/tender_approvals_by_id_response/tender_approvals_by_id_response.dart';
import 'package:tm_app/data/services/model/tender_approvals_toggle_response/tender_approvals_toggle_response.dart';
import 'package:tm_app/data/services/model/tenders/tenders_response/tenders_response.dart';

import '../../core/network/network_manager.dart';

class TendersService {
  TendersService({required NetworkManager networkManager})
    : _networkManager = networkManager;
  final NetworkManager _networkManager;

  Future<Result<TendersResponse>> getTenders({
    required int limit,
    required int offset,
    String? query,
    String? sortBy,
    String? sortOrder,
    String? publicationDateFrom,
    String? publicationDateTo,
  }) async {
    final result = await _networkManager.makeRequest(
      TendersApi.getTenders(
        limit: limit,
        offset: offset,
        query: query,
        sortBy: sortBy,
        sortOrder: sortOrder,
        publicationDateFrom: publicationDateFrom,
        publicationDateTo: publicationDateTo,
      ),

      method: 'GET',
      (json) => TendersResponse.fromJson(json),
    );
    return result;
  }

  Future<Result<FeedbackResponse>> getTenderFeedback({
    required String tenderId,
  }) async {
    final result = await _networkManager.makeRequest(
      TendersApi.getFeedbackForTender(tenderId: tenderId),
      method: 'GET',
      (json) => FeedbackResponse.fromJson(json),
    );
    return result;
  }

  Future<Result<FeedbackResponse>> toggleFeedback({
    required String tenderId,
    required String feedbackType,
  }) async {
    final data = jsonEncode({
      'feedback_type': feedbackType,
      'tender_id': tenderId,
    });
    final result = await _networkManager.makeRequest(
      TendersApi.feedback,
      method: 'POST',
      (json) => FeedbackResponse.fromJson(json),
      data: data,
    );
    return result;
  }

  Future<Result<TenderApprovalsToggleResponse>> toggleApprovals({
    required String tenderId,
    required String submissionMode,
    required String status,
  }) async {
    final data = jsonEncode({
      'tender_id': tenderId,
      'submission_mode': submissionMode,
      'status': status,
    });
    final result = await _networkManager.makeRequest(
      TendersApi.tenderApprovals,
      method: 'POST',
      (json) => TenderApprovalsToggleResponse.fromJson(json),
      data: data,
    );
    return result;
  }

  Future<Result<TenderApprovalsByIdResponse>> getTenderApprovalsById({
    required String tenderId,
  }) async {
    final result = await _networkManager.makeRequest(
      TendersApi.getTenderApprovalById(tenderId: tenderId),
      method: 'GET',
      (json) => TenderApprovalsByIdResponse.fromJson(json),
    );
    return result;
  }
}
