import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../providers/auth_provider.dart';
import '../../shared/api_client.dart';
import '../../shared/models/recording_item.dart';
import '../../shared/models/team.dart';

final teamsProvider = FutureProvider<List<Team>>((ref) async {
  final auth = ref.watch(authProvider);
  if (!auth.isAuthenticated) return [];
  final client = ref.read(apiClientProvider);
  return client.fetchTeams();
});

final primaryTeamProvider = FutureProvider<Team?>((ref) async {
  final teams = await ref.watch(teamsProvider.future);
  if (teams.isEmpty) return null;
  return teams.first;
});

final teamByIdProvider = FutureProvider.family<Team?, String>((ref, teamId) async {
  final teams = await ref.watch(teamsProvider.future);
  for (final t in teams) {
    if (t.id == teamId) return t;
  }
  return null;
});

final recordingsProvider = FutureProvider.family<List<RecordingItem>, String>((ref, teamId) async {
  final team = await ref.watch(teamByIdProvider(teamId).future);
  if (team == null || !team.recordingsEnabled) return [];
  final client = ref.read(apiClientProvider);
  return client.fetchRecordings(teamId);
});
