// Version: 1.2.0 | Created: 2026-04-01 | Updated: 2026-04-02
// MatrixRTC repository — handles outgoing call invites and detects incoming
// calls via m.call.invite events per MSC4143 (MatrixRTC spec).
//
// Incoming calls are surfaced via the incomingCallStream so the
// IncomingCallOverlay can react to them.

import 'dart:async';

import 'package:matrix/matrix.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import '../../../core/auth/auth_notifier.dart';
import '../../../core/auth/auth_state.dart';
import '../../../core/network/matrix_client.dart';
import '../../../shared/utils/matrix_id.dart';
import '../../../shared/utils/mxc_url.dart';
import '../domain/incoming_call.dart';

part 'matrixrtc_repository.g.dart';

/// Repository for sending and receiving MatrixRTC call signalling events.
class MatrixRtcRepository {
  MatrixRtcRepository({required Client client, required String? myUserId})
    : _client = client,
      _myUserId = myUserId;

  final Client _client;
  final String? _myUserId;

  final _incomingCallController = StreamController<IncomingCall>.broadcast();

  /// Emits whenever an incoming call invite arrives for the local user.
  Stream<IncomingCall> get incomingCallStream => _incomingCallController.stream;

  StreamSubscription<EventUpdate>? _eventSubscription;

  /// Begin listening for incoming m.call.invite events.
  void startListening() {
    _eventSubscription?.cancel();
    _eventSubscription = _client.onEvent.stream.listen(_onEvent);
  }

  void stopListening() {
    _eventSubscription?.cancel();
    _eventSubscription = null;
  }

  /// Close the stream controller. Called from ref.onDispose only.
  void dispose() {
    stopListening();
    _incomingCallController.close();
  }

  /// Send a call invite to [roomId] to start a voice or video call.
  ///
  /// Uses the standard m.call.invite event type. The room's other participants
  /// will receive this via their sync stream.
  Future<void> sendCallInvite({
    required String roomId,
    required bool isVideo,
  }) async {
    final room = _client.getRoomById(roomId);
    if (room == null) return;

    final callId = 'call_${DateTime.now().millisecondsSinceEpoch}';

    await room.sendEvent({
      'msgtype': EventTypes.CallInvite,
      'call_id': callId,
      'lifetime': 60000, // 60 seconds before invite expires
      'offer': {
        'type': 'offer',
        'sdp': '', // SDP is populated by LiveKit once connected
      },
      'version': '1',
      'invitee': null, // null = invite entire room
      'm.intentional_mentions': {'user_ids': [], 'room': false},
    }, type: EventTypes.CallInvite);
  }

  void _onEvent(EventUpdate update) {
    if (update.type != EventUpdateType.timeline) return;
    if (update.content['type'] != EventTypes.CallInvite) return;

    final senderId = update.content['sender'] as String?;
    // Ignore our own invites.
    if (senderId == _myUserId) return;

    final roomId = update.roomID;
    final content = update.content['content'] as Map<String, dynamic>?;
    if (content == null) return;

    final callId = content['call_id'] as String?;
    if (callId == null) return;

    final room = _client.getRoomById(roomId);
    final senderProfile = room?.unsafeGetUserFromMemoryOrFallback(
      senderId ?? '',
    );

    _incomingCallController.add(
      IncomingCall(
        callId: callId,
        roomId: roomId,
        callerId: senderId ?? '',
        callerDisplayName:
            senderProfile?.displayName ?? senderId?.matrixLocalpart ?? '',
        callerAvatarUrl: resolveMxcUrl(_client, senderProfile?.avatarUrl),
        isVideo: (content['offer'] != null),
      ),
    );
  }
}

@Riverpod(keepAlive: true)
MatrixRtcRepository matrixRtcRepository(Ref ref) {
  final client = ref.watch(matrixClientProvider);
  final authState = ref.watch(authProvider);
  final myUserId = authState is AuthAuthenticated ? authState.userId : null;

  final repo = MatrixRtcRepository(client: client, myUserId: myUserId);
  repo.startListening();
  ref.onDispose(repo.dispose);
  return repo;
}
