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

export type GetTeamDetailsInput = {
  teamId: string;
  driverId: string;
};

export type GetTeamDetailsResult = {
  team: Team;
  membership: TeamMembership | null;
  canManage: boolean;
};

export type GetTeamDetailsErrorCode = 'TEAM_NOT_FOUND' | 'REPOSITORY_ERROR';

/**
 * Use Case for retrieving team details.
 */
export class GetTeamDetailsUseCase {
  constructor(
    private readonly teamRepository: ITeamRepository,
    private readonly membershipRepository: ITeamMembershipRepository,
    private readonly output: UseCaseOutputPort<GetTeamDetailsResult>,
  ) {}

  async execute(
    input: GetTeamDetailsInput,
  ): Promise<Result<void, ApplicationErrorCode<GetTeamDetailsErrorCode, { message: string }>>> {
    try {
      const { teamId, driverId } = input;
      const team = await this.teamRepository.findById(teamId);
      if (!team) {
        return Result.err({
          code: 'TEAM_NOT_FOUND',
          details: {
            message: 'Team not found',
          },
        });
      }

      const membership = await this.membershipRepository.getMembership(teamId, driverId);

      this.output.present({
        team,
        membership,
        canManage: membership ? membership.role === 'owner' || membership.role === 'manager' : false,
      });

      return Result.ok(undefined);
    } catch (err) {
      const error = err as { message?: string } | undefined;

      return Result.err({
        code: 'REPOSITORY_ERROR',
        details: {
          message: error?.message ?? 'Failed to load team details',
        },
      });
    }
  }
}