// lib\app\modules\notification\controllers\notification_controller.dart
import 'package:get/get.dart';

class NotificationItem {
  final String id;
  final String title;
  final String message;
  final String room;
  final String patientName;
  final DateTime time;
  final String deviceId;
  final bool isRead;

  NotificationItem({
    required this.id,
    required this.title,
    required this.message,
    required this.room,
    required this.deviceId,
    required this.patientName,
    required this.time,
    this.isRead = false,
  });

  NotificationItem copyWith({
    String? id,
    String? title,
    String? message,
    String? room,
    String? patientName,
    DateTime? time,
    String? deviceId,
    bool? isRead,
  }) {
    return NotificationItem(
      id: id ?? this.id,
      title: title ?? this.title,
      message: message ?? this.message,
      room: room ?? this.room,
      deviceId: deviceId ?? this.deviceId,
      patientName: patientName ?? this.patientName,
      time: time ?? this.time,
      isRead: isRead ?? this.isRead,
    );
  }
}

class NotificationController extends GetxController {
  final selectedTab = 0.obs;
  
  final _allNotifications = <NotificationItem>[
    NotificationItem(
      id: '1',
      title: 'Peringatan!!',
      message: 'Aliran infus telah terhenti. Segera periksa pasien dan ganti selang infus.',
      room: 'MAWAR 002',
      deviceId: 'SI001',
      patientName: 'Mrs Shinta',
      time: DateTime.now().subtract(const Duration(minutes: 5)),
      isRead: false,
    ),
    NotificationItem(
      id: '2',
      title: 'Peringatan!!',
      message: 'Aliran infus telah terhenti. Segera periksa pasien dan ganti selang infus.',
      room: 'MAWAR 003',
      deviceId: 'SI002',
      patientName: 'Mr Budi',
      time: DateTime.now().subtract(const Duration(hours: 2)),
      isRead: false,
    ),
    NotificationItem(
      id: '3',
      title: 'Peringatan!!',
      message: 'Aliran infus telah terhenti. Segera periksa pasien dan ganti selang infus.',
      room: 'MAWAR 004',
      deviceId: 'SI003',
      patientName: 'Mrs Ani',
      time: DateTime.now().subtract(const Duration(hours: 5)),
      isRead: true,
    ),
    NotificationItem(
      id: '4',
      title: 'Peringatan!!',
      message: 'Aliran infus telah terhenti. Segera periksa pasien dan ganti selang infus.',
      room: 'MAWAR 005',
      deviceId: 'SI004',
      patientName: 'Mr Doni',
      time: DateTime.now().subtract(const Duration(days: 1)),
      isRead: true,
    ),
  ].obs;

  // Computed property untuk notifikasi yang difilter
  List<NotificationItem> get notifications {
    if (selectedTab.value == 0) {
      // Tab "Baru" - notifikasi yang belum dibaca
      return _allNotifications.where((notif) => !notif.isRead).toList();
    } else {
      // Tab "Sudah dibaca" - notifikasi yang sudah dibaca
      return _allNotifications.where((notif) => notif.isRead).toList();
    }
  }

  void changeTab(int index) {
    selectedTab.value = index;
  }

  void deleteNotification(String id) {
    _allNotifications.removeWhere((notif) => notif.id == id);
  }

  void markAsRead(String id) {
    final index = _allNotifications.indexWhere((notif) => notif.id == id);
    if (index != -1) {
      _allNotifications[index] = _allNotifications[index].copyWith(isRead: true);
      _allNotifications.refresh();
    }
  }

  String getTimeAgo(DateTime time) {
    final difference = DateTime.now().difference(time);
    if (difference.inMinutes < 60) {
      return '${difference.inMinutes}min ago';
    } else if (difference.inHours < 24) {
      return '${difference.inHours}h ago';
    } else {
      return '${difference.inDays}d ago';
    }
  }

  // Method untuk menghitung jumlah notifikasi baru
  int get unreadCount {
    return _allNotifications.where((notif) => !notif.isRead).length;
  }
}