import 'package:flutter_riverpod/flutter_riverpod.dart';

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

final matchesProvider = FutureProvider<List<MatchModel>>((ref) async {
  final auth = ref.watch(authProvider);
  if (!auth.isAuthenticated) return [];
  final team = await ref.watch(activeTeamProvider.future);
  if (team == null) return [];
  final client = ref.read(apiClientProvider);
  final list = await client.fetchMatches(team.id);
  list.sort((a, b) {
    final sa = a.scheduledAt;
    final sb = b.scheduledAt;
    if (sa == null && sb == null) return 0;
    if (sa == null) return 1;
    if (sb == null) return -1;
    return sa.compareTo(sb);
  });
  return list;
});

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 selectedTeamIdProvider =
    AsyncNotifierProvider<SelectedTeamIdNotifier, String?>(SelectedTeamIdNotifier.new);

class SelectedTeamIdNotifier extends AsyncNotifier<String?> {
  @override
  Future<String?> build() => TeamSelectionStorage.load();

  Future<void> select(String teamId) async {
    await TeamSelectionStorage.save(teamId);
    state = AsyncData(teamId);
  }
}

/// Squadra attiva: preferisce la selezione salvata, altrimenti la prima disponibile.
final activeTeamProvider = FutureProvider<Team?>((ref) async {
  final teams = await ref.watch(teamsProvider.future);
  if (teams.isEmpty) return null;

  var selectedId = ref.watch(selectedTeamIdProvider).valueOrNull;
  final valid = selectedId != null && teams.any((t) => t.id == selectedId);

  if (!valid) {
    selectedId = teams.first.id;
    await ref.read(selectedTeamIdProvider.notifier).select(selectedId);
  }

  return teams.firstWhere((t) => t.id == selectedId);
});

/// Compatibilità con codice esistente.
final primaryTeamProvider = activeTeamProvider;

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);
});
