/**
 * Provider dashboard URL + label helpers. Centralized so a new payment
 * provider lands in one file (rather than scattering provider-specific
 * URL formats across every admin page that links into the dashboard).
 *
 * Returns null when the provider is unknown or the id is missing — call
 * sites should hide the link element entirely in that case rather than
 * rendering a broken anchor.
 */

const STRIPE_BASE = 'https://dashboard.stripe.com';
const RAZORPAY_BASE = 'https://dashboard.razorpay.com/app';
// PayPal splits sandbox + live to two completely different dashboard hosts.
// Mode-aware helpers below pick the right one.
const PAYPAL_LIVE_BASE = 'https://www.paypal.com';
const PAYPAL_SANDBOX_BASE = 'https://www.sandbox.paypal.com';
const PAYPAL_DEV_BASE = 'https://developer.paypal.com';

export const PROVIDER_LABELS: Record<string, string> = {
    stripe: 'Stripe',
    razorpay: 'Razorpay',
    paypal: 'PayPal',
};

export function providerLabel(providerKey: string): string {
    return PROVIDER_LABELS[providerKey] ?? providerKey;
}

export function subscriptionDashboardUrl(
    providerKey: string,
    providerSubscriptionId: string | null,
    mode: 'live' | 'test' = 'live',
): string | null {
    if (!providerSubscriptionId) return null;
    if (providerKey === 'stripe') {
        return `${STRIPE_BASE}/subscriptions/${providerSubscriptionId}`;
    }
    if (providerKey === 'razorpay') {
        return `${RAZORPAY_BASE}/subscriptions/${providerSubscriptionId}`;
    }
    if (providerKey === 'paypal') {
        // PayPal sub detail lives under /billing/subscriptions/{id}.
        // Sandbox/live split by host.
        const base = mode === 'test' ? PAYPAL_SANDBOX_BASE : PAYPAL_LIVE_BASE;
        return `${base}/billing/subscriptions/${providerSubscriptionId}`;
    }
    return null;
}

export function invoiceDashboardUrl(
    providerKey: string,
    providerInvoiceId: string | null,
    mode: 'live' | 'test' = 'live',
): string | null {
    if (!providerInvoiceId) return null;
    if (providerKey === 'stripe') {
        return `${STRIPE_BASE}/invoices/${providerInvoiceId}`;
    }
    if (providerKey === 'razorpay') {
        // Razorpay's `provider_invoice_id` is actually the payment id
        // (`pay_…`) — see RazorpayProvider::handlePaymentCaptured for the
        // rationale (refunds flow through the payment entity, not the
        // invoice). Dashboard route is /app/payments/{pay_id}. The
        // /app/invoices/... path expects an `inv_…` and 404s on `pay_…`.
        return `${RAZORPAY_BASE}/payments/${providerInvoiceId}`;
    }
    if (providerKey === 'paypal') {
        // PayPal's `provider_invoice_id` is the capture id (e.g.
        // `CAP-1AB23...`). Activity page deep-links to the capture
        // record. Sandbox/live split by host.
        const base = mode === 'test' ? PAYPAL_SANDBOX_BASE : PAYPAL_LIVE_BASE;
        return `${base}/activity/payment/${providerInvoiceId}`;
    }
    return null;
}

/**
 * Direct link to the page where the merchant generates / copies API keys.
 * Used on /admin/billing to spare the admin from hunting through the
 * provider dashboard. Stripe routes by mode (test keys live behind the
 * `/test/` segment); Razorpay's keys page is mode-agnostic — admin picks
 * the mode in the dashboard's top toggle.
 */
export function apiKeysDashboardUrl(
    providerKey: string,
    mode: 'live' | 'test' = 'live',
): string | null {
    if (providerKey === 'stripe') {
        return mode === 'test'
            ? `${STRIPE_BASE}/test/apikeys`
            : `${STRIPE_BASE}/apikeys`;
    }
    if (providerKey === 'razorpay') {
        return `${RAZORPAY_BASE}/keys`;
    }
    if (providerKey === 'paypal') {
        // PayPal credentials live in the Developer dashboard's "Apps &
        // Credentials" — mode toggle in the UI's top-right switches the
        // visible app list. Same URL for both modes.
        return `${PAYPAL_DEV_BASE}/dashboard/applications/${mode === 'test' ? 'sandbox' : 'live'}`;
    }
    return null;
}

/**
 * Direct link to the webhook-management page. Useful as a fallback when
 * auto-registration fails (restricted-scope keys, manual environment) and
 * the admin needs to set up the webhook by hand.
 */
export function webhooksDashboardUrl(
    providerKey: string,
    mode: 'live' | 'test' = 'live',
): string | null {
    if (providerKey === 'stripe') {
        return mode === 'test'
            ? `${STRIPE_BASE}/test/webhooks`
            : `${STRIPE_BASE}/webhooks`;
    }
    if (providerKey === 'razorpay') {
        return `${RAZORPAY_BASE}/webhooks`;
    }
    if (providerKey === 'paypal') {
        // Webhooks are configured per-app on PayPal — admin picks the
        // app first then sees webhook config under "Sandbox/Live
        // Webhooks" section.
        return `${PAYPAL_DEV_BASE}/dashboard/applications/${mode === 'test' ? 'sandbox' : 'live'}`;
    }
    return null;
}
