import { HttpException, HttpStatus } from '@nestjs/common';
import { ErrorCode } from '../constants/error-codes';

/**
 * Custom application exception with error code support
 */
export class AppException extends HttpException {
  public readonly errorCode: ErrorCode;

  constructor(
    errorCode: ErrorCode,
    statusCode: HttpStatus = HttpStatus.BAD_REQUEST,
    details?: any,
  ) {
    super({ errorCode, details }, statusCode);
    this.errorCode = errorCode;
  }
}

/**
 * Authentication exceptions
 */
export class AuthException extends AppException {
  constructor(errorCode: ErrorCode, details?: any) {
    super(errorCode, HttpStatus.UNAUTHORIZED, details);
  }
}

/**
 * Authorization exceptions
 */
export class ForbiddenException extends AppException {
  constructor(errorCode: ErrorCode, details?: any) {
    super(errorCode, HttpStatus.FORBIDDEN, details);
  }
}

/**
 * Not found exceptions
 */
export class NotFoundException extends AppException {
  constructor(errorCode: ErrorCode, details?: any) {
    super(errorCode, HttpStatus.NOT_FOUND, details);
  }
}

/**
 * Validation exceptions
 */
export class ValidationException extends AppException {
  constructor(errorCode: ErrorCode, details?: any) {
    super(errorCode, HttpStatus.BAD_REQUEST, details);
  }
}

/**
 * Conflict exceptions
 */
export class ConflictException extends AppException {
  constructor(errorCode: ErrorCode, details?: any) {
    super(errorCode, HttpStatus.CONFLICT, details);
  }
}

/**
 * Rate limit exceptions
 */
export class RateLimitException extends AppException {
  constructor(errorCode: ErrorCode, details?: any) {
    super(errorCode, HttpStatus.TOO_MANY_REQUESTS, details);
  }
}

/**
 * Internal server exceptions
 */
export class InternalServerException extends AppException {
  constructor(errorCode: ErrorCode, details?: any) {
    super(errorCode, HttpStatus.INTERNAL_SERVER_ERROR, details);
  }
}
