/**
 * Domain-specific typed exceptions for better error handling
 *
 * These exceptions provide structured error information with error codes
 * for consistent error handling across the application.
 */

import { BadRequestException, InternalServerErrorException } from "@nestjs/common";

export interface DomainExceptionDetails {
  message: string;
  details?: Record<string, unknown>;
  code: string;
}

/**
 * Base class for order-related exceptions
 */
export class OrderException extends BadRequestException {
  constructor(message: string, details?: Record<string, unknown>, code = "ORDER_ERROR") {
    super({ message, details, code });
    this.name = "OrderException";
  }
}

/**
 * Thrown when order validation fails
 */
export class OrderValidationException extends BadRequestException {
  constructor(message: string, details?: Record<string, unknown>) {
    super({ message, details, code: "ORDER_VALIDATION_FAILED" });
    this.name = "OrderValidationException";
  }
}

/**
 * Thrown when order fulfillment fails
 */
export class FulfillmentException extends InternalServerErrorException {
  constructor(message: string, details?: Record<string, unknown>) {
    super({ message, details, code: "FULFILLMENT_FAILED" });
    this.name = "FulfillmentException";
  }
}

/**
 * Thrown when SIM activation fails
 */
export class SimActivationException extends BadRequestException {
  constructor(message: string, details?: Record<string, unknown>) {
    super({ message, details, code: "SIM_ACTIVATION_FAILED" });
    this.name = "SimActivationException";
  }
}

/**
 * Thrown when WHMCS operations fail
 */
export class WhmcsOperationException extends InternalServerErrorException {
  constructor(message: string, details?: Record<string, unknown>) {
    super({ message, details, code: "WHMCS_OPERATION_FAILED" });
    this.name = "WhmcsOperationException";
  }
}

/**
 * Thrown when Salesforce operations fail
 */
export class SalesforceOperationException extends InternalServerErrorException {
  constructor(message: string, details?: Record<string, unknown>) {
    super({ message, details, code: "SALESFORCE_OPERATION_FAILED" });
    this.name = "SalesforceOperationException";
  }
}

/**
 * Thrown when Freebit operations fail
 */
export class FreebitOperationException extends InternalServerErrorException {
  constructor(message: string, details?: Record<string, unknown>) {
    super({ message, details, code: "FREEBIT_OPERATION_FAILED" });
    this.name = "FreebitOperationException";
  }
}

/**
 * Thrown when catalog operations fail
 */
export class CatalogException extends BadRequestException {
  constructor(message: string, details?: Record<string, unknown>) {
    super({ message, details, code: "CATALOG_ERROR" });
    this.name = "CatalogException";
  }
}

/**
 * Thrown when payment operations fail
 */
export class PaymentException extends BadRequestException {
  constructor(message: string, details?: Record<string, unknown>) {
    super({ message, details, code: "PAYMENT_FAILED" });
    this.name = "PaymentException";
  }
}
