import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { GetTeamDetailsUseCase } from './GetTeamDetailsUseCase';
import { ITeamRepository } from '../../domain/repositories/ITeamRepository';
import { ITeamMembershipRepository } from '../../domain/repositories/ITeamMembershipRepository';
import { Team } from '../../domain/entities/Team';

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 ITeamRepository,
      membershipRepository as unknown as ITeamMembershipRepository,
    );
  });

  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 = {
      driverId,
      role: 'member' as const,
      status: 'active' as const,
      joinedAt: new Date(),
    };

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

    const result = await useCase.execute({ teamId, driverId });

    expect(result.isOk()).toBe(true);
    const viewModel = result.unwrap();
    expect(viewModel.team.id).toBe(teamId);
    expect(viewModel.membership?.role).toBe('member');
    expect(viewModel.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 = {
      driverId,
      role: 'owner' as const,
      status: 'active' as const,
      joinedAt: new Date(),
    };

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

    const result = await useCase.execute({ teamId, driverId });

    expect(result.isOk()).toBe(true);
    const viewModel = result.unwrap();
    expect(viewModel.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 result = await useCase.execute({ teamId, driverId });

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({
      code: 'TEAM_NOT_FOUND',
      message: '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 result = await useCase.execute({ teamId, driverId });

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({
      code: 'REPOSITORY_ERROR',
      message: 'Failed to fetch team details',
    });
  });
});