/**
 * Subscription Domain Exceptions
 */

export class NoLicensesAvailableException extends Error {
  constructor(
    public readonly organizationId: string,
    public readonly currentLicenses: number,
    public readonly maxLicenses: number
  ) {
    super(
      `No licenses available for organization ${organizationId}. ` +
        `Currently using ${currentLicenses}/${maxLicenses} licenses.`
    );
    this.name = 'NoLicensesAvailableException';
    Object.setPrototypeOf(this, NoLicensesAvailableException.prototype);
  }
}

export class SubscriptionNotFoundException extends Error {
  constructor(public readonly identifier: string) {
    super(`Subscription not found: ${identifier}`);
    this.name = 'SubscriptionNotFoundException';
    Object.setPrototypeOf(this, SubscriptionNotFoundException.prototype);
  }
}

export class LicenseNotFoundException extends Error {
  constructor(public readonly identifier: string) {
    super(`License not found: ${identifier}`);
    this.name = 'LicenseNotFoundException';
    Object.setPrototypeOf(this, LicenseNotFoundException.prototype);
  }
}

export class LicenseAlreadyAssignedException extends Error {
  constructor(public readonly userId: string) {
    super(`User ${userId} already has an assigned license`);
    this.name = 'LicenseAlreadyAssignedException';
    Object.setPrototypeOf(this, LicenseAlreadyAssignedException.prototype);
  }
}

export class InvalidSubscriptionDowngradeException extends Error {
  constructor(
    public readonly currentPlan: string,
    public readonly targetPlan: string,
    public readonly currentUsers: number,
    public readonly targetMaxLicenses: number
  ) {
    super(
      `Cannot downgrade from ${currentPlan} to ${targetPlan}. ` +
        `Current users (${currentUsers}) exceed target plan limit (${targetMaxLicenses}).`
    );
    this.name = 'InvalidSubscriptionDowngradeException';
    Object.setPrototypeOf(this, InvalidSubscriptionDowngradeException.prototype);
  }
}

export class SubscriptionNotActiveException extends Error {
  constructor(
    public readonly subscriptionId: string,
    public readonly currentStatus: string
  ) {
    super(`Subscription ${subscriptionId} is not active. Current status: ${currentStatus}`);
    this.name = 'SubscriptionNotActiveException';
    Object.setPrototypeOf(this, SubscriptionNotActiveException.prototype);
  }
}

export class InvalidSubscriptionStatusTransitionException extends Error {
  constructor(
    public readonly fromStatus: string,
    public readonly toStatus: string
  ) {
    super(`Invalid subscription status transition from ${fromStatus} to ${toStatus}`);
    this.name = 'InvalidSubscriptionStatusTransitionException';
    Object.setPrototypeOf(this, InvalidSubscriptionStatusTransitionException.prototype);
  }
}
