import { Result } from '@/lib/contracts/Result';
import { LeagueService } from '@/lib/services/leagues/LeagueService';
import type { CreateLeagueInputDTO } from '@/lib/types/generated/CreateLeagueInputDTO';
import { DomainError } from '@/lib/contracts/services/Service';

/**
 * CreateLeagueMutation
 * 
 * Framework-agnostic mutation for creating leagues.
 * Can be called from Server Actions or other contexts.
 */
export class CreateLeagueMutation {
  private service: LeagueService;

  constructor() {
    this.service = new LeagueService();
  }

  async execute(input: CreateLeagueInputDTO): Promise<Result<string, DomainError>> {
    try {
      const result = await this.service.createLeague(input);
      if (result.isErr()) {
        return Result.err(result.getError());
      }
      return Result.ok(result.unwrap().leagueId);
    } catch (error: any) {
      console.error('CreateLeagueMutation failed:', error);
      return Result.err({ type: 'serverError', message: error.message || 'Failed to create league' });
    }
  }
}
