/**
 * Application Use Case: CreateTeamUseCase
 *
 * Creates a new team.
 */
import { v4 as uuidv4 } from 'uuid';
import type { ITeamRepository } from '../../domain/repositories/ITeamRepository';
import type { ITeamMembershipRepository } from '../../domain/repositories/ITeamMembershipRepository';
import { Team } from '../../domain/entities/Team';
import type {
  TeamMembership,
  TeamMembershipStatus,
  TeamRole,
} from '../../domain/types/TeamMembership';
import type { AsyncUseCase } from '@core/shared/application';
import type { Logger } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';

export interface CreateTeamCommandDTO {
  name: string;
  tag: string;
  description: string;
  ownerId: string;
  leagues: string[];
}

export interface CreateTeamResultDTO {
  team: Team;
}

export class CreateTeamUseCase
  implements AsyncUseCase<CreateTeamCommandDTO, CreateTeamResultDTO, 'ALREADY_IN_TEAM' | 'REPOSITORY_ERROR'>
{
  constructor(
    private readonly teamRepository: ITeamRepository,
    private readonly membershipRepository: ITeamMembershipRepository,
    private readonly logger: Logger,
  ) {}

  async execute(
    command: CreateTeamCommandDTO,
  ): Promise<Result<CreateTeamResultDTO, ApplicationErrorCode<'ALREADY_IN_TEAM' | 'REPOSITORY_ERROR', { message: string }>>> {
    this.logger.debug('Executing CreateTeamUseCase', { command });
    const { name, tag, description, ownerId, leagues } = command;

    const existingMembership = await this.membershipRepository.getActiveMembershipForDriver(
      ownerId,
    );
    if (existingMembership) {
      this.logger.warn('Validation failed: Driver already belongs to a team', { ownerId });
      return Result.err({ code: 'ALREADY_IN_TEAM', details: { message: 'Driver already belongs to a team' } });
    }

    this.logger.info('Command validated successfully.');
    try {
      const teamId = uuidv4();
      this.logger.debug(`Generated teamId: ${teamId}`);

      const team = Team.create({
        id: teamId,
        name,
        tag,
        description,
        ownerId,
        leagues,
      });

      const createdTeam = await this.teamRepository.create(team);
      this.logger.info(`Team ${createdTeam.name} (${createdTeam.id}) created successfully.`);

      const membership: TeamMembership = {
        teamId: createdTeam.id,
        driverId: ownerId,
        role: 'owner' as TeamRole,
        status: 'active' as TeamMembershipStatus,
        joinedAt: new Date(),
      };

      await this.membershipRepository.saveMembership(membership);
      this.logger.debug('Team membership created successfully.');

      const result: CreateTeamResultDTO = { team: createdTeam };
      this.logger.debug('CreateTeamUseCase completed successfully.', { result });
      return Result.ok(result);
    } catch (error) {
      return Result.err({ code: 'REPOSITORY_ERROR', details: { message: error instanceof Error ? error.message : 'Unknown error' } });
    }
  }
}