import 'package:dio/dio.dart';

class Cheque {
  final String? type;
  final String? InstrType;
  final String? Date;
  final String? branchCode;
  final String? fromCheque;
  final String? toCheque;
  final String? Chequescount;
  final String? ChequeNumber;
  final String? transactionCode;
  final int? amount;
  final String? status;
  final String? stopIssueDate;
  final String? StopExpiryDate;

  Cheque({
    this.type,
    this.InstrType,
    this.Date,
    this.branchCode,
    this.fromCheque,
    this.toCheque,
    this.Chequescount,
    this.ChequeNumber,
    this.transactionCode,
    this.amount,
    this.status,
    this.stopIssueDate,
    this.StopExpiryDate,
  });

  factory Cheque.fromJson(Map<String, dynamic> json) {
    return Cheque(
      type: json['type'] ?? '',
      InstrType: json['InstrType'] ?? '',
      Date: json['Date'] ?? '',
      branchCode: json['branchCode'] ?? '',
      fromCheque: json['fromCheque'] ?? '',
      toCheque: json['toCheque'] ?? '',
      Chequescount: json['Chequescount'] ?? '',
      ChequeNumber: json['ChequeNumber'] ?? '',
      transactionCode: json['transactionCode'] ?? '',
      amount: json['amount'],
      status: json['status'] ?? '',
      stopIssueDate: json['stopIssueDate'] ?? '',
      StopExpiryDate: json['StopExpiryDate'] ?? '',
    );
  }

  static List<Cheque> listFromJson(List<dynamic> jsonList) {
    final chequeList =
        jsonList.map((cheque) => Cheque.fromJson(cheque)).toList();
    return chequeList;
  }
}

class ChequeService {
  final Dio _dio;
  ChequeService(this._dio);

  Future<List<Cheque>> ChequeEnquiry({
    required String accountNumber,
    required String instrType,
  }) async {
    try {
      final response = await _dio.get(
        "/api/cheque/enquiry",
        queryParameters: {
          'accountNumber': accountNumber,
          'instrumentType': instrType,
        },
        options: Options(
          headers: {
            "Content-Type": "application/json",
          },
        ),
      );

      if (response.statusCode == 200) {
        if (response.data is Map<String, dynamic> &&
            response.data.containsKey('records')) {
          final records = response.data['records'];
          if (records is List) {
            return Cheque.listFromJson(records);
          }
        }
        throw Exception(
            "Unexpected API response format: 'records' list not found or malformed");
      } else {
        throw Exception("Failed to fetch");
      }
    } catch (e) {
      print('Error in ChequeEnquiry: $e');
      throw e;
    }
  }

  Future stopCheque({
    required String accountno,
    required String stopFromChequeNo,
    required String instrType,
    String? stopToChequeNo,
    String? stopIssueDate,
    String? stopExpiryDate,
    String? stopAmount,
    String? stopComment,
    String? chequeIssueDate,
    required String tpin,
  }) async {
    final response = await _dio.post(
      '/api/cheque/stop',
      options: Options(
        validateStatus: (int? status) => true,
        receiveDataWhenStatusError: true,
      ),
      data: {
        'accountNumber': accountno,
        'stopFromChequeNo': stopFromChequeNo,
        'instrumentType': instrType,
        'stopToChequeNo': stopToChequeNo,
        'stopIssueDate': stopIssueDate,
        'stopExpiryDate': stopExpiryDate,
        'stopAmount': stopAmount,
        'stopComment': stopComment,
        'chqIssueDate': chequeIssueDate,
        'tpin': tpin,
      },
    );
    return response.toString();
  }

  Future revokeStop({
    required String accountno,
    required String removeFromChequeNo,
    required String instrType,
    String? removeToChequeNo,
    String? removeIssueDate,
    String? removeExpiryDate,
    String? removeAmount,
    String? removeComment,
    required String tpin,
  }) async {
    final response = await _dio.post(
      '/api/cheque/removeStop',
      options: Options(
        validateStatus: (int? status) => true,
        receiveDataWhenStatusError: true,
      ),
      data: {
        'accountNumber': accountno,
        'removeFromChequeNo': removeFromChequeNo,
        'instrumentType': instrType,
        'removeToChequeNo': removeToChequeNo,
        'removeIssueDate': removeIssueDate,
        'removeExpiryDate': removeExpiryDate,
        'removeAmount': removeAmount,
        'removeComment': removeComment,
        'tpin': tpin,
      },
    );
    return response.toString();
  }

  Future registerPPS({
    required String cheque_no,
    required String account_number,
    String? issue_date,
    String? amount,
    String? payee_name,
    required String tpin,
  }) async {
    final response = await _dio.post(
      '/api/pps',
      options: Options(
        validateStatus: (int? status) => true,
        receiveDataWhenStatusError: true,
      ),
      data: {
        'cheque_no': cheque_no,
        'account_number': account_number,
        'issue_date': issue_date,
        'amount': amount,
        'payee_name': payee_name,
        'tpin': tpin,
      },
    );
    return response.toString();
  }
}
