// lib\app\modules\notification\views\notification_view.dart
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controllers/notification_controller.dart';
import '../../../widgets/notification_card.dart';

class NotificationView extends GetView<NotificationController> {
  const NotificationView({super.key});
  
  @override
  Widget build(BuildContext context) {
    Get.put(NotificationController());
    
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              color: Colors.white,
              padding: const EdgeInsets.all(20),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const Text(
                    'History Notifikasi',
                    style: TextStyle(
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  const SizedBox(height: 20),
                  Obx(
                    () => Row(
                      children: [
                        Expanded(
                          child: _buildTabButton('Baru', 0),
                        ),
                        const SizedBox(width: 10),
                        Expanded(
                          child: _buildTabButton('Sudah dibaca', 1),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            const SizedBox(height: 10),
            Expanded(
              child: Obx(
                () {
                  final notificationList = controller.notifications;
                  
                  if (notificationList.isEmpty) {
                    return Center(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Icon(
                            Icons.notifications_none,
                            size: 80,
                            color: Colors.grey[400],
                          ),
                          const SizedBox(height: 16),
                          Text(
                            controller.selectedTab.value == 0
                                ? 'Tidak ada notifikasi baru'
                                : 'Tidak ada notifikasi yang dibaca',
                            style: TextStyle(
                              fontSize: 16,
                              color: Colors.grey[600],
                            ),
                          ),
                        ],
                      ),
                    );
                  }
                  
                  return ListView.builder(
                    padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
                    itemCount: notificationList.length,
                    itemBuilder: (context, index) {
                      final notif = notificationList[index];
                      return NotificationCard(
                        notification: notif,
                        onDismissed: () {
                          controller.deleteNotification(notif.id);
                          Get.snackbar(
                            'Notifikasi Dihapus',
                            'Notifikasi telah dihapus',
                            snackPosition: SnackPosition.BOTTOM,
                            duration: const Duration(seconds: 2),
                            margin: const EdgeInsets.all(16),
                            backgroundColor: Colors.grey[800],
                            colorText: Colors.white,
                          );
                        },
                        onTap: !notif.isRead ? () {
                          controller.markAsRead(notif.id);
                          
                          // Delay untuk animasi sebelum pindah tab
                          Future.delayed(const Duration(milliseconds: 300), () {
                            controller.changeTab(1);
                          });
                          
                          Get.snackbar(
                            'Notifikasi Dibaca',
                            'Notifikasi telah dipindahkan ke sudah dibaca',
                            snackPosition: SnackPosition.BOTTOM,
                            duration: const Duration(seconds: 2),
                            margin: const EdgeInsets.all(16),
                            backgroundColor: const Color(0xFF0091EA),
                            colorText: Colors.white,
                          );
                        } : null,
                      );
                    },
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildTabButton(String title, int index) {
    final isSelected = controller.selectedTab.value == index;
    return GestureDetector(
      onTap: () => controller.changeTab(index),
      child: Container(
        padding: const EdgeInsets.symmetric(vertical: 12),
        decoration: BoxDecoration(
          color: isSelected ? const Color(0xFF0091EA) : Colors.transparent,
          borderRadius: BorderRadius.circular(10),
          border: Border.all(
            color: isSelected ? const Color(0xFF0091EA) : Colors.grey[300]!,
          ),
        ),
        child: Text(
          title,
          textAlign: TextAlign.center,
          style: TextStyle(
            color: isSelected ? Colors.white : Colors.grey[600],
            fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
            fontSize: 15,
          ),
        ),
      ),
    );
  }
}