// engine/webhook/event-handlers.ts — Handles individual Lemon Squeezy webhook events.
// Each handler receives parsed payload and applies the corresponding business logic.
// ---------------------------------------------------------------------------
// Individual event handlers
// ---------------------------------------------------------------------------
/**
 * Handles subscription_cancelled: the customer cancelled their subscription.
 * The tier should be reverted to free when the subscription period ends.
 */
export function handleSubscriptionCancelled(payload) {
    const attrs = payload.data.attributes;
    const endsAt = attrs.ends_at ?? 'unknown date';
    const productName = attrs.product_name ?? 'Planu subscription';
    return {
        success: true,
        eventType: 'subscription_cancelled',
        statusCode: 200,
        message: [
            `Subscription cancellation recorded: ${productName}`,
            `Access continues until: ${endsAt}`,
            'Tier will revert to free upon expiry.',
        ].join(' | '),
    };
}
/**
 * Handles subscription_updated: plan tier may have changed (upgrade/downgrade).
 */
export function handleSubscriptionUpdated(payload) {
    const attrs = payload.data.attributes;
    const variantName = attrs.variant_name ?? 'unknown variant';
    const status = attrs.status ?? 'unknown';
    return {
        success: true,
        eventType: 'subscription_updated',
        statusCode: 200,
        message: [`Subscription updated: variant=${variantName}`, `status=${status}`].join(' | '),
    };
}
/**
 * Handles license_key_created: a new license key was issued (after purchase).
 * This is informational — the user will activate the key via activate_license tool.
 */
export function handleLicenseKeyCreated(payload) {
    const attrs = payload.data.attributes;
    const productName = attrs.product_name ?? 'Planu';
    const userEmail = attrs.user_email ?? 'unknown';
    return {
        success: true,
        eventType: 'license_key_created',
        statusCode: 200,
        message: [
            `License key created for: ${productName}`,
            `customer=${userEmail}`,
            'Key can be activated using activate_license tool.',
        ].join(' | '),
    };
}
/**
 * Returns a generic "acknowledged" result for unhandled but recognised events.
 */
export function handleUnknownEvent(eventType) {
    return {
        success: true,
        eventType,
        statusCode: 200,
        message: `Event acknowledged (no handler): ${eventType}`,
    };
}
// ---------------------------------------------------------------------------
// Dispatcher
// ---------------------------------------------------------------------------
const HANDLERS = {
    subscription_cancelled: handleSubscriptionCancelled,
    subscription_updated: handleSubscriptionUpdated,
    license_key_created: handleLicenseKeyCreated,
};
/**
 * Dispatches a parsed webhook payload to the appropriate event handler.
 * Unhandled but valid events are acknowledged with a 200 response.
 */
export function dispatchWebhookEvent(payload) {
    const eventType = payload.meta.event_name;
    const handler = HANDLERS[eventType];
    if (handler) {
        return handler(payload);
    }
    return handleUnknownEvent(eventType);
}
//# sourceMappingURL=event-handlers.js.map