import { describe, expect, it } from 'vitest';
import { LeagueMembership } from './LeagueMembership';

describe('LeagueMembership', () => {
  it('should create a league membership', () => {
    const membership = LeagueMembership.create({
      leagueId: 'league1',
      driverId: 'driver1',
      role: 'member',
    });

    expect(membership.id).toBe('league1:driver1');
    expect(membership.leagueId.toString()).toBe('league1');
    expect(membership.driverId.toString()).toBe('driver1');
    expect(membership.role.toString()).toBe('member');
    expect(membership.status.toString()).toBe('pending');
  });

  it('should create with custom id', () => {
    const membership = LeagueMembership.create({
      id: 'custom-id',
      leagueId: 'league1',
      driverId: 'driver1',
      role: 'admin',
      status: 'active',
      joinedAt: new Date('2023-01-01'),
    });

    expect(membership.id).toBe('custom-id');
    expect(membership.role.toString()).toBe('admin');
    expect(membership.status.toString()).toBe('active');
    expect(membership.joinedAt.toDate()).toEqual(new Date('2023-01-01'));
  });

  it('should throw on invalid leagueId', () => {
    expect(() => LeagueMembership.create({
      leagueId: '',
      driverId: 'driver1',
      role: 'member',
    })).toThrow('League ID is required');
  });

  it('should throw on invalid driverId', () => {
    expect(() => LeagueMembership.create({
      leagueId: 'league1',
      driverId: '',
      role: 'member',
    })).toThrow('Driver ID is required');
  });

  it('should throw on invalid role', () => {
    expect(() => LeagueMembership.create({
      leagueId: 'league1',
      driverId: 'driver1',
      role: '',
    })).toThrow('Membership role is required');
  });

  it('should create with valid role', () => {
    const membership = LeagueMembership.create({
      leagueId: 'league1',
      driverId: 'driver1',
      role: 'owner',
    });

    expect(membership.role.toString()).toBe('owner');
  });
});