import 'dart:developer';
import 'package:dio/dio.dart';


class YojnaService {
  final Dio _dio;

  YojnaService(this._dio);

  Future<dynamic> fetchpmydetails({
    required String scheme,
    required String action,
    required String accountno,
  }) async {
    try {
      final response = await _dio.post(
        "/api/gov-scheme/req/PMJBY",
        data: {
          'scheme': scheme,
          'action': action,
          'accountNo': accountno,
        },
        options: Options(
          headers: {
            "Content-Type": "application/json",
          },
        ),
      );

      log("PMY Details Response: ${response.data}");

      if (response.statusCode == 200) {
        return response.data;
      } else {
        throw Exception("INTERNAL SERVER ERROR");
      }
    } catch (e) {
      log("Error fetching PMY details: $e");
      return null;
    }
  }

  Future secondvalidationPMJJBY({
     String? aadharno,  
String? accountno,  
String? availablebalance,  
String? country,  
String? customerdob,  
String? customername,  
String? customerno,  
String? dateofacctopening,  
String? emailid,  
String? financialyear, 
String? gender,  
String? ifsccode,  
String? married,  
String? mobileno, 
String? pan,  
String? pincode,  
String? policynumber,  
String? premiumamount,  
String? state,  
String? healthstatus, 
String? collectionchannel, 
String? nomineename,
String? nomineeaddress,
String? nomineerelationship,   
String? nomineeminor,
String? ruralcategory,
  }) async {
    final response = await _dio.post(
      '/api/gov-scheme/create/PMJBY',
      options: Options(
        validateStatus: (int? status) => true,
        receiveDataWhenStatusError: true,
      ),
      data: {
        'aadharno': aadharno ,  
        'accountno': accountno,  
'availablebalance': availablebalance,  
'country':             country,  
'customerdob':         customerdob,  
'customername':        customername,  
'customerno':          customerno,  
'dateofacctopening':   dateofacctopening,  
'emailid':             emailid,  
'financialyear':       financialyear, 
'gender':              gender,  
'ifsccode':            ifsccode,  
'married':             married,  
'mobileno':            mobileno, 
'pan':                 pan,  
'pincode':             pincode,  
'policynumber':        policynumber,  
'premiumamount':       premiumamount,  
'state':               state,  
'healthstatus':        healthstatus, 
'collectionchannel':   collectionchannel, 
'nomineename':         nomineename,
'nomineeaddress':      nomineeaddress,
'nomineerelationship': nomineerelationship, 
'nomineeminor':        nomineeminor,
'ruralcategory':       ruralcategory,
      },
    );
    return response.toString();
  }

  Future secondvalidationPMSBY({
String? aadharno,
String? accountno,
String? address1,
String? address2,
String? availablebalance,
String? city,
String? country,
String? customerdob,
String? customername,
String? customerno,
String? emailid,
String? financialyear,
String? gender,
String? married,
String? mobileno,
String? pan,
String? pincode,
String? policyno,
String? premiumamount,
String? state,
String? healthstatus,
String? nomineename,
String? nomineeadress,
String? relationwithnominee,
String? nomineeminor,
String? collectionchannel,
String? ruralcategory,
String? policystatus,
  }) async {
    final response = await _dio.post(
      '/api/gov-scheme/create/PMSBY',
      options: Options(
        validateStatus: (int? status) => true,
        receiveDataWhenStatusError: true,
      ),
      data: {
        "aadharno": aadharno,
"accountno": accountno,
"address1": address1,
"address2": address2,
"availablebalance": availablebalance,
"city": city,
"country": country,
"customerdob": customerdob,
"customername": customername,
"customerno": customerno,
"emailid": emailid,
"financialyear": financialyear,
"gender": gender,
"married": married,
"mobileno": mobileno,
"pan": pan,
"pincode": pincode,
"policyno": policyno,
"premiumamount": premiumamount,
"state": state,
"healthstatus": healthstatus,
"nomineename": nomineename,
"nomineeadress": nomineeadress,
"relationwithnominee": relationwithnominee,
"nomineeminor": nomineeminor,
"collectionchannel": collectionchannel,
"ruralcategory": ruralcategory,
"policystatus": policystatus,
      },
    );
    return response.toString();
  }

   Future<dynamic> enquiry({
    required String scheme,
    required String action,
    required String financialyear,
    String? customerno,
  }) async {
    try {
      final response = await _dio.get(
        "/api/gov-scheme/enquiry/PMJBY",
        queryParameters: {
          'scheme': scheme,
          'action': action,
          'financialyear': financialyear,
          'customerno': customerno,
        },
        options: Options(
          headers: {
            "Content-Type": "application/json",
          },
        ),
      );

      log("PMY Details Response: ${response.data}");

      if (response.statusCode == 200) {
        return response.data;
      } else {
        throw Exception("INTERNAL SERVER ERROR");
      }
    } catch (e) {
      log("Error fetching PMY details: $e");
      return null;
    }
  }
}