File size: 51,294 Bytes
3533fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.responses import RedirectResponse
from sqlalchemy.orm import Session
from typing import List
import datetime
import logging
import hashlib
import hmac
import json

from app.db.session import get_db
from app.schemas import schemas
from app.core.dependencies import get_current_user
from app.models.models import User, Wallet, Transaction, PayoutRequest
from app.crud.monetization import get_or_create_wallet, process_tip
from app.core.config import PAYSTACK_SECRET_KEY, STRIPE_SECRET_KEY, STRIPE_WEBHOOK_SECRET, BASE_URL, FRONTEND_URL
import httpx
import stripe

logger = logging.getLogger(__name__)

router = APIRouter()

# ── Paystack integration URLs ──────────────────────────────────────────────
# These are computed once at startup so they are always consistent.
# Copy the values printed in startup logs and paste them into Paystack Dashboard.
#
#   Webhook URL  β†’ Paystack Dashboard β†’ Settings β†’ API Keys & Webhooks β†’ Webhook URL
#   Callback URL β†’ Paystack Dashboard β†’ Settings β†’ API Keys & Webhooks β†’ Callback URL
#
WEBHOOK_URL  = f"{BASE_URL}/api/v1/monetization/webhook/paystack"
CALLBACK_URL = f"{BASE_URL}/api/v1/monetization/payment/callback"

import logging as _log
_log.getLogger(__name__).info(
    f"\n" \
    f"  ╔══════════════════════════════════════════════════════════╗\n" \
    f"  β•‘         Paystack Integration URLs (paste in dashboard)  β•‘\n" \
    f"  ╠══════════════════════════════════════════════════════════╣\n" \
    f"  β•‘  Webhook URL : {WEBHOOK_URL:<42} β•‘\n" \
    f"  β•‘  Callback URL: {CALLBACK_URL:<42} β•‘\n" \
    f"  β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•"
)

@router.get("/wallet", response_model=schemas.Wallet)
def get_creator_wallet(
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """Fetch the authenticated user's wallet and transaction history."""
    return get_or_create_wallet(db, current_user.id)

@router.post("/tip/{user_id}", response_model=schemas.Transaction)
def send_tip(
    user_id: int,
    amount: float,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """Send a direct tip to a creator."""
    if amount <= 0:
        raise HTTPException(status_code=400, detail="Tip amount must be positive.")
    creator = db.query(User).filter(User.id == user_id).first()
    if not creator:
        raise HTTPException(status_code=404, detail="Creator not found")
    
    if creator.id == current_user.id:
        raise HTTPException(status_code=400, detail="You cannot tip yourself")

    result = process_tip(db, from_user_id=current_user.id, to_user_id=creator.id, amount=amount)
    if not result:
        raise HTTPException(status_code=400, detail="Insufficient wallet balance. Please top up your wallet.")
    return result

# ------------------------------------------------------------------ #
#  PAYOUT REQUEST ENDPOINTS
# ------------------------------------------------------------------ #

@router.post("/payout/request", response_model=schemas.PayoutRequestSchema)
def request_payout(
    payload: schemas.PayoutRequestCreate,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """
    Creator requests a payout from their wallet.
    Minimum payout: ₦1,000. Funds stay frozen until admin marks as completed.
    """
    wallet = get_or_create_wallet(db, current_user.id)
    balance = float(wallet.balance)

    MIN_PAYOUT = 1000.0
    if payload.amount < MIN_PAYOUT:
        raise HTTPException(status_code=400, detail=f"Minimum payout is ₦{MIN_PAYOUT:,.2f}")
    if payload.amount > balance:
        raise HTTPException(status_code=400, detail=f"Insufficient balance. Your balance is ₦{balance:,.2f}")

    # Block duplicate pending requests
    existing_pending = db.query(PayoutRequest).filter(
        PayoutRequest.user_id == current_user.id,
        PayoutRequest.status == "pending"
    ).first()
    if existing_pending:
        raise HTTPException(status_code=400, detail="You already have a pending payout request. Please wait for it to be processed.")

    payout = PayoutRequest(
        user_id=current_user.id,
        wallet_id=wallet.id,
        amount=payload.amount,
        bank_details=payload.bank_details,
        status="pending"
    )

    # Deduct from wallet immediately (held in escrow until approved)
    from decimal import Decimal
    wallet.balance -= Decimal(str(payload.amount))

    db.add(payout)
    db.commit()
    db.refresh(payout)
    return payout

@router.get("/payout/my-requests", response_model=List[schemas.PayoutRequestSchema])
def get_my_payout_requests(
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """List the authenticated user's payout history."""
    return db.query(PayoutRequest).filter(
        PayoutRequest.user_id == current_user.id
    ).order_by(PayoutRequest.requested_at.desc()).all()

# ------------------------------------------------------------------ #
#  ADMIN-ONLY PAYOUT MANAGEMENT
# ------------------------------------------------------------------ #

def _require_admin(current_user: User):
    if current_user.role != "admin":
        raise HTTPException(status_code=403, detail="Admin access required")

@router.get("/admin/payouts", response_model=List[schemas.PayoutRequestSchema])
def admin_list_payouts(
    status: str = None,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """Admin: list all payout requests, optionally filtered by status."""
    _require_admin(current_user)
    q = db.query(PayoutRequest)
    if status:
        q = q.filter(PayoutRequest.status == status)
    return q.order_by(PayoutRequest.requested_at.desc()).all()

@router.get("/admin/transactions")
def admin_list_transactions(
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """Admin: view all transactions including pending and failed."""
    _require_admin(current_user)
    return db.query(Transaction).order_by(Transaction.created_at.desc()).all()

@router.put("/admin/payouts/{payout_id}", response_model=schemas.PayoutRequestSchema)
def admin_update_payout(
    payout_id: int,
    status: str,
    admin_note: str = None,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """Admin: approve, reject, or mark a payout as completed."""
    _require_admin(current_user)
    payout = db.query(PayoutRequest).filter(PayoutRequest.id == payout_id).first()
    if not payout:
        raise HTTPException(status_code=404, detail="Payout request not found")

    valid_statuses = {"pending", "processing", "completed", "rejected"}
    if status not in valid_statuses:
        raise HTTPException(status_code=400, detail=f"Status must be one of: {valid_statuses}")

    # If rejected, refund the escrowed amount back to wallet
    if status == "rejected" and payout.status != "rejected":
        wallet = db.query(Wallet).filter(Wallet.id == payout.wallet_id).first()
        if wallet:
            from decimal import Decimal
            wallet.balance += Decimal(str(payout.amount))

    payout.status = status
    payout.admin_note = admin_note
    payout.processed_at = datetime.datetime.now()
    db.commit()
    db.refresh(payout)
    return payout

def get_or_create_stripe_price(price_amount: float, is_yearly: bool) -> str:
    """Helper to dynamically find or create a Price ID in Stripe for the subscription."""
    stripe.api_key = STRIPE_SECRET_KEY
    price_cents = int(price_amount * 100)
    interval = "year" if is_yearly else "month"
    product_name = f"Monteeq Pro {'Yearly' if is_yearly else 'Monthly'}"
    
    try:
        prices = stripe.Price.list(active=True, limit=100)
        for p in prices.auto_paging_iter():
            if p.unit_amount == price_cents and p.recurring and p.recurring.interval == interval:
                try:
                    prod = stripe.Product.retrieve(p.product)
                    if "Monteeq Pro" in prod.name:
                        return p.id
                except Exception:
                    pass
    except Exception as e:
        logger.warning(f"Error listing Stripe prices: {str(e)}")
        
    try:
        product = stripe.Product.create(
            name=product_name,
            description="Access to premium creator features including ad-free, 4K rendering, gold challenges.",
        )
        price = stripe.Price.create(
            unit_amount=price_cents,
            currency="usd",
            recurring={"interval": interval},
            product=product.id,
        )
        return price.id
    except Exception as e:
        logger.error(f"Error creating Stripe product/price: {str(e)}")
        raise e

@router.get("/pro/pricing", response_model=schemas.ProPricingResponse)
def get_pro_pricing():
    """Returns the current pricing for Monteeq Pro."""
    return {"monthly_price": 4.99, "yearly_price": 53.89}

@router.post("/subscriptions/create", response_model=schemas.SubscriptionCreateResponse)
def create_subscription(
    payload: schemas.SubscriptionCreateRequest,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """
    Creates or retrieves a Stripe Customer, creates a subscription,
    and returns the client secret of the payment intent to complete verification on the frontend.
    """
    if current_user.subscription_tier == "pro" and current_user.subscription_status == "active":
        raise HTTPException(status_code=400, detail="You already have an active subscription")

    stripe.api_key = STRIPE_SECRET_KEY
    
    customer_id = current_user.stripe_customer_id
    if not customer_id:
        try:
            customer = stripe.Customer.create(
                email=payload.billing_email,
                name=payload.full_name,
                metadata={
                    "user_id": str(current_user.id),
                    "billing_country": payload.billing_country,
                    "billing_zip": payload.billing_zip
                }
            )
            customer_id = customer.id
            current_user.stripe_customer_id = customer_id
            db.commit()
        except Exception as e:
            logger.error(f"Failed to create Stripe customer: {str(e)}")
            raise HTTPException(status_code=500, detail=f"Failed to configure customer profile: {str(e)}")
    else:
        try:
            stripe.Customer.modify(
                customer_id,
                email=payload.billing_email,
                name=payload.full_name,
                metadata={
                    "user_id": str(current_user.id),
                    "billing_country": payload.billing_country,
                    "billing_zip": payload.billing_zip
                }
            )
        except Exception as e:
            logger.warning(f"Failed to update Stripe customer: {str(e)}")

    price_amount = 53.89 if payload.is_yearly else 4.99
    try:
        price_id = get_or_create_stripe_price(price_amount, payload.is_yearly)
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Failed to retrieve subscription plan price: {str(e)}")

    try:
        if current_user.subscription_id:
            try:
                existing_sub = stripe.Subscription.retrieve(current_user.subscription_id)
                if existing_sub.status in ["incomplete", "incomplete_expired"]:
                    stripe.Subscription.cancel(current_user.subscription_id)
            except Exception:
                pass

        # Support both new Basil API version (2025-03-31+) and legacy versions dynamically
        try:
            subscription = stripe.Subscription.create(
                customer=customer_id,
                items=[{"price": price_id}],
                payment_behavior="default_incomplete",
                payment_settings={"save_default_payment_method": "on_subscription"},
                expand=["latest_invoice.confirmation_secret"],
                metadata={
                    "user_id": str(current_user.id),
                    "payment_type": "pro_subscription",
                    "is_yearly": str(payload.is_yearly)
                }
            )
            client_secret = subscription.latest_invoice.confirmation_secret.client_secret
        except Exception as e:
            # If the Stripe API version is legacy, confirmation_secret expansion will fail.
            # We then fall back to the legacy payment_intent expansion.
            if "confirmation_secret" in str(e) or "Invalid expand" in str(e) or "attribute" in str(e):
                logger.info("Falling back to legacy Stripe payment_intent expansion...")
                subscription = stripe.Subscription.create(
                    customer=customer_id,
                    items=[{"price": price_id}],
                    payment_behavior="default_incomplete",
                    payment_settings={"save_default_payment_method": "on_subscription"},
                    expand=["latest_invoice.payment_intent"],
                    metadata={
                        "user_id": str(current_user.id),
                        "payment_type": "pro_subscription",
                        "is_yearly": str(payload.is_yearly)
                    }
                )
                client_secret = subscription.latest_invoice.payment_intent.client_secret
            else:
                raise e
        
        subscription_id = subscription.id
        
        current_user.subscription_id = subscription_id
        current_user.subscription_status = "incomplete"
        current_user.subscription_tier = "pro"
        db.commit()
        
    except Exception as e:
        logger.error(f"Failed to create Stripe subscription: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Failed to initialize subscription: {str(e)}")
        
    return {
        "status": "success",
        "subscriptionId": subscription_id,
        "clientSecret": client_secret
    }

@router.post("/subscriptions/verify")
def verify_subscription(
    payload: schemas.SubscriptionVerifyRequest,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """
    Direct client-side verification endpoint.
    Retrieves the subscription state from Stripe and upgrades the user tier immediately in the DB.
    Useful as a fallback or immediate confirmation path before the webhook arrives.
    """
    stripe.api_key = STRIPE_SECRET_KEY
    try:
        subscription = stripe.Subscription.retrieve(payload.subscription_id)
    except Exception as e:
        logger.error(f"Failed to retrieve subscription from Stripe: {str(e)}")
        raise HTTPException(status_code=400, detail="Invalid subscription ID")

    # Check if the subscription belongs to this user or customer
    customer_id = subscription.get("customer")
    if customer_id != current_user.stripe_customer_id:
        raise HTTPException(status_code=403, detail="Subscription does not belong to this user")

    status = subscription.get("status")
    if status in ["active", "trialing"]:
        from datetime import datetime as dt, timezone as tz
        subscription_id = subscription.get("id")
        current_user.subscription_id = subscription_id
        current_user.subscription_status = status
        current_user.subscription_tier = "pro"
        current_user.is_premium = True
        
        current_period_end = subscription.get("current_period_end")
        if current_period_end:
            current_user.current_period_end = dt.fromtimestamp(current_period_end, tz.utc)
        
        # Check if transaction already logged
        wallet = get_or_create_wallet(db, current_user.id)
        existing_tx = db.query(Transaction).filter(
            Transaction.wallet_id == wallet.id,
            Transaction.reference_id == subscription_id,
            Transaction.status == "success"
        ).first()
        
        if not existing_tx:
            amount = 4.99 # Fallback amount
            # Try to get price from first item
            try:
                price = subscription["items"]["data"][0]["price"]
                amount = (price["unit_amount"] or 0) / 100
            except Exception:
                pass
                
            db.add(Transaction(
                wallet_id=wallet.id,
                amount=amount,
                transaction_type="pro_subscription",
                status="success",
                reference_id=subscription_id,
                description=f"Monteeq Pro Subscription Verified (Immediate fallback, USD {amount:.2f})"
            ))
            
        db.commit()
        logger.info(f"Stripe verified Pro Upgrade via client verify. User: {current_user.id}")
        return {"status": "success", "message": "Subscription verified and upgraded successfully!", "is_premium": True}
    else:
        logger.warning(f"Subscription status is not active: {status}")
        return {"status": "incomplete", "message": f"Subscription status is {status}", "is_premium": False}

@router.get("/subscription/status", response_model=schemas.SubscriptionStatusResponse)
def get_subscription_status(
    current_user: User = Depends(get_current_user)
):
    """
    Returns the subscription tier, status, current period end date, and stripe customer id for the user.
    """
    cancel_at_period_end = False
    if current_user.subscription_id:
        try:
            stripe.api_key = STRIPE_SECRET_KEY
            sub = stripe.Subscription.retrieve(current_user.subscription_id)
            cancel_at_period_end = sub.get("cancel_at_period_end", False)
        except Exception:
            pass

    return {
        "status": current_user.subscription_status or "inactive",
        "tier": current_user.subscription_tier or "free",
        "current_period_end": current_user.current_period_end,
        "stripe_customer_id": current_user.stripe_customer_id,
        "cancel_at_period_end": cancel_at_period_end
    }

@router.post("/subscriptions/auto-renew")
def toggle_subscription_auto_renew(
    payload: schemas.SubscriptionAutoRenewRequest,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """
    Toggles the cancel_at_period_end flag on Stripe to enable/disable auto-renew.
    """
    if not current_user.subscription_id:
        raise HTTPException(status_code=400, detail="No active subscription found")

    stripe.api_key = STRIPE_SECRET_KEY
    try:
        sub = stripe.Subscription.modify(
            current_user.subscription_id,
            cancel_at_period_end=not payload.auto_renew
        )
        return {
            "status": "success",
            "auto_renew": payload.auto_renew,
            "message": "Auto-renewal status updated successfully"
        }
    except Exception as e:
        logger.error(f"Failed to toggle auto-renew: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Failed to update auto-renewal: {str(e)}")

@router.post("/subscriptions/cancel")
def cancel_subscription(
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """
    Cancels the user's subscription immediately in Stripe and updates database.
    """
    if not current_user.subscription_id:
        raise HTTPException(status_code=400, detail="No active subscription found")

    stripe.api_key = STRIPE_SECRET_KEY
    try:
        stripe.Subscription.cancel(current_user.subscription_id)
        
        # Downgrade user in DB
        current_user.subscription_status = "canceled"
        current_user.subscription_tier = "free"
        current_user.is_premium = False
        db.commit()
        
        return {
            "status": "success",
            "message": "Subscription canceled successfully."
        }
    except Exception as e:
        logger.error(f"Failed to cancel subscription: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Failed to cancel subscription: {str(e)}")

@router.post("/customer-portal", response_model=schemas.CustomerPortalResponse)
def create_customer_portal(
    current_user: User = Depends(get_current_user)
):
    """
    Generates a Stripe Billing Customer Portal link and returns it.
    """
    if not current_user.stripe_customer_id:
        raise HTTPException(status_code=400, detail="You do not have a billing history or active customer profile yet")
        
    stripe.api_key = STRIPE_SECRET_KEY
    try:
        portal_session = stripe.billing_portal.Session.create(
            customer=current_user.stripe_customer_id,
            return_url=f"{FRONTEND_URL}/pro"
        )
        return {"url": portal_session.url}
    except Exception as e:
        logger.error(f"Failed to create Stripe Billing Portal session: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Failed to open billing settings: {str(e)}")

import uuid
@router.post("/pro/initialize", response_model=schemas.PaymentInitializeResponse)
def initialize_pro_subscription(
    payload: schemas.PaymentInitialize,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """
    Initializes a Stripe checkout session for Pro Subscription
    and saves the pending transaction in the database.
    """
    if current_user.is_premium:
        raise HTTPException(status_code=400, detail="You are already a Pro member")
    
    wallet = get_or_create_wallet(db, current_user.id)
    
    amount = 26400.0 if payload.is_yearly else 2500.0
    stripe_amount_cents = int(amount * 100)
    
    try:
        stripe.api_key = STRIPE_SECRET_KEY
        
        checkout_session = stripe.checkout.Session.create(
            payment_method_types=['card'],
            line_items=[{
                'price_data': {
                    'currency': 'ngn',
                    'product_data': {
                        'name': f"Monteeq Pro {'Yearly' if payload.is_yearly else 'Monthly'}",
                        'description': "Access to premium video rendering (4K), Gold Challenges, and 0% commission.",
                    },
                    'unit_amount': stripe_amount_cents,
                },
                'quantity': 1,
            }],
            mode='payment',
            success_url=f"{FRONTEND_URL}/payment?status=success&reference={{CHECKOUT_SESSION_ID}}",
            cancel_url=f"{FRONTEND_URL}/payment?status=cancelled&reference={{CHECKOUT_SESSION_ID}}",
            metadata={
                "user_id": str(current_user.id),
                "payment_type": "pro_subscription",
                "is_yearly": str(payload.is_yearly)
            }
        )
        reference = checkout_session.id
        checkout_url = checkout_session.url
    except Exception as e:
        logger.error(f"Failed to create Stripe Checkout Session: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Failed to initialize payment gateway: {str(e)}")
    
    transaction = Transaction(
        wallet_id=wallet.id,
        amount=amount,
        transaction_type='pro_subscription',
        status='pending',
        reference_id=reference,
        description=f"Monteeq Pro {'Yearly' if payload.is_yearly else 'Monthly'} Subscription Upgrade (Pending - Stripe)"
    )
    db.add(transaction)
    db.commit()
    db.refresh(transaction)
    
    return {"reference": reference, "status": "success", "checkout_url": checkout_url}

@router.post("/verify-pro", response_model=schemas.ProUpgradeResponse)
async def verify_pro_subscription(
    payload: schemas.PaymentVerify,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """
    Verify a payment transaction reference (Stripe or Paystack) and upgrade user to Pro.
    """
    if current_user.is_premium:
        return {"status": "success", "message": "You are already a Pro member!", "is_premium": True}

    is_stripe = payload.reference.startswith("cs_")

    if is_stripe:
        try:
            stripe.api_key = STRIPE_SECRET_KEY
            session = stripe.checkout.Session.retrieve(payload.reference)
        except Exception as e:
            raise HTTPException(status_code=500, detail=f"Stripe verification failed: {str(e)}")

        if session.payment_status != "paid":
            logger.error(f"Stripe verification failed: {session}")
            existing = db.query(Transaction).filter(Transaction.reference_id == payload.reference).first()
            if existing and existing.status == "pending":
                existing.status = "failed"
                db.commit()
            raise HTTPException(status_code=400, detail="Payment verification failed: Session is unpaid")

        logger.info(f"Stripe verification success for reference: {payload.reference}")
        amount = (session.amount_total or 0) / 100
        is_yearly = session.metadata.get("is_yearly") == "True"
    else:
        # Verify with Paystack
        async with httpx.AsyncClient() as client:
            try:
                response = await client.get(
                    f"https://api.paystack.co/transaction/verify/{payload.reference}",
                    headers={"Authorization": f"Bearer {PAYSTACK_SECRET_KEY}"},
                    timeout=10.0
                )
                data = response.json()
            except Exception as e:
                raise HTTPException(status_code=500, detail=f"Paystack verification failed: {str(e)}")

        if not data.get("status") or data.get("data", {}).get("status") != "success":
            logger.error(f"Paystack verification failed: {data}")
            existing = db.query(Transaction).filter(Transaction.reference_id == payload.reference).first()
            if existing and existing.status == "pending":
                existing.status = "failed"
                db.commit()
            error_msg = data.get("data", {}).get("gateway_response") or data.get("message", "Unknown error")
            raise HTTPException(status_code=400, detail=f"Payment verification failed: {error_msg}")

        logger.info(f"Paystack verification success for reference: {payload.reference}")
        try:
            amount = data["data"]["amount"] / 100
        except (KeyError, TypeError):
            amount = 0.0
            logger.error(f"Could not extract amount for pro verify ref={payload.reference}")
        is_yearly = amount >= 20000

    # 3. Upgrade User
    from datetime import datetime, timedelta, timezone
    current_user.is_premium = True
    if is_yearly:
        current_user.premium_expires_at = datetime.now(timezone.utc) + timedelta(days=365)
    else:
        current_user.premium_expires_at = datetime.now(timezone.utc) + timedelta(days=30)
    
    # 4. Record Transaction (Idempotency)
    existing = db.query(Transaction).filter(Transaction.reference_id == payload.reference).first()
    if existing:
        if existing.status != "success":
            existing.status = "success"
            existing.amount = amount
    else:
        wallet = get_or_create_wallet(db, current_user.id)
        transaction = Transaction(
            wallet_id=wallet.id,
            amount=amount,
            transaction_type='pro_subscription',
            status='success',
            reference_id=payload.reference,
            description="Monteeq Pro Subscription Upgrade"
        )
        db.add(transaction)
        
    db.commit()
    db.refresh(current_user)

    logger.info(f"User {current_user.id} upgraded to premium. is_premium: {current_user.is_premium}")

    return {
        "status": "success",
        "message": "Welcome to Monteeq Pro! Your account has been upgraded.",
        "is_premium": True
    }


@router.post("/deposit/verify", response_model=schemas.Transaction)
async def verify_deposit_and_fund(
    payload: schemas.PaymentVerify,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """
    Verify a transaction reference (Stripe or Paystack) and fund the user's wallet or upgrade to Pro.
    """
    is_stripe = payload.reference.startswith("cs_")

    if is_stripe:
        try:
            stripe.api_key = STRIPE_SECRET_KEY
            session = stripe.checkout.Session.retrieve(payload.reference)
        except Exception as e:
            logger.error(f"Stripe API error for ref {payload.reference}: {str(e)}")
            raise HTTPException(status_code=500, detail=f"Stripe verification failed: {str(e)}")

        if session.payment_status != "paid":
            logger.warning(f"Stripe verification failed for ref {payload.reference}: session unpaid")
            raise HTTPException(status_code=400, detail="Payment verification failed: Session is unpaid")

        amount = (session.amount_total or 0) / 100
        payment_type = session.metadata.get("payment_type", "deposit")

        wallet = get_or_create_wallet(db, current_user.id)

        if payment_type == "pro_subscription":
            # Upgrade user to Pro
            is_yearly = session.metadata.get("is_yearly") == "True"
            from datetime import datetime, timedelta, timezone
            current_user.is_premium = True
            if is_yearly:
                current_user.premium_expires_at = datetime.now(timezone.utc) + timedelta(days=365)
            else:
                current_user.premium_expires_at = datetime.now(timezone.utc) + timedelta(days=30)

            # Record / update transaction
            existing = db.query(Transaction).filter(Transaction.reference_id == payload.reference).first()
            if existing:
                if existing.status != "success":
                    existing.status = "success"
                    existing.amount = amount
                transaction = existing
            else:
                transaction = Transaction(
                    wallet_id=wallet.id,
                    amount=amount,
                    transaction_type='pro_subscription',
                    status='success',
                    reference_id=payload.reference,
                    description="Monteeq Pro Subscription Upgrade"
                )
                db.add(transaction)
            db.commit()
            db.refresh(transaction)
            logger.info(f"Stripe verified Pro Upgrade via deposit verify. User: {current_user.id}")
            return transaction
        else:
            # Wallet deposit
            existing = db.query(Transaction).filter(Transaction.reference_id == payload.reference).first()
            if existing:
                return existing
            from decimal import Decimal
            wallet.balance += Decimal(str(amount))
            transaction = Transaction(
                wallet_id=wallet.id,
                amount=amount,
                transaction_type='deposit',
                reference_id=payload.reference,
                description="Wallet Top-up via Stripe"
            )
            db.add(transaction)
            db.add(wallet)
            db.commit()
            db.refresh(transaction)
            logger.info(f"Stripe verified Wallet Deposit via deposit verify. User: {current_user.id}, amount: {amount}")
            return transaction

    else:
        # Paystack verification flow
        async with httpx.AsyncClient() as client:
            try:
                response = await client.get(
                    f"https://api.paystack.co/transaction/verify/{payload.reference}",
                    headers={"Authorization": f"Bearer {PAYSTACK_SECRET_KEY}"},
                    timeout=10.0
                )
                data = response.json()
                logger.info(f"Paystack verification response for ref {payload.reference}: {data}")
            except Exception as e:
                logger.error(f"Paystack API error for ref {payload.reference}: {str(e)}")
                raise HTTPException(status_code=500, detail=f"Paystack verification failed: {str(e)}")

        if not data.get("status") or data.get("data", {}).get("status") != "success":
            logger.warning(f"Paystack verification failed for ref {payload.reference}: {data}")
            error_msg = data.get('data', {}).get('gateway_response') or data.get('message', 'Unknown error')
            raise HTTPException(status_code=400, detail=f"Payment verification failed: {error_msg}")

        try:
            amount = data["data"]["amount"] / 100  # Convert kobo to NGN
        except (KeyError, TypeError):
            logger.error(f"Failed to extract amount from Paystack response for ref {payload.reference}: {data}")
            raise HTTPException(status_code=500, detail="Invalid data received from Paystack")
        
        # Check if Paystack metadata says pro_subscription
        meta = data.get("data", {}).get("metadata") or {}
        payment_type = meta.get("payment_type", "deposit")

        if payment_type == "pro_subscription":
            wallet = get_or_create_wallet(db, current_user.id)
            is_yearly = meta.get("billing_cycle") == "yearly" or amount >= 20000
            from datetime import datetime, timedelta, timezone
            current_user.is_premium = True
            if is_yearly:
                current_user.premium_expires_at = datetime.now(timezone.utc) + timedelta(days=365)
            else:
                current_user.premium_expires_at = datetime.now(timezone.utc) + timedelta(days=30)
            
            existing = db.query(Transaction).filter(Transaction.reference_id == payload.reference).first()
            if existing:
                if existing.status != "success":
                    existing.status = "success"
                    existing.amount = amount
                transaction = existing
            else:
                transaction = Transaction(
                    wallet_id=wallet.id,
                    amount=amount,
                    transaction_type='pro_subscription',
                    status='success',
                    reference_id=payload.reference,
                    description="Monteeq Pro Subscription Upgrade"
                )
                db.add(transaction)
            db.commit()
            db.refresh(transaction)
            return transaction
        else:
            from app.crud.monetization import verify_deposit
            transaction = verify_deposit(db, user_id=current_user.id, amount=amount, reference=payload.reference)
            logger.info(f"Deposit verified OK. Transaction ID: {transaction.id}")
            return transaction



# ================================================================== #
#  PAYSTACK WEBHOOK                                                    #
#  POST /api/v1/monetization/webhook/paystack                         #
#                                                                      #
#  Register in Paystack Dashboard:                                     #
#    Settings β†’ API Keys & Webhooks β†’ Webhook URL                      #
# ================================================================== #

@router.post("/webhook/paystack", status_code=200)
async def paystack_webhook(request: Request, db: Session = Depends(get_db)):
    """
    Server-side Paystack event receiver (HMAC-SHA512 verified).

    Supported events:
      - charge.success β†’ pro_subscription upgrade OR wallet deposit

    The frontend must pass these fields in Paystack's metadata when
    initialising a transaction so the webhook can route correctly:
      metadata: { user_id: <int>, payment_type: "pro_subscription" | "deposit" }
    """
    # ── 1. Read raw body BEFORE any parsing (required for valid HMAC) ──
    raw_body = await request.body()

    # ── 2. Verify Paystack HMAC-SHA512 signature ────────────────────────
    paystack_sig = request.headers.get("x-paystack-signature", "")
    expected_sig = hmac.new(
        PAYSTACK_SECRET_KEY.encode("utf-8"),
        raw_body,
        hashlib.sha512
    ).hexdigest()

    if not hmac.compare_digest(paystack_sig, expected_sig):
        logger.warning("Paystack webhook: invalid HMAC signature β€” request rejected")
        raise HTTPException(status_code=401, detail="Invalid webhook signature")

    # ── 3. Parse event payload ───────────────────────────────────────────
    try:
        event = json.loads(raw_body)
    except Exception:
        raise HTTPException(status_code=400, detail="Invalid JSON payload")

    event_type = event.get("event")
    data       = event.get("data", {})
    reference  = data.get("reference")
    meta       = data.get("metadata") or {}
    logger.info(f"Paystack webhook received: event={event_type}, ref={reference}")

    # ── 4. Handle charge.success ─────────────────────────────────────────
    if event_type == "charge.success" and data.get("status") == "success":

        # Idempotency guard β€” skip if already processed
        existing = db.query(Transaction).filter(
            Transaction.reference_id == reference
        ).first()
        if existing:
            logger.info(f"Paystack webhook: ref {reference} already processed, skipping")
            return {"status": "ok", "message": "already_processed"}

        amount_ngn   = data.get("amount", 0) / 100  # kobo β†’ NGN
        payment_type = meta.get("payment_type", "deposit")
        user_id      = meta.get("user_id")

        # Fallback: resolve user by customer email if metadata missing user_id
        if not user_id:
            customer_email = data.get("customer", {}).get("email")
            if customer_email:
                u = db.query(User).filter(User.email == customer_email).first()
                user_id = u.id if u else None

        if not user_id:
            logger.error(f"Paystack webhook: cannot determine user for ref={reference}")
            # Return 200 so Paystack stops retrying β€” nothing we can do without a user
            return {"status": "ok", "message": "user_not_found"}

        user = db.query(User).filter(User.id == int(user_id)).first()
        if not user:
            logger.error(f"Paystack webhook: user_id={user_id} not in DB for ref={reference}")
            return {"status": "ok", "message": "user_not_found"}

        wallet = get_or_create_wallet(db, user.id)

        if payment_type == "pro_subscription":
            from datetime import datetime, timedelta, timezone
            if not user.is_premium:
                user.is_premium = True
            
            if amount_ngn >= 20000:
                user.premium_expires_at = datetime.now(timezone.utc) + timedelta(days=365)
            else:
                user.premium_expires_at = datetime.now(timezone.utc) + timedelta(days=30)
            
            existing_tx = db.query(Transaction).filter(Transaction.reference_id == reference).first()
            if existing_tx:
                if existing_tx.status != "success":
                    existing_tx.status = "success"
                    existing_tx.amount = amount_ngn
            else:
                db.add(Transaction(
                    wallet_id=wallet.id,
                    amount=amount_ngn,
                    transaction_type="pro_subscription",
                    status="success",
                    reference_id=reference,
                    description="Monteeq Pro Subscription (Paystack webhook)"
                ))
            db.commit()
            logger.info(f"Paystack webhook: user {user.id} upgraded to Pro via ref={reference}")

        else:  # wallet deposit (default)
            from decimal import Decimal
            wallet.balance += Decimal(str(amount_ngn))
            db.add(Transaction(
                wallet_id=wallet.id,
                amount=amount_ngn,
                transaction_type="deposit",
                reference_id=reference,
                description=f"Wallet top-up via Paystack (₦{amount_ngn:,.2f})"
            ))
            db.commit()
            logger.info(f"Paystack webhook: ₦{amount_ngn:,.2f} credited to user {user.id} wallet, ref={reference}")

    else:
        logger.info(f"Paystack webhook: unhandled event type '{event_type}' β€” ignored")

    # Always return 200 to stop Paystack from retrying
    return {"status": "ok"}


# ================================================================== #
#  PAYSTACK CALLBACK / FALLBACK URL                                    #
#  GET /api/v1/monetization/payment/callback                          #
#                                                                      #
#  Register in Paystack Dashboard:                                     #
#    Settings β†’ API Keys & Webhooks β†’ Callback URL                     #
# ================================================================== #

@router.get("/payment/callback")
async def paystack_callback(
    reference: str = None,
    trxref: str = None,
):
    """
    Browser redirect fallback after Paystack hosted-page checkout.

    Paystack appends ?reference=xxx&trxref=xxx to this URL after the
    user completes or cancels payment on the hosted checkout page.
    We redirect them to the frontend /payment page with the reference
    so the UI can call /deposit/verify or /verify-pro to confirm.
    """
    ref = reference or trxref
    frontend = FRONTEND_URL.rstrip("/")

    if not ref:
        redirect_target = f"{frontend}/payment?status=cancelled"
    else:
        redirect_target = f"{frontend}/payment?status=pending&reference={ref}"

    logger.info(f"Paystack callback: ref={ref} β†’ {redirect_target}")
    return RedirectResponse(url=redirect_target, status_code=302)


# ================================================================== #
#  STRIPE WEBHOOK                                                      #
#  POST /api/v1/monetization/webhook/stripe                           #
# ================================================================== #

@router.post("/webhook/stripe", status_code=200)
async def stripe_webhook(request: Request, db: Session = Depends(get_db)):
    """
    Server-side Stripe event receiver (verified using signature).
    """
    from datetime import datetime as dt, timezone as tz, timedelta
    
    payload = await request.body()
    sig_header = request.headers.get("stripe-signature")
    
    if not sig_header:
        logger.warning("Stripe webhook: missing stripe-signature header")
        raise HTTPException(status_code=400, detail="Missing stripe-signature header")
        
    try:
        stripe.api_key = STRIPE_SECRET_KEY
        event = stripe.Webhook.construct_event(
            payload, sig_header, STRIPE_WEBHOOK_SECRET
        )
    except ValueError as e:
        logger.warning(f"Stripe webhook: invalid payload: {str(e)}")
        raise HTTPException(status_code=400, detail="Invalid payload")
    except stripe.signature.SignatureVerificationError as e:
        logger.warning(f"Stripe webhook: signature verification failed: {str(e)}")
        raise HTTPException(status_code=400, detail="Invalid signature")

    event_type = event.get("type")
    logger.info(f"Stripe webhook received: event={event_type}")

    # Handle subscription events
    if event_type in ["customer.subscription.created", "customer.subscription.updated"]:
        subscription = event.get("data", {}).get("object", {})
        subscription_id = subscription.get("id")
        customer_id = subscription.get("customer")
        status = subscription.get("status")
        current_period_end_timestamp = subscription.get("current_period_end")
        
        user = db.query(User).filter(User.stripe_customer_id == customer_id).first()
        if user:
            user.subscription_id = subscription_id
            user.subscription_status = status
            if current_period_end_timestamp:
                user.current_period_end = dt.fromtimestamp(current_period_end_timestamp, tz.utc)
            
            if status == "active":
                user.subscription_tier = "pro"
                user.is_premium = True
            elif status in ["unpaid", "canceled", "incomplete_expired"]:
                user.is_premium = False
            db.commit()
            logger.info(f"Stripe webhook: updated subscription {subscription_id} for user {user.id} to status={status}")

    elif event_type == "customer.subscription.deleted":
        subscription = event.get("data", {}).get("object", {})
        subscription_id = subscription.get("id")
        user = db.query(User).filter(User.subscription_id == subscription_id).first()
        if user:
            user.subscription_status = "canceled"
            user.is_premium = False
            db.commit()
            logger.info(f"Stripe webhook: deleted subscription {subscription_id} for user {user.id}")

    elif event_type == "invoice.paid":
        invoice = event.get("data", {}).get("object", {})
        subscription_id = invoice.get("subscription")
        if subscription_id:
            user = db.query(User).filter(User.subscription_id == subscription_id).first()
            if user:
                user.subscription_status = "active"
                user.subscription_tier = "pro"
                user.is_premium = True
                
                amount = (invoice.get("amount_paid") or 0) / 100
                wallet = get_or_create_wallet(db, user.id)
                db.add(Transaction(
                    wallet_id=wallet.id,
                    amount=amount,
                    transaction_type="pro_subscription",
                    status="success",
                    reference_id=invoice.get("charge") or subscription_id,
                    description=f"Monteeq Pro Subscription Invoice Paid (Stripe webhook, USD {amount:.2f})"
                ))
                db.commit()
                logger.info(f"Stripe webhook: paid invoice for subscription {subscription_id}, user {user.id}")

    elif event_type == "invoice.payment_failed":
        invoice = event.get("data", {}).get("object", {})
        subscription_id = invoice.get("subscription")
        if subscription_id:
            user = db.query(User).filter(User.subscription_id == subscription_id).first()
            if user:
                user.subscription_status = "past_due"
                user.is_premium = False
                
                amount = (invoice.get("amount_due") or 0) / 100
                wallet = get_or_create_wallet(db, user.id)
                db.add(Transaction(
                    wallet_id=wallet.id,
                    amount=amount,
                    transaction_type="pro_subscription",
                    status="failed",
                    reference_id=invoice.get("id") or subscription_id,
                    description=f"Monteeq Pro Subscription Invoice Payment Failed (Stripe webhook, USD {amount:.2f})"
                ))
                db.commit()
                logger.warning(f"Stripe webhook: payment failed invoice for subscription {subscription_id}, user {user.id}")

    elif event_type == "checkout.session.completed":
        session = event.get("data", {}).get("object", {})
        reference = session.get("id") # Stripe Checkout Session ID
        metadata = session.get("metadata", {})
        
        # Idempotency guard
        existing = db.query(Transaction).filter(Transaction.reference_id == reference).first()
        if existing and existing.status == "success":
            logger.info(f"Stripe webhook: ref {reference} already processed, skipping")
            return {"status": "ok"}
            
        user_id = metadata.get("user_id")
        payment_type = metadata.get("payment_type", "deposit")
        is_yearly = metadata.get("is_yearly") == "True"
        amount = (session.get("amount_total") or 0) / 100
        
        if not user_id:
            logger.error(f"Stripe webhook: missing user_id in metadata for session={reference}")
            return {"status": "ok", "message": "user_id_missing"}
            
        user = db.query(User).filter(User.id == int(user_id)).first()
        if not user:
            logger.error(f"Stripe webhook: user {user_id} not found in DB")
            return {"status": "ok", "message": "user_not_found"}
            
        wallet = get_or_create_wallet(db, user.id)
        
        if payment_type == "pro_subscription":
            user.is_premium = True
            user.subscription_tier = "pro"
            user.subscription_status = "active"
            if is_yearly:
                user.premium_expires_at = dt.now(tz.utc) + timedelta(days=365)
                user.current_period_end = dt.now(tz.utc) + timedelta(days=365)
            else:
                user.premium_expires_at = dt.now(tz.utc) + timedelta(days=30)
                user.current_period_end = dt.now(tz.utc) + timedelta(days=30)
                
            existing_tx = db.query(Transaction).filter(Transaction.reference_id == reference).first()
            if existing_tx:
                if existing_tx.status != "success":
                    existing_tx.status = "success"
                    existing_tx.amount = amount
            else:
                db.add(Transaction(
                    wallet_id=wallet.id,
                    amount=amount,
                    transaction_type="pro_subscription",
                    status="success",
                    reference_id=reference,
                    description="Monteeq Pro Subscription (Stripe Checkout webhook)"
                ))
            db.commit()
            logger.info(f"Stripe webhook: user {user.id} upgraded to Pro via reference={reference}")
        else:
            # Wallet deposit
            from decimal import Decimal
            existing_tx = db.query(Transaction).filter(Transaction.reference_id == reference).first()
            if existing_tx:
                if existing_tx.status != "success":
                    existing_tx.status = "success"
                    wallet.balance += Decimal(str(amount))
            else:
                wallet.balance += Decimal(str(amount))
                db.add(Transaction(
                    wallet_id=wallet.id,
                    amount=amount,
                    transaction_type="deposit",
                    status="success",
                    reference_id=reference,
                    description=f"Wallet top-up via Stripe (₦{amount:,.2f})"
                ))
            db.commit()
            logger.info(f"Stripe webhook: credited ₦{amount:,.2f} to user {user.id} wallet, reference={reference}")
            
    return {"status": "ok"}