import { HttpException, HttpStatus } from '@nestjs/common';

export type TravelErrorCode =
  | 'CHARACTER_NOT_FOUND'
  | 'INVALID_TRAVEL_TARGET'
  | 'TRAVEL_ALREADY_ACTIVE'
  | 'TRAVEL_STATE_INVALID';

export class TravelDomainError extends HttpException {
  constructor(
    public readonly code: TravelErrorCode,
    status: HttpStatus,
    message: string,
  ) {
    super({ statusCode: status, code, message }, status);
  }
}

export function characterNotFound(): TravelDomainError {
  return new TravelDomainError(
    'CHARACTER_NOT_FOUND',
    HttpStatus.NOT_FOUND,
    'The character does not exist.',
  );
}

export function invalidTravelTarget(): TravelDomainError {
  return new TravelDomainError(
    'INVALID_TRAVEL_TARGET',
    HttpStatus.BAD_REQUEST,
    'The selected location is not connected to the current location.',
  );
}

export function travelAlreadyActive(): TravelDomainError {
  return new TravelDomainError(
    'TRAVEL_ALREADY_ACTIVE',
    HttpStatus.CONFLICT,
    'The character is already travelling.',
  );
}

export function invalidTravelState(): TravelDomainError {
  return new TravelDomainError(
    'TRAVEL_STATE_INVALID',
    HttpStatus.INTERNAL_SERVER_ERROR,
    'The persisted travel references an unavailable location.',
  );
}
