// lib/features/admin/tabs/admin_saved_screen.dart

import 'package:flutter/material.dart';

class AdminSavedScreen extends StatefulWidget {
  const AdminSavedScreen({super.key});

  @override
  State<AdminSavedScreen> createState() => _AdminSavedScreenState();
}

class _AdminSavedScreenState extends State<AdminSavedScreen> {
  // Standard Color Palette
  static const Color _darkGreen = Color.fromARGB(255, 16, 116, 41);
  static const Color _darkBlue = Color.fromARGB(255, 12, 53, 106);

  // Mock data for drafts
  final List<Map<String, dynamic>> _drafts = [
    {
      'id': 'D-101',
      'icon': Icons.edit_document,
      'title': 'ROCKVIEW UNIVERSITY - NDOLA CAMPUS - WEEKLY ANNOUNCEMENTS',
      'subtitle': 'Last edited 2 hours ago',
    },
    {
      'id': 'D-102',
      'icon': Icons.schedule_rounded,
      'title': 'Mid-Term Test Schedules',
      'subtitle': 'Last edited yesterday',
    },
  ];

  // Mock data for bookmarks
  final List<Map<String, dynamic>> _bookmarks = [
    {
      'id': 'B-201',
      'icon': Icons.gavel_rounded,
      'title': 'Investigate Market Listing #4092',
      'subtitle': 'Saved yesterday',
      'isAlert': true,
    },
    {
      'id': 'B-202',
      'icon': Icons.report_gmailerrorred_rounded,
      'title': 'Review User Flag: "John Smith"',
      'subtitle': 'Saved 3 days ago',
      'isAlert': true,
    },
    {
      'id': 'B-203',
      'icon': Icons.bookmark_outline,
      'title': 'Alumni Verification #ALU-1003',
      'subtitle': 'Saved 1 week ago',
      'isAlert': false,
    },
  ];

  void _removeDraft(int index) {
    final title = _drafts[index]['title'];
    setState(() => _drafts.removeAt(index));
    _showSnackBar('Draft deleted: $title', Colors.black87);
  }

  void _removeBookmark(int index) {
    setState(() => _bookmarks.removeAt(index));
    _showSnackBar('Bookmark removed.', Colors.black87);
  }

  void _showSnackBar(String message, Color color) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(message, style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold)),
        backgroundColor: color,
        behavior: SnackBarBehavior.floating,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 2,
      child: Scaffold(
        backgroundColor: Colors.white,
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // --- HEADER ---
            Padding(
              padding: const EdgeInsets.fromLTRB(32, 32, 32, 16),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    'Admin Workspace', 
                    style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: Colors.grey.shade900)
                  ),
                  const SizedBox(height: 8),
                  Text(
                    'Manage your saved reports and incomplete drafts.', 
                    style: TextStyle(fontSize: 14, color: Colors.grey.shade600)
                  ),
                ],
              ),
            ),
            
            // --- TAB BAR ---
            Container(
              decoration: BoxDecoration(
                border: Border(bottom: BorderSide(color: Colors.grey.shade200)),
              ),
              child: const TabBar(
                labelColor: _darkGreen,
                unselectedLabelColor: Colors.grey,
                indicatorColor: _darkGreen,
                indicatorWeight: 3,
                tabs: [
                  Tab(icon: Icon(Icons.drafts_outlined), text: 'Draft Announcements'),
                  Tab(icon: Icon(Icons.bookmarks_outlined), text: 'Bookmarked Reports'),
                ],
              ),
            ),
            
            // --- TAB VIEWS ---
            Expanded(
              child: TabBarView(
                children: [
                  // DRAFTS TAB
                  _drafts.isEmpty 
                    ? _buildEmptyState(Icons.drafts_outlined, 'No Drafts', 'You currently have no saved drafts.')
                    : ListView.builder(
                        padding: const EdgeInsets.all(32),
                        itemCount: _drafts.length,
                        itemBuilder: (context, index) {
                          final draft = _drafts[index];
                          return _buildWorkspaceItem(
                            icon: draft['icon'],
                            title: draft['title'],
                            subtitle: draft['subtitle'],
                            actionText: 'Resume Editing',
                            actionColor: _darkBlue,
                            isAlert: false,
                            onRemove: () => _removeDraft(index),
                          );
                        },
                      ),
                  
                  // BOOKMARKS TAB
                  _bookmarks.isEmpty 
                    ? _buildEmptyState(Icons.bookmark_border_rounded, 'No Bookmarks', 'You have no saved items requiring review.')
                    : ListView.builder(
                        padding: const EdgeInsets.all(32),
                        itemCount: _bookmarks.length,
                        itemBuilder: (context, index) {
                          final bookmark = _bookmarks[index];
                          return _buildWorkspaceItem(
                            icon: bookmark['icon'],
                            title: bookmark['title'],
                            subtitle: bookmark['subtitle'],
                            actionText: 'View Details',
                            actionColor: bookmark['isAlert'] ? Colors.orange.shade700 : _darkGreen,
                            isAlert: bookmark['isAlert'],
                            onRemove: () => _removeBookmark(index),
                          );
                        },
                      ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  // =====================================================================
  // FLAT CUSTOM LIST ITEM
  // =====================================================================
  Widget _buildWorkspaceItem({
    required IconData icon, 
    required String title, 
    required String subtitle, 
    required String actionText, 
    required Color actionColor,
    required bool isAlert,
    required VoidCallback onRemove,
  }) {
    return Container(
      margin: const EdgeInsets.only(bottom: 16),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border.all(color: isAlert ? Colors.orange.shade300 : Colors.grey.shade300, width: 1),
        borderRadius: BorderRadius.circular(4), // Flat UI
      ),
      child: IntrinsicHeight(
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            // Left Status Bar indicator
            Container(
              width: 4,
              decoration: BoxDecoration(
                color: isAlert ? Colors.orange.shade400 : Colors.grey.shade300,
                borderRadius: const BorderRadius.horizontal(left: Radius.circular(3)),
              ),
            ),
            
            Expanded(
              child: Padding(
                padding: const EdgeInsets.all(20),
                child: Row(
                  children: [
                    // Icon Box
                    Container(
                      padding: const EdgeInsets.all(12),
                      decoration: BoxDecoration(
                        color: isAlert ? Colors.orange.withOpacity(0.1) : Colors.grey.shade100, 
                        borderRadius: BorderRadius.circular(4),
                        border: Border.all(color: isAlert ? Colors.orange.withOpacity(0.2) : Colors.transparent)
                      ),
                      child: Icon(icon, color: isAlert ? Colors.orange.shade700 : Colors.grey.shade700),
                    ),
                    const SizedBox(width: 20),
                    
                    // Text Details
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(title, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16, color: Colors.black87)),
                          const SizedBox(height: 4),
                          Text(subtitle, style: TextStyle(color: Colors.grey.shade500, fontSize: 13)),
                        ],
                      ),
                    ),
                    const SizedBox(width: 16),
                    
                    // Actions
                    OutlinedButton(
                      onPressed: onRemove,
                      style: OutlinedButton.styleFrom(
                        foregroundColor: Colors.grey.shade600,
                        side: BorderSide(color: Colors.grey.shade300),
                        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
                        padding: const EdgeInsets.all(16),
                      ),
                      child: const Icon(Icons.delete_outline, size: 20),
                    ),
                    const SizedBox(width: 12),
                    ElevatedButton(
                      onPressed: () {},
                      style: ElevatedButton.styleFrom(
                        backgroundColor: actionColor,
                        foregroundColor: Colors.white,
                        elevation: 0,
                        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
                        padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
                      ),
                      child: Text(actionText, style: const TextStyle(fontWeight: FontWeight.bold)),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  // =====================================================================
  // EMPTY STATE HELPER
  // =====================================================================
  Widget _buildEmptyState(IconData icon, String title, String message) {
    return Container(
      alignment: Alignment.center,
      padding: const EdgeInsets.all(32),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            padding: const EdgeInsets.all(24),
            decoration: BoxDecoration(
              color: Colors.grey.shade50,
              shape: BoxShape.circle,
            ),
            child: Icon(icon, size: 64, color: Colors.grey.shade300),
          ),
          const SizedBox(height: 24),
          Text(title, style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: Colors.grey.shade800)),
          const SizedBox(height: 8),
          Text(message, style: TextStyle(color: Colors.grey.shade600)),
        ],
      ),
    );
  }
}