import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { Team } from '../../domain/entities/Team';
import type { ITeamMembershipRepository } from '../../domain/repositories/ITeamMembershipRepository';
import type { ITeamRepository } from '../../domain/repositories/ITeamRepository';
import type { TeamMembership } from '../../domain/types/TeamMembership';

export type JoinTeamErrorCode =
  | 'ALREADY_IN_TEAM'
  | 'ALREADY_MEMBER'
  | 'TEAM_NOT_FOUND'
  | 'REPOSITORY_ERROR';

export interface JoinTeamInput {
  teamId: string;
  driverId: string;
}

export interface JoinTeamResult {
  team: Team;
  membership: TeamMembership;
}

export class JoinTeamUseCase {
  constructor(
    private readonly teamRepository: ITeamRepository,
    private readonly membershipRepository: ITeamMembershipRepository,
    private readonly logger: Logger,
    private readonly output: UseCaseOutputPort<JoinTeamResult>,
  ) {}

  async execute(
    input: JoinTeamInput,
  ): Promise<
    Result<void, ApplicationErrorCode<JoinTeamErrorCode, { message: string }>>
  > {
    this.logger.debug('Attempting to join team', { input });
    const { teamId, driverId } = input;

    try {
      const existingActive =
        await this.membershipRepository.getActiveMembershipForDriver(driverId);
      if (existingActive) {
        this.logger.warn('Driver already belongs to a team', {
          driverId,
          teamId,
        });
        return Result.err({
          code: 'ALREADY_IN_TEAM',
          details: { message: 'Driver already belongs to a team' },
        });
      }

      const existingMembership =
        await this.membershipRepository.getMembership(teamId, driverId);
      if (existingMembership) {
        this.logger.warn(
          'Driver already has a pending or active membership request',
          { driverId, teamId },
        );
        return Result.err({
          code: 'ALREADY_MEMBER',
          details: { message: 'Already a member or have a pending request' },
        });
      }

      const team = await this.teamRepository.findById(teamId);
      if (!team) {
        this.logger.error('Team not found', new Error(`Team ${teamId} not found`));
        return Result.err({
          code: 'TEAM_NOT_FOUND',
          details: { message: `Team ${teamId} not found` },
        });
      }

      const membership: TeamMembership = {
        teamId,
        driverId,
        role: 'driver',
        status: 'active',
        joinedAt: new Date(),
      };

      const savedMembership =
        await this.membershipRepository.saveMembership(membership);
      this.logger.info('Driver successfully joined team', { driverId, teamId });

      this.output.present({
        team,
        membership: savedMembership,
      });

      return Result.ok(undefined);
    } catch (error) {
      this.logger.error(
        'Failed to join team due to an unexpected error',
        error instanceof Error ? error : new Error('Unknown error'),
      );
      return Result.err({
        code: 'REPOSITORY_ERROR',
        details: {
          message: error instanceof Error ? error.message : 'Unknown error',
        },
      });
    }
  }
}