/**
 * @ai-summary Fastify routes for subscriptions API
 * @ai-context Route definitions with authentication for subscription management
 */

import { FastifyInstance, FastifyPluginOptions } from 'fastify';
import { FastifyPluginAsync } from 'fastify';
import { SubscriptionsController } from './subscriptions.controller';

export const subscriptionsRoutes: FastifyPluginAsync = async (
  fastify: FastifyInstance,
  _opts: FastifyPluginOptions
) => {
  const subscriptionsController = new SubscriptionsController();

  // GET /api/subscriptions - Get current subscription
  fastify.get('/subscriptions', {
    preHandler: [fastify.authenticate],
    handler: subscriptionsController.getSubscription.bind(subscriptionsController)
  });

  // GET /api/subscriptions/needs-vehicle-selection - Check if user needs vehicle selection
  fastify.get('/subscriptions/needs-vehicle-selection', {
    preHandler: [fastify.authenticate],
    handler: subscriptionsController.checkNeedsVehicleSelection.bind(subscriptionsController)
  });

  // POST /api/subscriptions/checkout - Create Stripe checkout session
  fastify.post('/subscriptions/checkout', {
    preHandler: [fastify.authenticate],
    handler: subscriptionsController.createCheckout.bind(subscriptionsController)
  });

  // POST /api/subscriptions/cancel - Schedule cancellation
  fastify.post('/subscriptions/cancel', {
    preHandler: [fastify.authenticate],
    handler: subscriptionsController.cancelSubscription.bind(subscriptionsController)
  });

  // POST /api/subscriptions/reactivate - Cancel pending cancellation
  fastify.post('/subscriptions/reactivate', {
    preHandler: [fastify.authenticate],
    handler: subscriptionsController.reactivateSubscription.bind(subscriptionsController)
  });

  // PUT /api/subscriptions/payment-method - Update payment method
  fastify.put('/subscriptions/payment-method', {
    preHandler: [fastify.authenticate],
    handler: subscriptionsController.updatePaymentMethod.bind(subscriptionsController)
  });

  // GET /api/subscriptions/invoices - Get billing history
  fastify.get('/subscriptions/invoices', {
    preHandler: [fastify.authenticate],
    handler: subscriptionsController.getInvoices.bind(subscriptionsController)
  });

  // POST /api/subscriptions/downgrade - Downgrade subscription with vehicle selection
  fastify.post('/subscriptions/downgrade', {
    preHandler: [fastify.authenticate],
    handler: subscriptionsController.downgrade.bind(subscriptionsController)
  });
};
