import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'supabase_provider.dart';
import '../models/team.dart';
import '../models/team_member.dart';

/// Real-time stream of teams (keeps UI in sync with DB changes).
final teamsProvider = StreamProvider<List<Team>>((ref) {
  final client = ref.watch(supabaseClientProvider);
  return client
      .from('teams')
      .stream(primaryKey: ['id'])
      .order('name')
      .map((rows) => rows.map((r) => Team.fromMap(r)).toList());
});

/// Real-time stream of team membership rows.
final teamMembersProvider = StreamProvider<List<TeamMember>>((ref) {
  final client = ref.watch(supabaseClientProvider);
  return client
      .from('team_members')
      .stream(primaryKey: ['team_id', 'user_id'])
      .map((rows) => rows.map((r) => TeamMember.fromMap(r)).toList());
});
