import { HttpStatus } from "@nestjs/common";
import { ErrorCode, FreebitProviderError } from "@customer-portal/domain/common";
import { BaseProviderError } from "./base-provider.error.js";

export class FreebitApiError extends BaseProviderError {
  readonly provider = "freebit" as const;
  readonly providerCode = FreebitProviderError.API_ERROR;
  readonly domainErrorCode = ErrorCode.EXTERNAL_SERVICE_ERROR;
  readonly httpStatus = HttpStatus.BAD_GATEWAY;

  constructor(message: string, providerMessage: string, cause?: unknown) {
    super(message, providerMessage, cause);
  }
}

export class FreebitAccountNotFoundError extends BaseProviderError {
  readonly provider = "freebit" as const;
  readonly providerCode = FreebitProviderError.ACCOUNT_NOT_FOUND;
  readonly domainErrorCode = ErrorCode.NOT_FOUND;
  readonly httpStatus = HttpStatus.NOT_FOUND;

  constructor(providerMessage: string, cause?: unknown) {
    super("Freebit account not found", providerMessage, cause);
  }
}

export class FreebitTimeoutError extends BaseProviderError {
  readonly provider = "freebit" as const;
  readonly providerCode = FreebitProviderError.TIMEOUT;
  readonly domainErrorCode = ErrorCode.TIMEOUT;
  readonly httpStatus = HttpStatus.GATEWAY_TIMEOUT;

  constructor(providerMessage: string, cause?: unknown) {
    super("Freebit request timed out", providerMessage, cause);
  }
}

export class FreebitAuthError extends BaseProviderError {
  readonly provider = "freebit" as const;
  readonly providerCode = FreebitProviderError.AUTH_FAILED;
  readonly domainErrorCode = ErrorCode.EXTERNAL_SERVICE_ERROR;
  readonly httpStatus = HttpStatus.SERVICE_UNAVAILABLE;

  constructor(providerMessage: string, cause?: unknown) {
    super("Freebit authentication failed", providerMessage, cause);
  }
}

export class FreebitRateLimitError extends BaseProviderError {
  readonly provider = "freebit" as const;
  readonly providerCode = FreebitProviderError.RATE_LIMITED;
  readonly domainErrorCode = ErrorCode.RATE_LIMITED;
  readonly httpStatus = HttpStatus.TOO_MANY_REQUESTS;

  constructor(providerMessage: string, cause?: unknown) {
    super("Freebit rate limit exceeded", providerMessage, cause);
  }
}

export class FreebitValidationError extends BaseProviderError {
  readonly provider = "freebit" as const;
  readonly providerCode = FreebitProviderError.VALIDATION_ERROR;
  readonly domainErrorCode = ErrorCode.VALIDATION_FAILED;
  readonly httpStatus = HttpStatus.BAD_REQUEST;

  constructor(providerMessage: string, cause?: unknown) {
    super("Freebit validation failed", providerMessage, cause);
  }
}

export class FreebitNetworkError extends BaseProviderError {
  readonly provider = "freebit" as const;
  readonly providerCode = FreebitProviderError.NETWORK_ERROR;
  readonly domainErrorCode = ErrorCode.NETWORK_ERROR;
  readonly httpStatus = HttpStatus.BAD_GATEWAY;

  constructor(providerMessage: string, cause?: unknown) {
    super("Freebit network error", providerMessage, cause);
  }
}
