import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import { Team } from '../../domain/entities/Team';
import { TeamMembershipRepository } from '../../domain/repositories/TeamMembershipRepository';
import { TeamRepository } from '../../domain/repositories/TeamRepository';
import type { TeamMembership } from '../../domain/types/TeamMembership';
import {
    GetTeamDetailsUseCase,
    type GetTeamDetailsErrorCode,
    type GetTeamDetailsInput,
} from './GetTeamDetailsUseCase';

describe('GetTeamDetailsUseCase', () => {
  let useCase: GetTeamDetailsUseCase;
  let teamRepository: {
    findById: Mock;
  };
  let membershipRepository: {
    getMembership: Mock;
  };

  beforeEach(() => {
    teamRepository = {
      findById: vi.fn(),
    };
    membershipRepository = {
      getMembership: vi.fn(),
    };
    useCase = new GetTeamDetailsUseCase(teamRepository as unknown as TeamRepository,
      membershipRepository as unknown as TeamMembershipRepository);
  });

  it('should return team details with membership', async () => {
    const teamId = 'team-1';
    const driverId = 'driver-1';
    const team = Team.create({
      id: teamId,
      name: 'Test Team',
      tag: 'TT',
      description: 'Test',
      ownerId: 'owner-1',
      leagues: [],
    });
    const membership: TeamMembership = {
      teamId,
      driverId,
      role: 'driver',
      status: 'active',
      joinedAt: new Date(),
    };

    teamRepository.findById.mockResolvedValue(team);
    membershipRepository.getMembership.mockResolvedValue(membership);

    const input: GetTeamDetailsInput = { teamId, driverId };

    const result = await useCase.execute(input);

    expect(result.isOk()).toBe(true);
    const presented = result.unwrap();
    expect(presented.team).toBe(team);
    expect(presented.membership).toEqual(membership);
    expect(presented.canManage).toBe(false);
  });

  it('should return team details for owner', async () => {
    const teamId = 'team-1';
    const driverId = 'driver-1';
    const team = Team.create({
      id: teamId,
      name: 'Test Team',
      tag: 'TT',
      description: 'Test',
      ownerId: driverId,
      leagues: [],
    });
    const membership: TeamMembership = {
      teamId,
      driverId,
      role: 'owner',
      status: 'active',
      joinedAt: new Date(),
    };

    teamRepository.findById.mockResolvedValue(team);
    membershipRepository.getMembership.mockResolvedValue(membership);

    const input: GetTeamDetailsInput = { teamId, driverId };

    const result = await useCase.execute(input);

    expect(result.isOk()).toBe(true);
    const presented = result.unwrap();
    expect(presented.canManage).toBe(true);
  });

  it('should return error for non-existing team', async () => {
    const teamId = 'team-1';
    const driverId = 'driver-1';
    teamRepository.findById.mockResolvedValue(null);

    const input: GetTeamDetailsInput = { teamId, driverId };

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const errorResult = result.unwrapErr() as ApplicationErrorCode<
      GetTeamDetailsErrorCode,
      { message: string }
    >;
    expect(errorResult.code).toBe('TEAM_NOT_FOUND');
    expect(errorResult.details?.message).toBe('Team not found');
  });

  it('should return error on repository failure', async () => {
    const teamId = 'team-1';
    const driverId = 'driver-1';
    teamRepository.findById.mockRejectedValue(new Error('DB error'));

    const input: GetTeamDetailsInput = { teamId, driverId };

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const errorResult = result.unwrapErr() as ApplicationErrorCode<
      GetTeamDetailsErrorCode,
      { message: string }
    >;
    expect(errorResult.code).toBe('REPOSITORY_ERROR');
    expect(errorResult.details?.message).toBe('DB error');
  });
});
