import 'package:dio/dio.dart';

class Branch {
  final String branch_code;
  final String branch_name;
  final String zone;
  final String tehsil;
  final String block;
  final String block_code;
  final String distt_name;
  final String distt_code_slbc;
  final String date_of_opening;
  final String rbi_code_1;
  final String rbi_code_2;
  final String telephone_no;
  final String type_of_branch;
  final String rtgs_acct_no;
  final String br_lattitude;
  final String br_longitude;
  final String pincode;
  final String post_office;

  Branch({
    required this.branch_code,
    required this.branch_name,
    required this.zone,
    required this.tehsil,
    required this.block,
    required this.block_code,
    required this.distt_name,
    required this.distt_code_slbc,
    required this.date_of_opening,
    required this.rbi_code_1,
    required this.rbi_code_2,
    required this.telephone_no,
    required this.type_of_branch,
    required this.rtgs_acct_no,
    required this.br_lattitude,
    required this.br_longitude,
    required this.pincode,
    required this.post_office,
  });

  factory Branch.fromJson(Map<String, dynamic> json) {
    return Branch(
      branch_code: json['branch_code'] ?? json['branch_code'] ?? '',
      branch_name: json['branch_name'] ?? json['branch_name'] ?? '',
      zone: json['zone'] ?? json['zone'] ?? '',
      tehsil: json['tehsil'] ?? json['tehsil'] ?? '',
      block: json['block'] ?? json['block'] ?? '',
      block_code: json['block_code'] ?? json['block_code'] ?? '',
      distt_name: json['distt_name'] ?? json['distt_name'] ?? '',
      distt_code_slbc: json['distt_code_slbc'] ?? json['distt_code_slbc'] ?? '',
      date_of_opening: json['date_of_opening'] ?? json['date_of_opening'] ?? '',
      rbi_code_1: json['rbi_code_1'] ?? json['rbi_code_1'] ?? '',
      rbi_code_2: json['rbi_code_2'] ?? json['rbi_code_2'] ?? '',
      telephone_no: json['telephone_no'] ?? json['telephone_no'] ?? '',
      type_of_branch: json['type_of_branch'] ?? json['type_of_branch'] ?? '',
      rtgs_acct_no: json['rtgs_acct_no'] ?? json['rtgs_acct_no'] ?? '',
      br_lattitude: json['br_lattitude'] ?? json['br_lattitude'] ?? '',
      br_longitude: json['br_longitude'] ?? json['br_longitude'] ?? '',
      pincode: json['pincode'] ?? json['pincode'] ?? '',
      post_office: json['post_office'] ?? json['post_office'] ?? '',
    );
  }

  static List<Branch> listFromJson(List<dynamic> jsonList) {
    final beneficiaryList =
        jsonList.map((beneficiary) => Branch.fromJson(beneficiary)).toList();
    return beneficiaryList;
  }
}

class Atm {
  final String name;

  Atm({required this.name});

  factory Atm.fromJson(Map<String, dynamic> json) {
    return Atm(
      name: json['name'] ?? '', // Assuming the API returns a 'name' field
    );
  }

  static List<Atm> listFromJson(List<dynamic> jsonList) {
    return jsonList.map((atm) => Atm.fromJson(atm)).toList();
  }
}

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

  Future<List<Branch>> fetchBranchList() async {
    try {
      final response = await _dio.get(
        "/api/branch",
        options: Options(
          headers: {
            "Content-Type": "application/json",
          },
        ),
      );

      if (response.statusCode == 200) {
        return Branch.listFromJson(response.data);
      } else {
        throw Exception("Failed to fetch");
      }
    } catch (e) {
      return [];
    }
  }

  Future<List<Atm>> fetchAtmList() async {
    try {
      final response = await _dio.get(
        "/api/atm",
        options: Options(
          headers: {
            "Content-Type": "application/json",
          },
        ),
      );
      if (response.statusCode == 200) {
        return Atm.listFromJson(response.data);
      } else {
        throw Exception("Failed to fetch ATM list: ${response.statusCode}");
      }
    } catch (e) {
      // You might want to log the error here for debugging
      print("Error fetching ATM list: $e");
      return [];
    }
  }
}
