mockforge-registry-server 0.3.121

Plugin registry server for MockForge
Documentation
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
//! Billing and subscription handlers

use axum::{extract::State, http::HeaderMap, Json};
use serde::{Deserialize, Serialize};
use stripe::{
    BillingPortalSession, CheckoutSession, CheckoutSessionMode, Client, CreateBillingPortalSession,
    CreateCheckoutSession, CreateCheckoutSessionLineItems, EventObject, EventType,
};
use uuid::Uuid;

use crate::{
    email::EmailService,
    error::{ApiError, ApiResult},
    middleware::{resolve_org_context, AuthUser},
    models::{
        AuditEventType, Organization, Plan, Subscription, SubscriptionStatus, UsageCounter, User,
    },
    AppState,
};

/// Get current subscription status
pub async fn get_subscription(
    State(state): State<AppState>,
    AuthUser(user_id): AuthUser,
    headers: HeaderMap,
) -> ApiResult<Json<SubscriptionResponse>> {
    let pool = state.db.pool();

    // Resolve org context (extensions not available in handler, use None)
    let org_ctx = resolve_org_context(&state, user_id, &headers, None)
        .await
        .map_err(|_| ApiError::InvalidRequest("Organization not found".to_string()))?;

    // Get subscription
    let subscription = Subscription::find_by_org(pool, org_ctx.org_id)
        .await
        .map_err(ApiError::Database)?;

    // Get current usage
    let usage = UsageCounter::get_or_create_current(pool, org_ctx.org_id)
        .await
        .map_err(ApiError::Database)?;

    // Get plan limits
    let limits = org_ctx.org.limits_json.clone();

    Ok(Json(SubscriptionResponse {
        org_id: org_ctx.org_id,
        plan: org_ctx.org.plan().to_string(),
        status: subscription
            .as_ref()
            .map(|s| s.status().to_string())
            .unwrap_or_else(|| "free".to_string()),
        cancel_at_period_end: subscription
            .as_ref()
            .map(|s| s.cancel_at_period_end)
            .unwrap_or(false),
        current_period_end: subscription.as_ref().map(|s| s.current_period_end).or_else(|| {
            // For free plan, return None or far future
            Some(chrono::Utc::now() + chrono::Duration::days(365))
        }),
        usage: UsageStats {
            requests: usage.requests,
            requests_limit: limits
                .get("requests_per_30d")
                .and_then(|v| v.as_i64())
                .unwrap_or(10000),
            storage_bytes: usage.storage_bytes,
            storage_limit_bytes: limits.get("storage_gb").and_then(|v| v.as_i64()).unwrap_or(1)
                * 1_000_000_000, // Convert GB to bytes
            ai_tokens_used: usage.ai_tokens_used,
            ai_tokens_limit: limits
                .get("ai_tokens_per_month")
                .and_then(|v| v.as_i64())
                .unwrap_or(0),
        },
        limits,
    }))
}

#[derive(Debug, Serialize)]
pub struct SubscriptionResponse {
    pub org_id: Uuid,
    pub plan: String,
    pub status: String,
    pub cancel_at_period_end: bool,
    pub current_period_end: Option<chrono::DateTime<chrono::Utc>>,
    pub usage: UsageStats,
    pub limits: serde_json::Value,
}

#[derive(Debug, Serialize)]
pub struct UsageStats {
    pub requests: i64,
    pub requests_limit: i64,
    pub storage_bytes: i64,
    pub storage_limit_bytes: i64,
    pub ai_tokens_used: i64,
    pub ai_tokens_limit: i64,
}

/// Create Stripe checkout session
/// This would typically redirect to Stripe Checkout
#[derive(Debug, Deserialize)]
pub struct CreateCheckoutRequest {
    pub plan: String, // "pro" or "team"
    pub success_url: Option<String>,
    pub cancel_url: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct CreateCheckoutResponse {
    pub checkout_url: String,
    pub session_id: String,
}

pub async fn create_checkout(
    State(state): State<AppState>,
    AuthUser(user_id): AuthUser,
    headers: HeaderMap,
    Json(request): Json<CreateCheckoutRequest>,
) -> ApiResult<Json<CreateCheckoutResponse>> {
    // Resolve org context (extensions not available in handler, use None)
    let org_ctx = resolve_org_context(&state, user_id, &headers, None)
        .await
        .map_err(|_| ApiError::InvalidRequest("Organization not found".to_string()))?;

    // Validate plan
    let plan = match request.plan.as_str() {
        "pro" => Plan::Pro,
        "team" => Plan::Team,
        _ => {
            return Err(ApiError::InvalidRequest(
                "Invalid plan. Must be 'pro' or 'team'".to_string(),
            ))
        }
    };

    // Get Stripe client
    let stripe_secret = state
        .config
        .stripe_secret_key
        .as_ref()
        .ok_or_else(|| ApiError::InvalidRequest("Stripe not configured".to_string()))?;
    let client = Client::new(stripe_secret);

    // Get price ID for the plan
    let price_id = match plan {
        Plan::Pro => state.config.stripe_price_id_pro.as_ref().ok_or_else(|| {
            ApiError::InvalidRequest("Stripe Pro price ID not configured".to_string())
        })?,
        Plan::Team => state.config.stripe_price_id_team.as_ref().ok_or_else(|| {
            ApiError::InvalidRequest("Stripe Team price ID not configured".to_string())
        })?,
        Plan::Free => {
            return Err(ApiError::InvalidRequest(
                "Cannot create checkout for free plan".to_string(),
            ))
        }
    };

    // Build success and cancel URLs
    let success_url = request.success_url.unwrap_or_else(|| {
        format!(
            "{}/billing/success?session_id={{CHECKOUT_SESSION_ID}}",
            state.config.app_base_url
        )
    });
    let cancel_url = request
        .cancel_url
        .unwrap_or_else(|| format!("{}/billing/cancel", state.config.app_base_url));

    // Create checkout session
    let org_id_str = org_ctx.org_id.to_string();
    let plan_str = plan.to_string();

    let mut checkout_params = CreateCheckoutSession::new();
    checkout_params.success_url = Some(&success_url);
    checkout_params.cancel_url = Some(&cancel_url);
    checkout_params.mode = Some(CheckoutSessionMode::Subscription);
    checkout_params.client_reference_id = Some(&org_id_str);

    // Add metadata for org_id (as backup)
    checkout_params.metadata = Some(std::collections::HashMap::from([
        ("org_id".to_string(), org_id_str.clone()),
        ("plan".to_string(), plan_str.clone()),
    ]));

    // Add line item with price
    checkout_params.line_items = Some(vec![CreateCheckoutSessionLineItems {
        price: Some(price_id.clone()),
        quantity: Some(1),
        ..Default::default()
    }]);

    // Create the checkout session
    let session = CheckoutSession::create(&client, checkout_params)
        .await
        .map_err(|e| ApiError::Internal(anyhow::anyhow!("Stripe error: {}", e)))?;

    // Record audit log
    let ip_address = headers
        .get("X-Forwarded-For")
        .or_else(|| headers.get("X-Real-IP"))
        .and_then(|h| h.to_str().ok())
        .map(|s| s.split(',').next().unwrap_or(s).trim());
    let user_agent = headers.get("User-Agent").and_then(|h| h.to_str().ok());

    state
        .store
        .record_audit_event(
            org_ctx.org_id,
            Some(user_id),
            AuditEventType::BillingCheckout,
            format!("Checkout session created for {} plan", request.plan),
            Some(serde_json::json!({
                "plan": request.plan,
                "session_id": session.id.to_string(),
            })),
            ip_address,
            user_agent,
        )
        .await;

    Ok(Json(CreateCheckoutResponse {
        checkout_url: session
            .url
            .ok_or_else(|| ApiError::Internal(anyhow::anyhow!("Stripe session missing URL")))?
            .to_string(),
        session_id: session.id.to_string(),
    }))
}

/// Create Stripe Customer Portal session
/// Allows users to manage subscription, payment methods, and view invoices
#[derive(Debug, Deserialize)]
pub struct CreatePortalRequest {
    pub return_url: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct CreatePortalResponse {
    pub portal_url: String,
}

pub async fn create_portal_session(
    State(state): State<AppState>,
    AuthUser(user_id): AuthUser,
    headers: HeaderMap,
    Json(request): Json<CreatePortalRequest>,
) -> ApiResult<Json<CreatePortalResponse>> {
    let org_ctx = resolve_org_context(&state, user_id, &headers, None)
        .await
        .map_err(|_| ApiError::InvalidRequest("Organization not found".to_string()))?;

    // Only org owners can access the billing portal
    if org_ctx.org.owner_id != user_id {
        return Err(ApiError::PermissionDenied);
    }

    // Require a Stripe customer ID
    let stripe_customer_id = org_ctx.org.stripe_customer_id.as_ref().ok_or_else(|| {
        ApiError::InvalidRequest(
            "No billing account found. Please subscribe to a plan first.".to_string(),
        )
    })?;

    // Get Stripe client
    let stripe_secret = state
        .config
        .stripe_secret_key
        .as_ref()
        .ok_or_else(|| ApiError::InvalidRequest("Stripe not configured".to_string()))?;
    let client = Client::new(stripe_secret);

    let return_url = request
        .return_url
        .unwrap_or_else(|| format!("{}/billing", state.config.app_base_url));

    let customer_id = stripe_customer_id
        .parse()
        .map_err(|_| ApiError::Internal(anyhow::anyhow!("Invalid Stripe customer ID")))?;

    let mut params = CreateBillingPortalSession::new(customer_id);
    params.return_url = Some(&return_url);

    let session = BillingPortalSession::create(&client, params)
        .await
        .map_err(|e| ApiError::Internal(anyhow::anyhow!("Stripe portal error: {}", e)))?;

    Ok(Json(CreatePortalResponse {
        portal_url: session.url,
    }))
}

/// Stripe webhook handler
/// This receives events from Stripe and updates subscriptions
pub async fn stripe_webhook(
    State(state): State<AppState>,
    headers: HeaderMap,
    body: axum::body::Bytes,
) -> ApiResult<Json<serde_json::Value>> {
    // Get webhook secret
    let webhook_secret = state.config.stripe_webhook_secret.as_ref().ok_or_else(|| {
        ApiError::InvalidRequest("Stripe webhook secret not configured".to_string())
    })?;

    // Get signature from headers
    let signature = headers
        .get("stripe-signature")
        .and_then(|h| h.to_str().ok())
        .ok_or_else(|| ApiError::InvalidRequest("Missing stripe-signature header".to_string()))?;

    // Verify webhook signature
    let body_str = std::str::from_utf8(&body)
        .map_err(|e| ApiError::InvalidRequest(format!("Invalid UTF-8 in webhook body: {}", e)))?;
    let event =
        stripe::Webhook::construct_event(body_str, signature, webhook_secret).map_err(|e| {
            ApiError::InvalidRequest(format!("Webhook signature verification failed: {}", e))
        })?;

    let pool = state.db.pool();

    match event.type_ {
        EventType::CheckoutSessionCompleted => {
            // Handle checkout completion
            if let EventObject::CheckoutSession(session) = event.data.object {
                handle_checkout_completed(pool, &session).await?;
            }
        }
        EventType::CustomerSubscriptionCreated | EventType::CustomerSubscriptionUpdated => {
            // Handle subscription creation/update
            if let EventObject::Subscription(subscription) = event.data.object {
                handle_subscription_event(pool, &subscription, &state.config).await?;
            }
        }
        EventType::CustomerSubscriptionDeleted => {
            // Handle subscription cancellation
            if let EventObject::Subscription(subscription) = event.data.object {
                handle_subscription_deleted(pool, &subscription).await?;
            }
        }
        EventType::InvoicePaymentSucceeded => {
            // Payment succeeded - subscription is active
            if let EventObject::Invoice(invoice) = event.data.object {
                handle_payment_succeeded(pool, &invoice).await?;
            }
        }
        EventType::InvoicePaymentFailed => {
            // Payment failed - mark subscription as past_due
            if let EventObject::Invoice(invoice) = event.data.object {
                handle_payment_failed(pool, &invoice).await?;
            }
        }
        _ => {
            tracing::debug!("Unhandled Stripe event: {:?}", event.type_);
        }
    }

    Ok(Json(serde_json::json!({ "received": true })))
}

/// Handle checkout session completed
async fn handle_checkout_completed(
    pool: &sqlx::PgPool,
    session: &CheckoutSession,
) -> Result<(), ApiError> {
    // Extract org_id from client_reference_id
    let org_id_str = session.client_reference_id.as_ref().ok_or_else(|| {
        ApiError::InvalidRequest("Missing client_reference_id in checkout session".to_string())
    })?;

    let org_id = Uuid::parse_str(org_id_str)
        .map_err(|_| ApiError::InvalidRequest("Invalid org_id in checkout session".to_string()))?;

    // Get subscription ID from session
    let subscription_id = session
        .subscription
        .as_ref()
        .and_then(|s| match s {
            stripe::Expandable::Id(id) => Some(id.clone()),
            stripe::Expandable::Object(_) => None, // Would need to expand
        })
        .ok_or_else(|| {
            ApiError::InvalidRequest("Missing subscription in checkout session".to_string())
        })?;

    tracing::info!("Checkout completed: org_id={}, subscription_id={}", org_id, subscription_id);

    // The subscription will be handled by the subscription.created/updated webhook
    // But we can update the org's stripe_customer_id here if needed
    if let Some(customer_id) = &session.customer {
        let customer_id_str = match customer_id {
            stripe::Expandable::Id(id) => id.to_string(),
            stripe::Expandable::Object(customer) => customer.id.to_string(),
        };

        Organization::update_stripe_customer_id(pool, org_id, Some(&customer_id_str))
            .await
            .map_err(ApiError::Database)?;
    }

    Ok(())
}

/// Get organization owner's email for sending notifications.
///
/// Returns `(email, username, email_notifications)`. Callers should skip
/// non-critical sends when `email_notifications` is false.
async fn get_org_owner_email(
    pool: &sqlx::PgPool,
    org_id: Uuid,
) -> Result<(String, String, bool), ApiError> {
    // Get organization
    let org = Organization::find_by_id(pool, org_id)
        .await
        .map_err(ApiError::Database)?
        .ok_or_else(|| ApiError::InvalidRequest("Organization not found".to_string()))?;

    // Get owner user
    let owner = User::find_by_id(pool, org.owner_id)
        .await
        .map_err(ApiError::Database)?
        .ok_or_else(|| ApiError::InvalidRequest("Organization owner not found".to_string()))?;

    Ok((owner.email, owner.username, owner.email_notifications))
}

async fn handle_subscription_event(
    pool: &sqlx::PgPool,
    subscription: &stripe::Subscription,
    config: &crate::config::Config,
) -> Result<(), ApiError> {
    let stripe_sub_id = subscription.id.to_string();

    let stripe_customer_id = match &subscription.customer {
        stripe::Expandable::Id(id) => id.to_string(),
        stripe::Expandable::Object(customer) => customer.id.to_string(),
    };

    // Get price_id from subscription items
    let price_id = subscription
        .items
        .data
        .first()
        .and_then(|item| item.price.as_ref())
        .map(|price| price.id.to_string())
        .ok_or_else(|| ApiError::InvalidRequest("Missing price in subscription".to_string()))?;

    // Determine plan from price_id (using config for exact matching)
    let plan = determine_plan_from_price_id(&price_id, config);

    let status = SubscriptionStatus::from_string(subscription.status.as_ref());

    let current_period_start =
        chrono::DateTime::<chrono::Utc>::from_timestamp(subscription.current_period_start, 0)
            .unwrap_or_else(chrono::Utc::now);

    let current_period_end =
        chrono::DateTime::<chrono::Utc>::from_timestamp(subscription.current_period_end, 0)
            .unwrap_or_else(|| chrono::Utc::now() + chrono::Duration::days(30));

    let cancel_at_period_end = subscription.cancel_at_period_end;

    let canceled_at = subscription
        .canceled_at
        .and_then(|ts| chrono::DateTime::<chrono::Utc>::from_timestamp(ts, 0));

    // Get org_id from metadata or find by customer_id
    let org_id = subscription
        .metadata
        .get("org_id")
        .and_then(|s| Uuid::parse_str(s).ok())
        .or({
            // Fallback: find org by stripe_customer_id
            // This requires async, so we'll need to handle it differently
            // For now, require metadata
            None
        })
        .ok_or_else(|| {
            ApiError::InvalidRequest("Missing org_id in subscription metadata".to_string())
        })?;

    // Upsert subscription
    let _subscription_record = Subscription::upsert_from_stripe(
        pool,
        org_id,
        &stripe_sub_id,
        &stripe_customer_id,
        &price_id,
        plan,
        status,
        current_period_start,
        current_period_end,
        cancel_at_period_end,
        canceled_at,
    )
    .await
    .map_err(ApiError::Database)?;

    // Get old plan before updating
    let old_org = Organization::find_by_id(pool, org_id)
        .await
        .map_err(ApiError::Database)?
        .ok_or_else(|| ApiError::InvalidRequest("Organization not found".to_string()))?;
    let old_plan = old_org.plan();

    // Update organization plan
    Organization::update_plan(pool, org_id, plan)
        .await
        .map_err(ApiError::Database)?;

    // Update stripe_customer_id on org
    Organization::update_stripe_customer_id(pool, org_id, Some(&stripe_customer_id))
        .await
        .map_err(ApiError::Database)?;

    tracing::info!("Subscription updated: org_id={}, plan={:?}", org_id, plan);

    // Record audit log (webhook event, so no user_id or IP)
    if old_plan != plan {
        // Determine if this is an upgrade or downgrade based on plan ordering
        // Free < Pro < Team
        let plan_order = |p: Plan| -> u8 {
            match p {
                Plan::Free => 0,
                Plan::Pro => 1,
                Plan::Team => 2,
            }
        };
        let event_type = if plan_order(plan) > plan_order(old_plan) {
            AuditEventType::BillingUpgrade
        } else {
            AuditEventType::BillingDowngrade
        };

        crate::models::record_audit_event(
            pool,
            org_id,
            None, // Webhook event, no user context
            event_type,
            format!("Subscription plan changed from {:?} to {:?}", old_plan, plan),
            Some(serde_json::json!({
                "old_plan": old_plan.to_string(),
                "new_plan": plan.to_string(),
                "subscription_id": stripe_sub_id,
                "status": status.to_string(),
            })),
            None, // Webhook event, no IP
            None, // Webhook event, no user agent
        )
        .await;
    }

    // Send subscription confirmation email (non-blocking, gated on owner pref)
    if let Ok((email_addr, username, email_notifications)) = get_org_owner_email(pool, org_id).await
    {
        if email_notifications {
            if let Ok(email_service) = EmailService::from_env() {
                let amount = subscription
                    .items
                    .data
                    .first()
                    .and_then(|item| item.price.as_ref())
                    .and_then(|price| price.unit_amount)
                    .map(|amount| amount as f64 / 100.0); // Convert cents to dollars

                let plan_str = plan.to_string();
                let email_msg = EmailService::generate_subscription_confirmation(
                    &username,
                    &email_addr,
                    &plan_str,
                    amount,
                    Some(current_period_end),
                );

                tokio::spawn(async move {
                    if let Err(e) = email_service.send(email_msg).await {
                        tracing::warn!("Failed to send subscription confirmation email: {}", e);
                    }
                });
            }
        }
    }

    Ok(())
}

async fn handle_subscription_deleted(
    pool: &sqlx::PgPool,
    subscription: &stripe::Subscription,
) -> Result<(), ApiError> {
    let stripe_sub_id = subscription.id.to_string();

    // Find subscription
    let subscription_record = Subscription::find_by_stripe_subscription_id(pool, &stripe_sub_id)
        .await
        .map_err(ApiError::Database)?
        .ok_or_else(|| {
            ApiError::InvalidRequest("Subscription not found in database".to_string())
        })?;

    // Update status to canceled
    Subscription::update_status(pool, subscription_record.id, SubscriptionStatus::Canceled)
        .await
        .map_err(ApiError::Database)?;

    // Get the organization before downgrading to log the old plan
    let org = Organization::find_by_id(pool, subscription_record.org_id)
        .await
        .map_err(ApiError::Database)?
        .ok_or_else(|| ApiError::InvalidRequest("Organization not found".to_string()))?;
    let old_plan = org.plan();

    // Downgrade org to free plan
    Organization::update_plan(pool, subscription_record.org_id, Plan::Free)
        .await
        .map_err(ApiError::Database)?;

    tracing::info!("Subscription canceled: org_id={}", subscription_record.org_id);

    // Record audit log (webhook event, so no user_id or IP)
    crate::models::record_audit_event(
        pool,
        subscription_record.org_id,
        None, // Webhook event, no user context
        AuditEventType::BillingCanceled,
        format!("Subscription canceled for plan: {:?}", old_plan),
        Some(serde_json::json!({
            "subscription_id": subscription.id.to_string(),
            "plan": old_plan.to_string(),
        })),
        None, // Webhook event, no IP
        None, // Webhook event, no user agent
    )
    .await;

    // Send subscription canceled email (non-blocking, gated on owner pref)
    if let Ok((email_addr, username, email_notifications)) =
        get_org_owner_email(pool, subscription_record.org_id).await
    {
        if email_notifications {
            if let Ok(email_service) = EmailService::from_env() {
                let plan_str = old_plan.to_string();
                let email_msg = EmailService::generate_subscription_canceled(
                    &username,
                    &email_addr,
                    &plan_str,
                    Some(subscription_record.current_period_end),
                );

                tokio::spawn(async move {
                    if let Err(e) = email_service.send(email_msg).await {
                        tracing::warn!("Failed to send subscription canceled email: {}", e);
                    }
                });
            }
        }
    }

    Ok(())
}

/// Handle payment succeeded
async fn handle_payment_succeeded(
    pool: &sqlx::PgPool,
    invoice: &stripe::Invoice,
) -> Result<(), ApiError> {
    let customer_id = match &invoice.customer {
        Some(stripe::Expandable::Id(id)) => id.to_string(),
        Some(stripe::Expandable::Object(customer)) => customer.id.to_string(),
        None => return Err(ApiError::InvalidRequest("Invoice missing customer".to_string())),
    };

    // Find subscription by customer_id
    let subscription = sqlx::query_as::<_, Subscription>(
        "SELECT * FROM subscriptions WHERE stripe_customer_id = $1 ORDER BY created_at DESC LIMIT 1"
    )
    .bind(&customer_id)
    .fetch_optional(pool)
    .await
    .map_err(ApiError::Database)?
    .ok_or_else(|| ApiError::InvalidRequest("Subscription not found".to_string()))?;

    // Update status to active if it was past_due
    if subscription.status() == SubscriptionStatus::PastDue {
        Subscription::update_status(pool, subscription.id, SubscriptionStatus::Active)
            .await
            .map_err(ApiError::Database)?;
    }

    tracing::info!("Payment succeeded: org_id={}", subscription.org_id);

    Ok(())
}

async fn handle_payment_failed(
    pool: &sqlx::PgPool,
    invoice: &stripe::Invoice,
) -> Result<(), ApiError> {
    let customer_id = match &invoice.customer {
        Some(stripe::Expandable::Id(id)) => id.to_string(),
        Some(stripe::Expandable::Object(customer)) => customer.id.to_string(),
        None => return Err(ApiError::InvalidRequest("Invoice missing customer".to_string())),
    };

    // Find subscription by customer_id
    let subscription = sqlx::query_as::<_, Subscription>(
        "SELECT * FROM subscriptions WHERE stripe_customer_id = $1 ORDER BY created_at DESC LIMIT 1"
    )
    .bind(&customer_id)
    .fetch_optional(pool)
    .await
    .map_err(ApiError::Database)?
    .ok_or_else(|| ApiError::InvalidRequest("Subscription not found".to_string()))?;

    // Update status to past_due
    Subscription::update_status(pool, subscription.id, SubscriptionStatus::PastDue)
        .await
        .map_err(ApiError::Database)?;

    tracing::info!("Payment failed: org_id={}", subscription.org_id);

    // Record audit log (webhook event, so no user_id or IP)
    crate::models::record_audit_event(
        pool,
        subscription.org_id,
        None,                            // Webhook event, no user context
        AuditEventType::BillingCanceled, // Using canceled as closest match
        format!("Payment failed for subscription: {}", invoice.id),
        Some(serde_json::json!({
            "invoice_id": invoice.id.to_string(),
            "amount_due": invoice.amount_due,
            "subscription_id": subscription.stripe_subscription_id,
        })),
        None, // Webhook event, no IP
        None, // Webhook event, no user agent
    )
    .await;

    // Send payment failed email (non-blocking, gated on owner pref)
    if let Ok((email_addr, username, email_notifications)) =
        get_org_owner_email(pool, subscription.org_id).await
    {
        if email_notifications {
            if let Ok(email_service) = EmailService::from_env() {
                let org = Organization::find_by_id(pool, subscription.org_id)
                    .await
                    .map_err(ApiError::Database)?
                    .ok_or_else(|| {
                        ApiError::InvalidRequest("Organization not found".to_string())
                    })?;

                let amount = invoice.amount_due.map(|a| a as f64 / 100.0).unwrap_or(0.0);
                let retry_date = invoice
                    .next_payment_attempt
                    .and_then(|ts| chrono::DateTime::<chrono::Utc>::from_timestamp(ts, 0));

                let plan_str = org.plan().to_string();
                let email_msg = EmailService::generate_payment_failed(
                    &username,
                    &email_addr,
                    &plan_str,
                    amount,
                    retry_date,
                );

                tokio::spawn(async move {
                    if let Err(e) = email_service.send(email_msg).await {
                        tracing::warn!("Failed to send payment failed email: {}", e);
                    }
                });
            }
        }
    }

    Ok(())
}

/// Determine plan from Stripe price_id
/// Maps Stripe price IDs to internal plans using exact matching from config
fn determine_plan_from_price_id(price_id: &str, config: &crate::config::Config) -> Plan {
    // Exact match against configured price IDs
    if let Some(pro_price_id) = &config.stripe_price_id_pro {
        if price_id == pro_price_id {
            return Plan::Pro;
        }
    }

    if let Some(team_price_id) = &config.stripe_price_id_team {
        if price_id == team_price_id {
            return Plan::Team;
        }
    }

    // Fallback: heuristic matching (for development/testing)
    if price_id.contains("pro") || price_id.contains("Pro") {
        tracing::warn!("Using heuristic matching for price_id: {} (Pro)", price_id);
        return Plan::Pro;
    }

    if price_id.contains("team") || price_id.contains("Team") {
        tracing::warn!("Using heuristic matching for price_id: {} (Team)", price_id);
        return Plan::Team;
    }

    // Default to Free if unknown
    tracing::warn!("Unknown price_id: {}, defaulting to Free", price_id);
    Plan::Free
}