import type { IRaceRegistrationRepository } from '@core/racing/domain/repositories/IRaceRegistrationRepository';
import type { ILeagueMembershipRepository } from '@core/racing/domain/repositories/ILeagueMembershipRepository';
import { RaceRegistration } from '@core/racing/domain/entities/RaceRegistration';
import { Logger, UseCaseOutputPort } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';

export type RegisterForRaceInput = {
  raceId: string;
  leagueId: string;
  driverId: string;
};

export type RegisterForRaceResult = {
  raceId: string;
  driverId: string;
  status: 'registered';
};

export type RegisterForRaceErrorCode =
  | 'ALREADY_REGISTERED'
  | 'NOT_ACTIVE_MEMBER'
  | 'REPOSITORY_ERROR';

export class RegisterForRaceUseCase {
  constructor(
    private readonly registrationRepository: IRaceRegistrationRepository,
    private readonly membershipRepository: ILeagueMembershipRepository,
    private readonly logger: Logger,
    private readonly output: UseCaseOutputPort<RegisterForRaceResult>,
  ) {}

  /**
   * Mirrors legacy registerForRace behavior:
   * - returns error if already registered
   * - validates active league membership
   * - registers driver for race
   */
  async execute(
    input: RegisterForRaceInput,
  ): Promise<
    Result<
      void,
      ApplicationErrorCode<
        RegisterForRaceErrorCode,
        {
          message: string;
        }
      >
    >
  > {
    const { raceId, leagueId, driverId } = input;
    this.logger.debug('RegisterForRaceUseCase: executing params', { raceId, leagueId, driverId });

    try {
      const alreadyRegistered = await this.registrationRepository.isRegistered(raceId, driverId);
      if (alreadyRegistered) {
        this.logger.warn(`RegisterForRaceUseCase: driver ${driverId} already registered for race ${raceId}`);
        return Result.err<void, ApplicationErrorCode<RegisterForRaceErrorCode, { message: string }>>({
          code: 'ALREADY_REGISTERED',
          details: { message: 'Already registered for this race' },
        });
      }

      const membership = await this.membershipRepository.getMembership(leagueId, driverId);
      if (!membership || membership.status.toString() !== 'active') {
        this.logger.error(`RegisterForRaceUseCase: driver ${driverId} not an active member of league ${leagueId}`);
        return Result.err<void, ApplicationErrorCode<RegisterForRaceErrorCode, { message: string }>>({
          code: 'NOT_ACTIVE_MEMBER',
          details: { message: 'Must be an active league member to register for races' },
        });
      }

      const registration = RaceRegistration.create({
        raceId,
        driverId,
      });

      await this.registrationRepository.register(registration);
      this.logger.info(`RegisterForRaceUseCase: driver ${driverId} successfully registered for race ${raceId}`);

      const result: RegisterForRaceResult = {
        raceId: registration.raceId.toString(),
        driverId: registration.driverId.toString(),
        status: 'registered',
      };

      this.output.present(result);

      return Result.ok(undefined);
    } catch (error) {
      const message =
        error instanceof Error && error.message
          ? error.message
          : 'Failed to register for race';

      this.logger.error('RegisterForRaceUseCase: unexpected error during registration', error instanceof Error ? error : undefined, {
        raceId,
        leagueId,
        driverId,
      });

      return Result.err<void, ApplicationErrorCode<RegisterForRaceErrorCode, { message: string }>>({
        code: 'REPOSITORY_ERROR',
        details: { message },
      });
    }
  }
}