// packages/functions/src/firebase/billing/cancelSubscription.ts

/**
 * @fileoverview Cancel Subscription Function
 * @description Cancels user's subscription at period end (doesn't delete immediately)
 *
 * @version 0.0.1
 * @since 0.0.1
 * @author AMBROISE PARK Consulting
 */

import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
import * as v from 'valibot';

import { cancelUserSubscription } from '../../shared/billing/helpers/subscriptionManagement.js';
import { createBaseFunction } from '../baseFunction.js';
import { STRIPE_CONFIG } from '../config/constants.js';

import type { CallableRequest } from 'firebase-functions/v2/https';

const cancelSubscriptionSchema = v.object({
  userId: v.string(),
});

/**
 * Business logic for canceling subscription
 * Base function handles: validation, auth, rate limiting, monitoring
 *
 * @version 0.0.1
 * @since 0.0.1
 * @author AMBROISE PARK Consulting
 */
async function cancelSubscriptionLogic(
  data: { userId: string },
  context: { uid: string; request: CallableRequest }
) {
  const { userId } = data;

  const authProvider = {
    async getUser(uid: string) {
      return getFirebaseAdminAuth().getUser(uid);
    },
    async setCustomUserClaims(uid: string, claims: Record<string, unknown>) {
      await getFirebaseAdminAuth().setCustomUserClaims(uid, claims);
    },
  };

  return await cancelUserSubscription(userId, authProvider);
}

/**
 * Cancel subscription function
 *
 * @version 0.0.1
 * @since 0.0.1
 * @author AMBROISE PARK Consulting
 */
export function cancelSubscription() {
  return createBaseFunction(
    STRIPE_CONFIG,
    cancelSubscriptionSchema,
    'cancel_subscription',
    cancelSubscriptionLogic
  );
}
