import type { Season } from '../types';

export async function fetchSeasons(): Promise<Season[]> {
  try {
    const res = await fetch('/api/seasons');
    if (!res.ok) return [];
    return res.json();
  } catch {
    console.warn('Failed to load seasons, using empty list');
    return [];
  }
}

export async function createSeason(season: Omit<Season, 'Id'>): Promise<Season> {
  const res = await fetch('/api/seasons', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(season),
  });
  if (!res.ok) throw new Error('Failed to create season');
  return res.json();
}

export async function updateSeason(id: number, season: Partial<Season>): Promise<Season> {
  const res = await fetch(`/api/seasons/${id}`, {
    method: 'PUT',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(season),
  });
  if (!res.ok) throw new Error('Failed to update season');
  return res.json();
}

export async function deleteSeason(id: number): Promise<void> {
  const res = await fetch(`/api/seasons/${id}`, { method: 'DELETE' });
  if (!res.ok) throw new Error('Failed to delete season');
}
