# # 3) Use it in your views/services
# # Replace direct imports with the factory:


# # in your recharge flow (views/services)
# from recharge.services.provider_factory import get_recharge_client

# client = get_recharge_client()

# # Mobile info
# info = client.mobile_info(mobile="9005464564")

# # Plans
# plans = client.recharge_plan(operator_code="AT", circle_code="23")

# # Recharge
# res = client.mobile_recharge(
#     txnid="AT12334304",
#     number="8918901273",
#     amount=10,
#     operator_code="JO",
#     circle_code="23",
# )

# # Status
# stat = client.status(txnid="AT12334304")

# # BBPS
# bf = client.bill_fetch(number="4024334467", operator_code="ADEM", txnid="G435646")
# bp = client.bill_pay(txnid="G5464", number="45657776", amount=10, operator_code="ADEM")

# # Complaint
# cmp = client.complaint(txnid="AT12334304", remark="Recharge Success but not received")


# # =======================================================

# # 4) (Optional) Normalizers
# # If you need uniform success flags/ids across providers, add a tiny helper:

# # recharge/services/normalizers.py
# def norm_status(v: str) -> str:
#     v = (v or "").strip().upper()
#     if v in {"SUCCESS"}: return "SUCCESS"
#     if v in {"PENDING", "PROCESSING"}: return "PENDING"
#     if v in {"FAILED"}: return "FAILED"
#     return "ERROR"

# def extract_ids(resp: dict) -> dict:
#     return {
#         "order_id": resp.get("orderId"),
#         "provider_txn_id": resp.get("RefId") or resp.get("optId"),
#         "client_txn_id": resp.get("TxnId") or resp.get("txnid"),
#     }
# # That’s it. Flip RECHARGE_PROVIDER in settings and your flow uses the other provider without touching business logic.
# # If your A1TopupAPI uses slightly different method names, tell me and I’ll stub a thin wrapper so it matches the
# # RechargeProvider Protocol exactly.

# recharge/views.py
from django.http import JsonResponse, HttpResponseBadRequest
from django.views.decorators.csrf import csrf_exempt
from django.utils import timezone
from recharge.models import RechargeTransaction  # adjust to your model

@csrf_exempt
def goterpay_callback(request):
    """
    Handles GoterPay callback like:
    ?Status=SUCCESS&TxnId=AT12334304&RefId=BR000BAQYOE7&optional=GooglePlay123
    """
    status = request.GET.get("Status")
    txn_id = request.GET.get("TxnId")
    ref_id = request.GET.get("RefId")
    optional_code = request.GET.get("optional")

    if not txn_id or not status:
        return HttpResponseBadRequest("Missing required parameters")

    # Normalize status (SUCCESS / FAILED / PENDING)
    status_normalized = status.strip().upper()

    # Update your transaction record
    try:
        tx = RechargeTransaction.objects.get(client_txn_id=txn_id)
        tx.status = status_normalized
        tx.operator_ref_id = ref_id
        tx.optional_code = optional_code
        tx.updated_at = timezone.now()
        tx.save()

        # Optional: trigger notifications to user
        # send_recharge_status_notification(tx.user, status_normalized)

    except RechargeTransaction.DoesNotExist:
        return HttpResponseBadRequest(f"Transaction not found: {txn_id}")

    return JsonResponse({"success": True, "message": "Callback processed"})
