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

export interface ErrorResponseBody {
  statusCode: number;
  errorCode: ErrorCode;
  message: string;
  details?: Record<string, unknown>;
  correlationId?: string;
  timestamp: string;
}

export class DomainException extends HttpException {
  constructor(
    public readonly errorCode: ErrorCode,
    message: string,
    statusCode: HttpStatus = HttpStatus.INTERNAL_SERVER_ERROR,
    public readonly details?: Record<string, unknown>,
  ) {
    super(message, statusCode);
  }
}

export class NotFoundException extends DomainException {
  constructor(entity: string, id?: string) {
    super(
      ErrorCode.NOT_FOUND,
      id ? `${entity} with id '${id}' not found` : `${entity} not found`,
      HttpStatus.NOT_FOUND,
    );
  }
}

export class ValidationException extends DomainException {
  constructor(message: string, details?: Record<string, unknown>) {
    super(ErrorCode.VALIDATION_FAILED, message, HttpStatus.BAD_REQUEST, details);
  }
}

export class ConflictException extends DomainException {
  constructor(message: string) {
    super(ErrorCode.CONFLICT, message, HttpStatus.CONFLICT);
  }
}

export class UnauthorizedException extends DomainException {
  constructor(message = 'Unauthorized') {
    super(ErrorCode.UNAUTHORIZED, message, HttpStatus.UNAUTHORIZED);
  }
}

export class ForbiddenException extends DomainException {
  constructor(message = 'Forbidden') {
    super(ErrorCode.FORBIDDEN, message, HttpStatus.FORBIDDEN);
  }
}
