import 'package:dio/dio.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../core/network/dio_client.dart';
import 'in_site_notification.dart';

class InSiteNotificationRepository {
  final Dio _dio;

  InSiteNotificationRepository(this._dio);

  Future<({List<InSiteNotification> items, int total})> getMyNotifications({
    int limit = 50,
    int offset = 0,
  }) async {
    final res = await _dio.get(
      '/api/v1/notifications/me',
      queryParameters: {'limit': limit, 'offset': offset},
    );
    final data = res.data as Map<String, dynamic>;
    final items = (data['items'] as List)
        .map((e) => InSiteNotification.fromJson(e as Map<String, dynamic>))
        .toList();
    return (items: items, total: data['total'] as int? ?? 0);
  }

  Future<int> getUnreadCount() async {
    final res = await _dio.get('/api/v1/notifications/me/unread-count');
    final data = res.data as Map<String, dynamic>;
    return data['count'] as int? ?? 0;
  }

  Future<void> markRead(String notificationId) async {
    await _dio.post(
      '/api/v1/notifications/me/mark-read',
      data: {'notificationId': notificationId},
    );
  }

  Future<void> markAllRead() async {
    await _dio.post('/api/v1/notifications/me/mark-read', data: {});
  }
}

// ── Riverpod provider ──────────────────────────────────────────────────────

final inSiteNotificationRepositoryProvider =
    Provider<InSiteNotificationRepository>((ref) {
  final dio = ref.watch(dioClientProvider);
  return InSiteNotificationRepository(dio);
});
