import { DriverTeamPresenter } from './DriverTeamPresenter';

describe('DriverTeamPresenter', () => {
  let presenter: DriverTeamPresenter;

  beforeEach(() => {
    presenter = new DriverTeamPresenter();
  });

  describe('present', () => {
    it('should map result to response model for owner', () => {
      const result = {
        driverId: 'driver-123',
        team: {
          id: 'team-456',
          name: { toString: () => 'Team Alpha' } as never,
          tag: { toString: () => 'TA' } as never,
          description: { toString: () => 'Best team' } as never,
          ownerId: { toString: () => 'driver-123' } as never,
          leagues: [{ toString: () => 'league-1' } as never],
          isRecruiting: true,
          createdAt: { toDate: () => new Date('2024-01-01') } as never,
        } as never,
        membership: {
          role: 'owner' as const,
          joinedAt: new Date('2024-01-01'),
          status: 'active' as const,
        } as never,
      };

      presenter.present(result);

      const model = presenter.getResponseModel();
      expect(model).toEqual({
        team: {
          id: 'team-456',
          name: 'Team Alpha',
          tag: 'TA',
          description: 'Best team',
          ownerId: 'driver-123',
          leagues: ['league-1'],
          isRecruiting: true,
          createdAt: '2024-01-01T00:00:00.000Z',
        },
        membership: {
          role: 'owner',
          joinedAt: '2024-01-01T00:00:00.000Z',
          isActive: true,
        },
        isOwner: true,
        canManage: true,
      });
    });

    it('should map result for manager', () => {
      const result = {
        driverId: 'driver-456',
        team: {
          id: 'team-789',
          name: { toString: () => 'Team Beta' } as never,
          tag: { toString: () => 'TB' } as never,
          description: { toString: () => '' } as never,
          ownerId: { toString: () => 'driver-123' } as never,
          leagues: [] as never[],
          isRecruiting: false,
          createdAt: { toDate: () => new Date('2024-01-02') } as never,
        } as never,
        membership: {
          role: 'manager' as const,
          joinedAt: new Date('2024-01-02'),
          status: 'active' as const,
        } as never,
      };

      presenter.present(result);

      const model = presenter.getResponseModel();
      expect(model?.isOwner).toBe(false);
      expect(model?.canManage).toBe(true);
      expect(model?.membership.role).toBe('manager');
    });

    it('should map result for member', () => {
      const result = {
        driverId: 'driver-789',
        team: {
          id: 'team-abc',
          name: { toString: () => 'Team Gamma' } as never,
          tag: { toString: () => 'TG' } as never,
          description: { toString: () => 'Test team' } as never,
          ownerId: { toString: () => 'driver-123' } as never,
          leagues: [{ toString: () => 'league-2' } as never],
          isRecruiting: false,
          createdAt: { toDate: () => new Date('2024-01-03') } as never,
        } as never,
        membership: {
          role: 'driver' as const,
          joinedAt: new Date('2024-01-03'),
          status: 'active' as const,
        } as never,
      };

      presenter.present(result);

      const model = presenter.getResponseModel();
      expect(model?.isOwner).toBe(false);
      expect(model?.canManage).toBe(false);
      expect(model?.membership.role).toBe('member');
    });

    it('should handle empty description', () => {
      const result = {
        driverId: 'driver-123',
        team: {
          id: 'team-456',
          name: { toString: () => 'Team' } as never,
          tag: { toString: () => 'T' } as never,
          description: { toString: () => '' } as never,
          ownerId: { toString: () => 'driver-123' } as never,
          leagues: [] as never[],
          isRecruiting: false,
          createdAt: { toDate: () => new Date('2024-01-01') } as never,
        } as never,
        membership: {
          role: 'owner' as const,
          joinedAt: new Date('2024-01-01'),
          status: 'active' as const,
        } as never,
      };

      presenter.present(result);

      const model = presenter.getResponseModel();
      expect(model?.team.description).toBe('');
    });

    it('should handle empty leagues', () => {
      const result = {
        driverId: 'driver-123',
        team: {
          id: 'team-456',
          name: { toString: () => 'Team' } as never,
          tag: { toString: () => 'T' } as never,
          description: { toString: () => 'Test' } as never,
          ownerId: { toString: () => 'driver-123' } as never,
          leagues: [] as never[],
          isRecruiting: false,
          createdAt: { toDate: () => new Date('2024-01-01') } as never,
        } as never,
        membership: {
          role: 'owner' as const,
          joinedAt: new Date('2024-01-01'),
          status: 'active' as const,
        } as never,
      };

      presenter.present(result);

      const model = presenter.getResponseModel();
      expect(model?.team.leagues).toEqual([]);
    });
  });

  describe('reset', () => {
    it('should clear the model', () => {
      presenter.present({
        driverId: 'driver-123',
        team: {
          id: 'team-456',
          name: { toString: () => 'Team' } as never,
          tag: { toString: () => 'T' } as never,
          description: { toString: () => '' } as never,
          ownerId: { toString: () => 'driver-123' } as never,
          leagues: [] as never[],
          isRecruiting: false,
          createdAt: { toDate: () => new Date('2024-01-01') } as never,
        } as never,
        membership: {
          role: 'owner' as const,
          joinedAt: new Date('2024-01-01'),
          status: 'active' as const,
        } as never,
      });
      expect(presenter.getResponseModel()).toBeDefined();

      presenter.reset();
      expect(presenter.getResponseModel()).toBeNull();
    });
  });

  describe('getResponseModel', () => {
    it('should return null before present()', () => {
      expect(presenter.getResponseModel()).toBeNull();
    });

    it('should return model after present()', () => {
      presenter.present({
        driverId: 'driver-123',
        team: {
          id: 'team-456',
          name: { toString: () => 'Team' } as never,
          tag: { toString: () => 'T' } as never,
          description: { toString: () => '' } as never,
          ownerId: { toString: () => 'driver-123' } as never,
          leagues: [] as never[],
          isRecruiting: false,
          createdAt: { toDate: () => new Date('2024-01-01') } as never,
        } as never,
        membership: {
          role: 'owner' as const,
          joinedAt: new Date('2024-01-01'),
          status: 'active' as const,
        } as never,
      });
      expect(presenter.getResponseModel()).not.toBeNull();
    });
  });
});