/// Notification Repository Implementation
///
/// Concrete implementation of NotificationRepository.
/// Coordinates between local datasource and domain layer.
library;

import 'package:worker/features/notifications/data/datasources/notification_local_datasource.dart';
import 'package:worker/features/notifications/data/models/notification_model.dart';
import 'package:worker/features/notifications/domain/entities/notification.dart';
import 'package:worker/features/notifications/domain/repositories/notification_repository.dart';

/// Notification Repository Implementation
class NotificationRepositoryImpl implements NotificationRepository {
  /// Local data source
  final NotificationLocalDataSource localDataSource;

  /// Constructor
  NotificationRepositoryImpl({required this.localDataSource});

  @override
  Future<List<Notification>> getAllNotifications() async {
    final jsonList = await localDataSource.getAllNotifications();
    return jsonList.map((json) => NotificationModel.fromJson(json)).toList()
      ..sort(
        (a, b) => b.createdAt.compareTo(a.createdAt),
      ); // Sort by newest first
  }

  @override
  Future<List<Notification>> getNotificationsByCategory(String category) async {
    final jsonList = await localDataSource.getNotificationsByCategory(category);
    return jsonList.map((json) => NotificationModel.fromJson(json)).toList()
      ..sort((a, b) => b.createdAt.compareTo(a.createdAt));
  }

  @override
  Future<int> getUnreadCount() async {
    return await localDataSource.getUnreadCount();
  }

  @override
  Future<void> markAsRead(String notificationId) async {
    await localDataSource.markAsRead(notificationId);
  }

  @override
  Future<void> markAllAsRead() async {
    await localDataSource.markAllAsRead();
  }

  @override
  Future<List<Notification>> refreshNotifications() async {
    // For now, just fetch all notifications
    // In production, this would fetch from server
    return await getAllNotifications();
  }
}
