import { Request, Response, NextFunction } from 'express';
import logger from '../utils/logger';

export interface AuthenticatedRequest extends Request {
  user?: import('firebase-admin').auth.DecodedIdToken;
}

/**
 * DEPRECATED: Legacy authentication middleware
 * Use Firebase Auth instead via ../middleware/firebaseAuth
 */
export async function authenticateToken(
  _req: AuthenticatedRequest,
  res: Response,
  _next: NextFunction
): Promise<void> {
  logger.warn('Legacy auth middleware is deprecated. Use Firebase Auth instead.');
  res.status(501).json({
    success: false,
    message: 'Legacy authentication is disabled. Use Firebase Auth instead.'
  });
}

// Alias for backward compatibility
export const auth = authenticateToken;

/**
 * DEPRECATED: Role-based authorization middleware
 */
export function requireRole(_allowedRoles: string[]) {
  return (_req: AuthenticatedRequest, res: Response, _next: NextFunction): void => {
    logger.warn('Legacy role-based auth is deprecated. Use Firebase Auth instead.');
    res.status(501).json({
      success: false,
      message: 'Legacy role-based authentication is disabled. Use Firebase Auth instead.'
    });
  };
}

/**
 * DEPRECATED: Admin-only middleware
 */
export function requireAdmin(
  _req: AuthenticatedRequest,
  res: Response,
  _next: NextFunction
): void {
  logger.warn('Legacy admin auth is deprecated. Use Firebase Auth instead.');
  res.status(501).json({
    success: false,
    message: 'Legacy admin authentication is disabled. Use Firebase Auth instead.'
  });
}

/**
 * DEPRECATED: User or admin middleware
 */
export function requireUserOrAdmin(
  _req: AuthenticatedRequest,
  res: Response,
  _next: NextFunction
): void {
  logger.warn('Legacy user/admin auth is deprecated. Use Firebase Auth instead.');
  res.status(501).json({
    success: false,
    message: 'Legacy user/admin authentication is disabled. Use Firebase Auth instead.'
  });
}

/**
 * DEPRECATED: Optional authentication middleware
 */
export async function optionalAuth(
  _req: AuthenticatedRequest,
  _res: Response,
  next: NextFunction
): Promise<void> {
  logger.debug('Legacy optional auth is deprecated. Use Firebase Auth instead.');
  // For optional auth, we just continue without authentication
  next();
}

/**
 * DEPRECATED: Rate limiting middleware
 */
export function authRateLimit(
  _req: Request,
  _res: Response,
  next: NextFunction
): void {
  next();
}

/**
 * DEPRECATED: Logout middleware
 */
export async function logout(
  _req: AuthenticatedRequest,
  res: Response,
  _next: NextFunction
): Promise<void> {
  logger.warn('Legacy logout is deprecated. Use Firebase Auth instead.');
  res.status(501).json({
    success: false,
    message: 'Legacy logout is disabled. Use Firebase Auth instead.'
  });
}