from appservices.common.util import *

bbps_frontend_apis = Blueprint("bbps_frontend_apis",__name__)

csrf.exempt(bbps_frontend_apis)


def getBbpsAuthorizationToken1():
    data_status = {"responseStatus":0,"result":"","accessToken":""}
    try:
        url = "https://accounts.payu.in/oauth/token"
        headers = {
        "content-type": "application/json",
        "content-type": "application/x-www-form-urlencoded"
        }
        payload="client_id=35e731ff20e6bc521de9f29618b22c1240a4c69f6350c50e0fcb8e6b7d6c1bec&client_secret=610a66e8fb918d2c6a7ccf5674ea2893cb7cbbd589a0c3d9544e3f3ddf7366f4&scope=read_bills create_transactions read_billers read_biller_categories read_transactions read_plans read_operator_circle read_circles read_operators&grant_type=client_credentials"
        payuresponse = requests.post(url, data=payload, headers=headers)
        payuresponseData = json.loads(payuresponse.text)
        data_status['responseStatus']=1
        data_status['accessToken']=payuresponseData.get("access_token")
        return data_status
    except Exception as e:
        app.logger.error(traceback.format_exc())
        data_status["result"]="Unable to fetch bill request data!!"
        return data_status

def dynamicBbpsAuthorizationToken(client_id,client_secret,scope,grant_type):
    data_status = {"responseStatus":0,"result":"","accessToken":""}
    try:
        url = "https://accounts.payu.in/oauth/token"
        headers = {
        "content-type": "application/json",
        "content-type": "application/x-www-form-urlencoded"
        }
        payload="client_id="+str(client_id)+"&client_secret="+str(client_secret)+"&scope="+str(scope)+"&grant_type="+str(grant_type)
        payuresponse = requests.post(url, data=payload, headers=headers)
        payuresponseData = json.loads(payuresponse.text)
        data_status['responseStatus']=1
        data_status['accessToken']=payuresponseData.get("access_token")
        return data_status
    except Exception as e:
        app.logger.error(traceback.format_exc())
        data_status["result"]="Unable to fetch bill request data!!"
        return data_status





@bbps_frontend_apis.route("/get_prepaid_recharge_plans",methods=["POST"])
@encrypt_decrypt_before_login
def get_prepaid_recharge_plans():
    data_status = {"responseStatus":0,"result":""}
    try:
        try:
          data = request.decrypted_data
          userId = data.get("userId","")
          operatorId = data.get("operatorId","")
          circleId = data.get("circleId","")
        except Exception as e:
          app.logger.error(traceback.format_exc())
          data_status["result"] = "Invalid Request"
          return data_status
        # userId = request.json.get("userId","")
        # operatorId = request.json.get("operatorId","")
        # circleId = request.json.get("circleId","")

        sampleResponse = {
          "code": 200,
          "payload": [
            {
              "circleWisePlanLists": [
                {
                  "circleId": "2",
                  "circleName": "ANDHRA PRADESH AND TELANGNA",
                  "plansInfo": [
                    {
                      "packageDescription": "Get Unlimited Data for 1 hour.",
                      "planName": "Data",
                      "planType": "NA",
                      "price": "11",
                      "talkTime": "0.00",
                      "validity": "1 Hour",
                      "validityDescription": "1 Hour"
                    },
                    {
                      "packageDescription": "Get 1 GB Data.",
                      "planName": "Data",
                      "planType": "NA",
                      "price": "22",
                      "talkTime": "0.00",
                      "validity": "1 Day",
                      "validityDescription": "1 Day"
                    },
                    {
                      "packageDescription": "Get 1.5 GB Data.",
                      "planName": "Data",
                      "planType": "NA",
                      "price": "26",
                      "talkTime": "0.00",
                      "validity": "1 Day",
                      "validityDescription": "1 Day"
                    },
                    {
                      "packageDescription": "Get 2 GB Data.",
                      "planName": "Data",
                      "planType": "NA",
                      "price": "33",
                      "talkTime": "0.00",
                      "validity": "1 Day",
                      "validityDescription": "1 Day"
                    },
                    {
                      "packageDescription": "Get Unlimited Data for 1 day.",
                      "planName": "Data",
                      "planType": "NA",
                      "price": "49",
                      "talkTime": "0.00",
                      "validity": "1 Day",
                      "validityDescription": "1 Day"
                    },
                    {
                      "packageDescription": "Get 5 GB Data.",
                      "planName": "Data",
                      "planType": "NA",
                      "price": "77",
                      "talkTime": "0.00",
                      "validity": "7 Days",
                      "validityDescription": "7 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Data for 2 days.",
                      "planName": "Data",
                      "planType": "NA",
                      "price": "99",
                      "talkTime": "0.00",
                      "validity": "2 Day",
                      "validityDescription": "2 Day"
                    },
                    {
                      "packageDescription": "Get 5 GB Data + JioHotstar Mobile subscription for 30 days.",
                      "planName": "Data",
                      "planType": "NA",
                      "price": "100",
                      "talkTime": "0.00",
                      "validity": "30 Days",
                      "validityDescription": "30 Days"
                    },
                    {
                      "packageDescription": "Get 6 GB Data.",
                      "planName": "Data",
                      "planType": "NA",
                      "price": "121",
                      "talkTime": "0.00",
                      "validity": "30 Days",
                      "validityDescription": "30 Days"
                    },
                    {
                      "packageDescription": "Get 1 GB Data + Airtel Xstream Play Premium (30 Days Free access to 22+ OTTs).",
                      "planName": "Data",
                      "planType": "NA",
                      "price": "149",
                      "talkTime": "0.00",
                      "validity": "Existing Plan",
                      "validityDescription": "Existing Plan"
                    },
                    {
                      "packageDescription": "Get 12 GB Data.",
                      "planName": "Data",
                      "planType": "NA",
                      "price": "161",
                      "talkTime": "0.00",
                      "validity": "30 Days",
                      "validityDescription": "30 Days"
                    },
                    {
                      "packageDescription": "Get 15 GB Data + Airtel Xstream Play Premium (30 Days Free access to 22+ OTTs).",
                      "planName": "Data",
                      "planType": "NA",
                      "price": "181",
                      "talkTime": "0.00",
                      "validity": "30 Days",
                      "validityDescription": "30 Days"
                    },
                    {
                      "packageDescription": "Get 15 GB Data + JioHotstar Mobile subscription for 3 months.",
                      "planName": "Data",
                      "planType": "NA",
                      "price": "195",
                      "talkTime": "0.00",
                      "validity": "90 Days",
                      "validityDescription": "90 Days"
                    },
                    {
                      "packageDescription": "Get 1 GB/Day Data.",
                      "planName": "Data",
                      "planType": "NA",
                      "price": "211",
                      "talkTime": "0.00",
                      "validity": "30 Days",
                      "validityDescription": "30 Days"
                    },
                    {
                      "packageDescription": "Get 50 GB Data.",
                      "planName": "Data",
                      "planType": "NA",
                      "price": "361",
                      "talkTime": "0.00",
                      "validity": "30 Days",
                      "validityDescription": "30 Days"
                    },
                    {
                      "packageDescription": "Get In-Flight 250 MB Data + 100 Mins OG, No incoming calls + 100 SMS (Selected INT Flight), for covered flights, visit www.airtel.in/IR",
                      "planName": "Inflight Roaming Pack",
                      "planType": "NA",
                      "price": "196",
                      "talkTime": "0.00",
                      "validity": "1 Day",
                      "validityDescription": "1 Day"
                    },
                    {
                      "packageDescription": "Get In-Flight 500 MB Data + 100 Mins OG, No incoming calls + 100 SMS (Selected INT Flight), for covered flights, visit www.airtel.in/IR",
                      "planName": "Inflight Roaming Pack",
                      "planType": "NA",
                      "price": "295",
                      "talkTime": "0.00",
                      "validity": "1 Day",
                      "validityDescription": "1 Day"
                    },
                    {
                      "packageDescription": "Get In-Flight 1 GB Data + 100 Mins OG, No incoming calls + 100 SMS (Selected INT Flight), for covered flights, visit www.airtel.in/IR",
                      "planName": "Inflight Roaming Pack",
                      "planType": "NA",
                      "price": "595",
                      "talkTime": "0.00",
                      "validity": "1 Day",
                      "validityDescription": "1 Day"
                    },
                    {
                      "packageDescription": "Get 500 MB Data + 100 Mins IC+OG (India + Local) + 10 SMS (Covers - USA, Europe, Gulf, Asia, Africa & More)",
                      "planName": "International Roaming",
                      "planType": "NA",
                      "price": "648",
                      "talkTime": "0.00",
                      "validity": "1 Day",
                      "validityDescription": "1 Day"
                    },
                    {
                      "packageDescription": "Get 1 GB Data (Covered Countries -USA, Europe, Gulf, Asia, Africa & more.)",
                      "planName": "International Roaming",
                      "planType": "NA",
                      "price": "755",
                      "talkTime": "0.00",
                      "validity": "5 Days",
                      "validityDescription": "5 Days"
                    },
                    {
                      "packageDescription": "Get 100 Mins IC+OG (India + Local) (Covers - USA, Europe, Gulf, Asia, Africa & more)",
                      "planName": "International Roaming",
                      "planType": "NA",
                      "price": "756",
                      "talkTime": "0.00",
                      "validity": "5 Days",
                      "validityDescription": "5 Days"
                    },
                    {
                      "packageDescription": "Get 1 GB Data + 100 Mins IC+OG (India + Local) + 20 SMS (Covers - USA, Europe, Gulf, Asia, Africa & more.)",
                      "planName": "International Roaming",
                      "planType": "NA",
                      "price": "899",
                      "talkTime": "0.00",
                      "validity": "10 Days",
                      "validityDescription": "10 Days"
                    },
                    {
                      "packageDescription": "Get 2 GB Data + 100 Mins IC+OG (India + Local) + 20 SMS (Covers - USA, Europe, Gulf, Asia, Africa & More), In-Flight 250 MB Data + 100 Mins OG + 100 SMS, 24 Hr Validity (Selected INTl Flights).",
                      "planName": "International Roaming",
                      "planType": "NA",
                      "price": "2997",
                      "talkTime": "0.00",
                      "validity": "365 Days",
                      "validityDescription": "365 Days"
                    },
                    {
                      "packageDescription": "Get 5 GB Data + (10 GB for USA) 200 Mins IC+OG (India + Local) + 20 SMS (Covers - USA, Europe, Gulf & more), In-Flight 250 MB Data + 100 Mins OG + 100 SMS, 24 Hr Validity (Selected INTl Flights).",
                      "planName": "International Roaming",
                      "planType": "NA",
                      "price": "2998",
                      "talkTime": "0.00",
                      "validity": "30 Days",
                      "validityDescription": "30 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 50 GB Data + 300 SMS + Apollo 24|7 Circle (3 Months, at No Cost) + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Plan Vouchers",
                      "planType": "NA",
                      "price": "589",
                      "talkTime": "0.00",
                      "validity": "30 Days",
                      "validityDescription": "30 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 60 GB Data + 300 SMS + Apollo 24|7 Circle (3 Months, at No Cost) + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Plan Vouchers",
                      "planType": "NA",
                      "price": "609",
                      "talkTime": "0.00",
                      "validity": "1 Month",
                      "validityDescription": "1 Month"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 2 GB/Day Data + 100 SMS/Day + Apollo 24|7 Circle (3 Months, at No Cost) + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS) + Unlimited 5G Data (Unlimited 5G Data is over and above your plan limit and can be used in 5G Network areas only).",
                      "planName": "Plan Vouchers",
                      "planType": "NA",
                      "price": "3599",
                      "talkTime": "0.00",
                      "validity": "365 Days",
                      "validityDescription": "365 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 2.5 GB/Day Data + 100 SMS/Day + JioHotstar Mobile Subscription for 1 Year + Apollo 24|7 Circle (3 Months, at No Cost) + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS) + Unlimited 5G Data (Unlimited 5G Data is over and above your plan limit and can be used in 5G Network areas only).",
                      "planName": "Plan Vouchers",
                      "planType": "NA",
                      "price": "3999",
                      "talkTime": "0.00",
                      "validity": "365 Days",
                      "validityDescription": "365 Days"
                    },
                    {
                      "packageDescription": "Get Talktime of Rs. 7.47",
                      "planName": "Topup Plan",
                      "planType": "NA",
                      "price": "10",
                      "talkTime": "7.47",
                      "validity": "NA",
                      "validityDescription": "NA"
                    },
                    {
                      "packageDescription": "Get Talktime of Rs. 98.69",
                      "planName": "Topup Plan",
                      "planType": "NA",
                      "price": "120",
                      "talkTime": "98.69",
                      "validity": "NA",
                      "validityDescription": "NA"
                    },
                    {
                      "packageDescription": "Get Talktime of Rs. 423.73",
                      "planName": "Topup Plan",
                      "planType": "NA",
                      "price": "500",
                      "talkTime": "423.73",
                      "validity": "NA",
                      "validityDescription": "NA"
                    },
                    {
                      "packageDescription": "Get Talktime of Rs. 847.46",
                      "planName": "Topup Plan",
                      "planType": "NA",
                      "price": "1000",
                      "talkTime": "847.46",
                      "validity": "NA",
                      "validityDescription": "NA"
                    },
                    {
                      "packageDescription": "Get Talktime of Rs. 4237.29",
                      "planName": "Topup Plan",
                      "planType": "NA",
                      "price": "5000",
                      "talkTime": "4237.29",
                      "validity": "NA",
                      "validityDescription": "NA"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 2 GB Data + 100 SMS/Day + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "199",
                      "talkTime": "0.00",
                      "validity": "28 Days",
                      "validityDescription": "28 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 3 GB Data + 300 SMS + Talktime of Rs. 5 + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "219",
                      "talkTime": "5.00",
                      "validity": "30 Days",
                      "validityDescription": "30 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 1 GB/Day Data + 100 SMS/Day + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "249",
                      "talkTime": "0.00",
                      "validity": "24 Days",
                      "validityDescription": "24 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 1 GB/Day Data + 100 SMS/Day + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "299",
                      "talkTime": "0.00",
                      "validity": "28 Days",
                      "validityDescription": "28 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 1 GB/Day Data + 100 SMS/Day + JioHotstar Mobile subscription for 3 months + Apollo 24|7 Circle (3 Months, at No Cost).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "301",
                      "talkTime": "0.00",
                      "validity": "28 Days",
                      "validityDescription": "28 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 1.5 GB/Day Data + 100 SMS/Day + Apollo 24|7 Circle (3 Months, at No Cost) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "349",
                      "talkTime": "0.00",
                      "validity": "28 Days",
                      "validityDescription": "28 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 25 GB Data + 100 SMS/Day + Apollo 24|7 Circle (3 Months, at No Cost) + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "355",
                      "talkTime": "0.00",
                      "validity": "30 Days",
                      "validityDescription": "30 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 2 GB/Day Data + 100 SMS/Day + Apollo 24|7 Circle (3 Months, at No Cost) + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS) + Unlimited 5G Data (Unlimited 5G Data is over and above your plan limit and can be used in 5G Network areas only).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "379",
                      "talkTime": "0.00",
                      "validity": "1 Month",
                      "validityDescription": "1 Month"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 2 GB/day Data + 100 SMS/Day + JioHotstar Subscription for 28 days + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS) + Unlimited 5G Data (Unlimited 5G Data is over and above your plan limit and can be used in 5G Network areas only).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "398",
                      "talkTime": "0.00",
                      "validity": "28 Days",
                      "validityDescription": "28 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 2.5 GB Data/Day + 100 SMS/Day + Talktime of Rs 5 + Airtel Xstream Play Premium (28 Days Free access to 22+ OTTs) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS) + Unlimited 5G Data (Unlimited 5G Data is over and above your plan limit and can be used in 5G Network areas only).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "409",
                      "talkTime": "5.00",
                      "validity": "28 Days",
                      "validityDescription": "28 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 2.5 GB/Day Data + 100 SMS/Day + Talktime of Rs. 5 + Apollo 24|7 Circle (3 Months, at No Cost) + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS) + Unlimited 5G Data (Unlimited 5G Data is over and above your plan limit and can be used in 5G Network areas only).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "429",
                      "talkTime": "0.00",
                      "validity": "1 Month",
                      "validityDescription": "1 Month"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 3 GB/Day Data + 100 SMS/Day + Apollo 24|7 Circle (3 Months, at No Cost) + Airtel Xstream Play Premium (28 Days Free access to 22+ OTTs) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS) + Unlimited 5G Data (Unlimited 5G Data is over and above your plan limit and can be used in 5G Network areas only).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "449",
                      "talkTime": "0.00",
                      "validity": "28 Days",
                      "validityDescription": "28 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 900 SMS + Apollo 24|7 Circle (3 Months, at No Cost) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "469",
                      "talkTime": "0.00",
                      "validity": "84 Days",
                      "validityDescription": "84 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 6 GB Data + 600 SMS + Apollo 24|7 Circle (3 Months, at No Cost) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "489",
                      "talkTime": "0.00",
                      "validity": "77 Days",
                      "validityDescription": "77 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 7 GB Data + 900 SMS + Apollo 24|7 Circle (3 Months, at No Cost) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "548",
                      "talkTime": "0.00",
                      "validity": "84 Days",
                      "validityDescription": "84 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 3 GB/Day Data + 100 SMS/Day + JioHotstar Mobile Subscription for 3 Months + Apollo 24|7 Circle (3 Months, at No Cost) + Airtel Xstream Play Premium (28 Days Free access to 22+ OTTs) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS) + Zee5 Premium Subcription. (Start from the day of recharge, clain on Airtel Xstream Play App) + Unlimited 5G Data (Unlimited 5G Data is over and above your plan limit and can be used in 5G Network areas only).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "549",
                      "talkTime": "0.00",
                      "validity": "28 Days",
                      "validityDescription": "28 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 1.5 GB/Day Data + 100 SMS/Day + Apollo 24|7 Circle (3 Months, at No Cost) + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "579",
                      "talkTime": "0.00",
                      "validity": "56 Days",
                      "validityDescription": "56 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 50 GB Data + 300 SMS + Apollo 24|7 Circle (3 Months, at No Cost) + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "589",
                      "talkTime": "0.00",
                      "validity": "30 Days",
                      "validityDescription": "30 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 60 GB Data + 300 SMS + Apollo 24|7 Circle (3 Months, at No Cost) + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "609",
                      "talkTime": "0.00",
                      "validity": "1 Month",
                      "validityDescription": "1 Month"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 1.5 GB/Day Data + 100 SMS/Day + Apollo 24|7 Circle (3 Months, at No Cost) + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "619",
                      "talkTime": "0.00",
                      "validity": "60 Days",
                      "validityDescription": "60 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 2 GB/Day Data + 100 SMS/Day + Apollo 24|7 Circle (3 Months, at No Cost) + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS) + Unlimited 5G Data (Unlimited 5G Data is over and above your plan limit and can be used in 5G Network areas only).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "649",
                      "talkTime": "0.00",
                      "validity": "56 Days",
                      "validityDescription": "56 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 1.5 GB/Day Data + 100 SMS/Day + Apollo 24|7 Circle (3 Months, at No Cost) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "799",
                      "talkTime": "0.00",
                      "validity": "77 Days",
                      "validityDescription": "77 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 3 GB/Day Data + 100 SMS/Day + Amazon Prime Lite Membership for 56 Days included with your plan + Apollo 24|7 Circle (3 Months, at No Cost) + Airtel Xstream Play Premium (56 Days Free access to 22+ OTTs) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS) + Unlimited 5G Data (Unlimited 5G Data is over and above your plan limit and can be used in 5G Network areas only).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "838",
                      "talkTime": "0.00",
                      "validity": "56 Days",
                      "validityDescription": "56 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 1.5 GB/Day Data + 100 SMS/Day + Apollo 24|7 Circle (3 Months, at No Cost) + RewardsMini Subscription + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "859",
                      "talkTime": "0.00",
                      "validity": "84 Days",
                      "validityDescription": "84 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 1.5 GB/Day Data + 100 SMS/Day + Apollo 24|7 Circle (3 Months, at No Cost) + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "929",
                      "talkTime": "0.00",
                      "validity": "90 Days",
                      "validityDescription": "90 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 2 GB/Day Data + 100 SMS/Day + Apollo 24|7 Circle (3 Months, at No Cost) + Airtel Xstream Play Premium (84 Days Free access to 22+ OTTs) + RewardsMini Subscription + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS) + Unlimited 5G Data (Unlimited 5G Data is over and above your plan limit and can be used in 5G Network areas only).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "979",
                      "talkTime": "0.00",
                      "validity": "84 Days",
                      "validityDescription": "84 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 2 GB/Day Data + 100 SMS/Day + JioHotstar Mobile Subscription for 3 month + Apollo 24|7 Circle (3 Months, at No Cost) + RewardsMini Subscription + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS) + Unlimited 5G Data (Unlimited 5G Data is over and above your plan limit and can be used in 5G Network areas only).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "1029",
                      "talkTime": "0.00",
                      "validity": "84 Days",
                      "validityDescription": "84 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 2.5 GB/Day Data + 100 SMS/Day + Amazon Prime Lite Membership for 84 Days + Apollo 24|7 Circle (3 Months, at No Cost) + Airtel Xstream Play Premium (84 Days Free access to 22+ OTTs) + RewardsMini Subscription + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS) + Unlimited 5G Data (Unlimited 5G Data is over and above your plan limit and can be used in 5G Network areas only).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "1199",
                      "talkTime": "0.00",
                      "validity": "84 Days",
                      "validityDescription": "84 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 3 GB/Day Data + 100 SMS/Day + Apollo 24|7 Circle (3 Months, at No Cost) + Netflix Basic (All device access, Unlimited entertainment) + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS) + Unlimited 5G Data (Unlimited 5G Data is over and above your plan limit and can be used in 5G Network areas only).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "1798",
                      "talkTime": "0.00",
                      "validity": "84 Days",
                      "validityDescription": "84 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 3600 SMS + Apollo 24|7 Circle (3 Months, at No Cost) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "1849",
                      "talkTime": "0.00",
                      "validity": "365 Days",
                      "validityDescription": "365 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 30 GB Data + 3600 SMS + Apollo 24|7 Circle (3 Months, at No Cost) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "2249",
                      "talkTime": "0.00",
                      "validity": "365 Days",
                      "validityDescription": "365 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 2 GB/Day Data + 100 SMS/Day + Apollo 24|7 Circle (3 Months, at No Cost) + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS) + Unlimited 5G Data (Unlimited 5G Data is over and above your plan limit and can be used in 5G Network areas only).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "3599",
                      "talkTime": "0.00",
                      "validity": "365 Days",
                      "validityDescription": "365 Days"
                    },
                    {
                      "packageDescription": "Get Unlimited Local STD & Roaming calls + 2.5 GB/Day Data + 100 SMS/Day + JioHotstar Mobile Subscription for 1 Year + Apollo 24|7 Circle (3 Months, at No Cost) + Watch free TV Shows, Movie, Live channels and much more. Download Airtel Xstream app to enjoy free content(premium subscription not included) + India's 1st Spam Fighting Network(Get Live Alerts as \"Airtel warning: SPAM\" on your phone for incoming spam calls and SMS) + Unlimited 5G Data (Unlimited 5G Data is over and above your plan limit and can be used in 5G Network areas only).",
                      "planName": "Unlimited Packs",
                      "planType": "NA",
                      "price": "3999",
                      "talkTime": "0.00",
                      "validity": "365 Days",
                      "validityDescription": "365 Days"
                    }
                  ]
                }
              ],
              "operatorId": "AT",
              "operatorName": "AIRTEL"
            }
          ],
          "status": "SUCCESS"
        }

        if userId and operatorId and circleId:
            user_queryset = Users.objects(id=userId).first()
            if not user_queryset:
                data_status["result"]="Invalid User Id!!"
                return data_status
            
            paymentGatewayId = None
            prepaidProviderId = None
            prepaid_api_queryset = PrepaidMobileSetup.objects(enablePrepaid=True,status=1).first()
            if prepaid_api_queryset:
                paymentGatewayId = str(prepaid_api_queryset.prepaidPgId.id)
                prepaidProviderId = str(prepaid_api_queryset.prepaidProviderId.id)
            else:
                data_status["result"]="Service provider not available please contact to admin!!"
                return data_status

            transaction_api_queryset = TransactionAPI.objects(id=paymentGatewayId,status=1).first()
            if not transaction_api_queryset:
                data_status["result"]="Invalid paymentgateway id!!"
                return data_status

            # if transaction_api_queryset.code == "PayU_BBPS":
            #   client_id = ""
            #   client_secret = ""
            #   get_base_url = ""
            #   agentId = ""
            #   scope = ""
            #   grant_type = ""
            #   for each_key in transaction_api_queryset.paramsList:
            #       get_key = each_key.get("key")
            #       if get_key == "client_id":
            #           client_id = each_key.get("value")
            #       if get_key == "client_secret":
            #           client_secret = each_key.get("value")
            #       if get_key == "get_base_url":
            #           get_base_url = each_key.get("value")
            #       if get_key == "agentId":
            #           agentId = each_key.get("value")
            #       if get_key == "scope":
            #           scope = each_key.get("value")
            #       if get_key == "grant_type":
            #           grant_type = each_key.get("value")

            #   try:
            #       authtokenresp=dynamicBbpsAuthorizationToken(client_id,client_secret,scope,grant_type)
            #       authorizationToken=authtokenresp.get('accessToken')
            #       headers = {
            #       "authorization": "Bearer "+str(authorizationToken)
            #       }
            #       print(headers,"headers")
            #       payload = {
            #       "agentId": agentId,
            #       "operatorId": operatorId,
            #       "circleId":circleId
            #       }
            #       url = get_base_url+"getRechargePlans"
            #       payuresponse = requests.post(url,json=payload,headers=headers)
            #       print(payuresponse.text,"(((((((((((((payuresponse)))))))))))))")
            #       payuresponseData = json.loads(payuresponse.text)
            #   except Exception as e:
            #       app.logger.error(traceback.format_exc())
            #       data_status["result"]="Service provider not available please contact to admin!!"
            #       return data_status
            # else:
            #   data_status["result"]="Service provider not available please contact to admin!!"
            #   return data_status
            plansList = []
            if sampleResponse.get("code") == 200:
                if sampleResponse.get("payload"):
                    plan_group_map = {}
                    # plans_info = sampleResponse["payload"][0]["circleWisePlanLists"][0]["plansInfo"]
                    payload = sampleResponse["payload"][0]
                    circle_data = payload["circleWisePlanLists"][0]
                    plans_info = circle_data["plansInfo"]

                    for plan in plans_info:
                        plan_name = plan["planName"]
                        if plan_name not in plan_group_map:
                            plan_group_map[plan_name] = []
                        plan_group_map[plan_name].append(plan)

                    for plan_name, plans in plan_group_map.items():
                        planDict = {
                        "planType": plan_name,
                        "plans": plans
                        }
                        plansList.append(planDict)

            operatorParametersList = []
            operator_parameters_queryset = OperatorParameter.objects(operatorId=operatorId,transactionAPIId=paymentGatewayId,status=1).order_by("sort")
            for each_operator_parameter in operator_parameters_queryset:
                operator_parameter_dict = fetching_operator_based_parameter_details(each_operator_parameter)
                operatorParametersList.append(operator_parameter_dict)

            data_status["responseStatus"]=1
            data_status["result"]="Prepaid recharge plans data fetched successfully!"
            data_status["prepaidRechargePlansList"]=plansList
            data_status["circleId"] = circle_data["circleId"]
            data_status["circleName"] = circle_data["circleName"]
            data_status["operatorId"] = payload["operatorId"]
            data_status["operatorName"] = payload["operatorName"]
            data_status["prepaidProviderId"]=prepaidProviderId
            data_status["operatorReferenceId"]=str(random_digit_generate(35))
            data_status["operatorParametersList"]=operatorParametersList
            return data_status
        else:
            data_status["result"]="Required fields are missing!!"
            return data_status
    except Exception as e:
        app.logger.error(traceback.format_exc())
        data_status["result"]="Unable to fetch prepaid circles data!!"
        return data_status

@bbps_frontend_apis.route("/get_operator_and_circle_by_mobile_number",methods=["POST"])
@encrypt_decrypt_before_login
def get_operator_and_circle_by_mobile_number():
    data_status = {"responseStatus":0,"result":""}
    try:
        try:
            data = request.decrypted_data
            userId = data.get("userId","")
            phoneNumber = data.get("phoneNumber","")
        except Exception as e:
            app.logger.error(traceback.format_exc())
            data_status["result"] = "Invalid Request"
            return data_status
        # userId = request.json.get("userId","")
        # phoneNumber = request.json.get("phoneNumber","")
        if userId and phoneNumber:
            user_queryset = Users.objects(id=userId).first()
            if not user_queryset:
                data_status["result"]="Invalid User Id!!"
                return data_status
            
            paymentGatewayId = None
            prepaid_api_queryset = PrepaidMobileSetup.objects(enablePrepaid=True,status=1,serviceType="Prepaid").first()
            if prepaid_api_queryset:
                paymentGatewayId = str(prepaid_api_queryset.prepaidProviderId.id)
            else:
                data_status["result"]="Service provider not available please contact to admin!!"
                return data_status

            transaction_api_queryset = TransactionAPI.objects(id=paymentGatewayId,status=1).first()
            if not transaction_api_queryset:
                data_status["result"]="Invalid paymentgateway id!!"
                return data_status

            print(transaction_api_queryset.code,"((((((((((transaction_api_queryset.code))))))))))")
            if transaction_api_queryset.code == "PayU_BBPS":
                client_id = ""
                client_secret = ""
                get_base_url = ""
                agentId = ""
                scope = ""
                grant_type = ""
                for each_key in transaction_api_queryset.paramsList:
                    get_key = each_key.get("key")
                    if get_key == "client_id":
                        client_id = each_key.get("value")
                    if get_key == "client_secret":
                        client_secret = each_key.get("value")
                    if get_key == "get_base_url":
                        get_base_url = each_key.get("value")
                    if get_key == "agentId":
                        agentId = each_key.get("value")
                    if get_key == "scope":
                        scope = each_key.get("value")
                    if get_key == "grant_type":
                        grant_type = each_key.get("value")

                try:
                    authtokenresp=dynamicBbpsAuthorizationToken(client_id,client_secret,scope,grant_type)
                    authorizationToken=authtokenresp.get('accessToken')
                    headers = {
                    "authorization": "Bearer "+str(authorizationToken)
                    }
                    print(headers,"headers")
                    payload = {
                    "agentId": agentId,
                    "customerPhoneNumber": phoneNumber
                    }
                    url = get_base_url+"getOperatorAndCircleInfo"
                    payuresponse = requests.post(url,json=payload,headers=headers)
                    print(payuresponse.text,"(((((((((((((payuresponse)))))))))))))")
                    payuresponseData = json.loads(payuresponse.text)
                except Exception as e:
                    app.logger.error(traceback.format_exc())
                    data_status["result"]="Service provider not available please contact to admin!!"
                    return data_status
            else:
                data_status["result"]="Service provider not available please contact to admin!!"
                return data_status

            data_status["responseStatus"]=1
            data_status["result"]="Prepaid circles data fetched successfully!"
            data_status["fetchCirclesByMobileNumber"]=payuresponseData
            return data_status
        else:
            data_status["result"]="Required fields are missing!!"
            return data_status
    except Exception as e:
        app.logger.error(traceback.format_exc())
        data_status["result"]="Unable to fetch prepaid circles data!!"
        return data_status

@bbps_frontend_apis.route("/mobile_prepaid_circles_and_operators_list",methods=["POST"])
@encrypt_decrypt_before_login
def mobile_prepaid_circles_list():
    data_status = {"responseStatus":0,"result":""}
    try:
        try:
            data = request.decrypted_data
            userId = data.get("userId","")
        except Exception as e:
            app.logger.error(traceback.format_exc())
            data_status["result"] = "Invalid Request"
            return data_status
        # userId=request.json.get("userId")
        if userId:
            user_queryset = Users.objects(id=userId).first()
            if not user_queryset:
                data_status["result"]="Invalid User Id!!"
                return data_status
            circlesList = []
            prepaid_circles_queryset = Circles.objects(status=1).order_by("-id")  
            for each_prepaid_circle in prepaid_circles_queryset:
                circleDict = {
                "id":str(each_prepaid_circle.id),
                "name":each_prepaid_circle.name,
                "circleRefId":each_prepaid_circle.circleRefId
                }
                if each_prepaid_circle.paymentGatewayId:
                    circleDict["paymentGatewayId"]=str(each_prepaid_circle.paymentGatewayId.id)
                    circleDict["paymentGatewayName"]=str(each_prepaid_circle.paymentGatewayId.apiName)
                else:
                    circleDict["paymentGatewayId"]=""
                    circleDict["paymentGatewayName"]=""
                circlesList.append(circleDict)

            operatorsList = []
            prepaid_operators_queryset = PrepaidOperators.objects(status=1).order_by("-id")
            for each_operator in prepaid_operators_queryset:
                operatorDict = {
                "id":str(each_operator.id),
                "name":each_operator.name,
                "operatorCode":each_operator.operatorCode
                }
                if each_operator.prepaidPgId:
                    operatorDict["prepaidPgId"]=str(each_operator.prepaidPgId.id)
                    operatorDict["prepaidPgName"]=str(each_operator.prepaidPgId.apiName)
                else:
                    operatorDict["prepaidPgId"]=""
                    operatorDict["prepaidPgName"]=""

                if each_operator.image:
                    operatorDict["image"]=domain+each_operator.image
                else:
                    operatorDict["image"]=""
                operatorsList.append(operatorDict)

            data_status["responseStatus"]=1
            data_status["circlesList"]=circlesList
            data_status["operatorsList"]=operatorsList
            data_status["result"]="Prepaid circles and operators data fetched successfully!"
            return data_status
        else:
            data_status["result"]="Required fields are missing!!"
            return data_status
    except Exception as e:
        app.logger.error(traceback.format_exc())
        data_status["result"]="Unable to fetch prepaid circles and operators data!!"
        return data_status