import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:qpay/screens/pay/pay_controller.dart';
import 'package:qpay/screens/home/home_controller.dart';

part 'transaction_controller.freezed.dart';
part 'transaction_controller.g.dart';

class TransactionModel {
  late Map<String, dynamic> confirmationData;
  late PaymentProcessor selectedPaymentProcessor;
  late FormData repeatFormData;

  BillProduct? selectedProduct;
  BillProvider? selectedProvider;
  Map<String, dynamic>? receiptData;
  String currency = 'USD';
  FormData formData = FormData(
    type: '',
    billClientId: '',
    debitRef: '',
    debitCurrency: 'USD',
    amount: '',
    debitPhone: '',
    debitAccount: '',
    creditAccount: '',
    creditPhone: '',
    creditName: '',
    creditEmail: '',
    billName: '',
    errorMessage: '',
    status: 'PENDING',
    paymentProcessorLabel: '',
    charge: '',
    gatewayCharge: '',
    tax: '',
    totalAmount: '',
    paymentProcessorName: '',
    paymentProcessorImage: '',
    providerImage: '',
    userId: '',
    workspaceId: '',
    providerLabel: '',
    region: '',
  );
  String paymentStatus = 'PENDING';
  String pollStatus = 'PENDING';
  String? errorMessage;
}

@freezed
abstract class FormData with _$FormData {
  const factory FormData({
    String? id,
    required String type, // CONFIRM, REQUEST, REVERSE
    required String billClientId,
    required String debitRef,
    required String debitCurrency,
    required String amount,
    required String creditAccount,
    required String? creditPhone,
    required String? creditName,
    required String? creditEmail,
    required String billName,
    required String? errorMessage,
    required String status,
    required String paymentProcessorLabel,
    required String paymentProcessorName,
    required String paymentProcessorImage,
    required String providerImage,
    required String providerLabel,
    required String userId,
    required String workspaceId,
    String? debitPhone,
    String? debitAccount,
    String? productUid,
    String? billProductName,
    String? trace,
    String? authType,
    String? charge,
    String? gatewayCharge,
    String? tax,
    String? totalAmount,
    String? region,
  }) = _FormData;

  factory FormData.fromJson(Map<String, dynamic> json) =>
      _$FormDataFromJson(json);
}

class TransactionController extends ChangeNotifier {
  TransactionModel model = TransactionModel();

  updateSelectedProvider(BillProvider provider) {
    model.selectedProvider = provider;
    notifyListeners();
  }

  updateSelectedProduct(BillProduct product) {
    model.selectedProduct = product;
    notifyListeners();
  }

  updateSelectedPaymentProcessor(PaymentProcessor processor) {
    model.selectedPaymentProcessor = processor;
    notifyListeners();
  }

  updateConfirmationData(Map<String, dynamic> data) {
    model.confirmationData = data;
    notifyListeners();
  }

  updateReceiptData(Map<String, dynamic> data) {
    model.receiptData = data;
    notifyListeners();
  }

  updateRepeatFormData(FormData data) {
    model.repeatFormData = data;
    notifyListeners();
  }

  updateCurrency(String currency) {
    model.currency = currency;
    notifyListeners();
  }

  clearFormData() {
    model.formData = FormData(
      id: null,
      type: '',
      billClientId: '',
      debitRef: '',
      debitCurrency: 'USD',
      amount: '',
      debitPhone: '',
      debitAccount: '',
      creditAccount: '',
      creditPhone: '',
      creditName: '',
      creditEmail: '',
      billName: '',
      errorMessage: '',
      status: 'PENDING',
      paymentProcessorLabel: '',
      charge: '',
      gatewayCharge: '',
      tax: '',
      totalAmount: '',
      paymentProcessorName: '',
      paymentProcessorImage: '',
      providerImage: '',
      userId: '',
      workspaceId: '',
      providerLabel: '',
      region: '',
    );

    notifyListeners();
  }
}
