// lib/features/admin/tabs/admin_alumni_tab.dart

import 'package:flutter/material.dart';

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

  @override
  State<AdminAlumniTab> createState() => _AdminAlumniTabState();
}

class _AdminAlumniTabState extends State<AdminAlumniTab> {
  // Standard Color Palette
  static const Color _standardGold = Color.fromARGB(255, 16, 116, 41);
  static const Color _darkGreen = Color.fromARGB(255, 16, 116, 41);

  // Mock data representing backend alumni requests
  final List<Map<String, dynamic>> _alumniRequests = [
    {
      'id': 'ALU-1001',
      'name': 'Michael K.',
      'gradYear': '2024',
      'course': 'BSc Computer Science',
      'email': 'michael.k@alumni.rockview.ac.zm',
      'status': 'Pending Review'
    },
    {
      'id': 'ALU-1002',
      'name': 'Grace M.',
      'gradYear': '2023',
      'course': 'BSc Information Technology',
      'email': 'grace.m@alumni.rockview.ac.zm',
      'status': 'Verified'
    },
    {
      'id': 'ALU-1003',
      'name': 'David Phiri',
      'gradYear': '2025',
      'course': 'BEd Secondary Education',
      'email': 'david.p@student.rockview.ac.zm',
      'status': 'Pending Review'
    },
  ];

  // =====================================================================
  // ACTIONS: APPROVE, REJECT, REVOKE
  // =====================================================================

  void _confirmApproval(int index, String name) {
    showDialog(
      context: context,
      builder: (ctx) => AlertDialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)), // Flat UI
        backgroundColor: Colors.white,
        title: Row(
          children: [
            Icon(Icons.verified_rounded, color: _darkGreen),
            const SizedBox(width: 8),
            const Text('Approve Alumni', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
          ],
        ),
        content: Text(
          'Are you sure you want to verify $name as an official alumni? They will gain access to the alumni network and resources.',
          style: TextStyle(color: Colors.grey.shade800),
        ),
        actionsPadding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(ctx),
            child: Text('CANCEL', style: TextStyle(color: Colors.grey.shade600, fontWeight: FontWeight.bold)),
          ),
          ElevatedButton(
            style: ElevatedButton.styleFrom(
              backgroundColor: _darkGreen,
              foregroundColor: Colors.white,
              elevation: 0,
              shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
            ),
            onPressed: () {
              setState(() => _alumniRequests[index]['status'] = 'Verified');
              Navigator.pop(ctx);
              _showSnackBar('$name has been successfully verified.', _darkGreen);
            },
            child: const Text('APPROVE'),
          ),
        ],
      ),
    );
  }

  void _confirmRejection(int index, String name) {
    showDialog(
      context: context,
      builder: (ctx) => AlertDialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
        backgroundColor: Colors.white,
        title: Row(
          children: [
            const Icon(Icons.cancel_rounded, color: Colors.redAccent),
            const SizedBox(width: 8),
            const Text('Reject Request', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
          ],
        ),
        content: Text(
          'Are you sure you want to reject the alumni verification request for $name? This will remove the request from the queue.',
          style: TextStyle(color: Colors.grey.shade800),
        ),
        actionsPadding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(ctx),
            child: Text('CANCEL', style: TextStyle(color: Colors.grey.shade600, fontWeight: FontWeight.bold)),
          ),
          ElevatedButton(
            style: ElevatedButton.styleFrom(
              backgroundColor: Colors.redAccent,
              foregroundColor: Colors.white,
              elevation: 0,
              shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
            ),
            onPressed: () {
              setState(() => _alumniRequests.removeAt(index));
              Navigator.pop(ctx);
              _showSnackBar('Request for $name has been rejected.', Colors.black87);
            },
            child: const Text('REJECT'),
          ),
        ],
      ),
    );
  }

  void _confirmRevoke(int index, String name) {
    showDialog(
      context: context,
      builder: (ctx) => AlertDialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
        backgroundColor: Colors.white,
        title: Row(
          children: [
            const Icon(Icons.person_remove_rounded, color: Colors.orange),
            const SizedBox(width: 8),
            const Text('Revoke Status', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
          ],
        ),
        content: Text(
          'Are you sure you want to revoke verified alumni status for $name? They will lose access to the alumni portal.',
          style: TextStyle(color: Colors.grey.shade800),
        ),
        actionsPadding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(ctx),
            child: Text('CANCEL', style: TextStyle(color: Colors.grey.shade600, fontWeight: FontWeight.bold)),
          ),
          ElevatedButton(
            style: ElevatedButton.styleFrom(
              backgroundColor: Colors.orange.shade700,
              foregroundColor: Colors.white,
              elevation: 0,
              shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
            ),
            onPressed: () {
              setState(() => _alumniRequests.removeAt(index));
              Navigator.pop(ctx);
              _showSnackBar('Alumni status for $name has been revoked.', Colors.black87);
            },
            child: const Text('REVOKE'),
          ),
        ],
      ),
    );
  }

  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)),
      ),
    );
  }

  // =====================================================================
  // MAIN BUILD
  // =====================================================================

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // Header Section
          Padding(
            padding: const EdgeInsets.all(32.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const Text(
                      'Alumni Verification Directory',
                      style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: Colors.black87),
                    ),
                    const SizedBox(height: 8),
                    Text(
                      'Review pending requests and manage verified alumni accounts.',
                      style: TextStyle(fontSize: 14, color: Colors.grey.shade600),
                    ),
                  ],
                ),
                Container(
                  padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                  decoration: BoxDecoration(
                    color: Colors.orange.shade50,
                    border: Border.all(color: Colors.orange.shade200),
                    borderRadius: BorderRadius.circular(4),
                  ),
                  child: Row(
                    children: [
                      Icon(Icons.pending_actions_rounded, color: Colors.orange.shade700, size: 20),
                      const SizedBox(width: 8),
                      Text(
                        '${_alumniRequests.where((r) => r['status'] == 'Pending Review').length} Pending',
                        style: TextStyle(color: Colors.orange.shade800, fontWeight: FontWeight.bold),
                      )
                    ],
                  ),
                )
              ],
            ),
          ),
          
          // Data List Section
          Expanded(
            child: _alumniRequests.isEmpty 
              ? _buildEmptyState()
              : ListView.builder(
                  padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 8),
                  itemCount: _alumniRequests.length,
                  itemBuilder: (context, index) {
                    final req = _alumniRequests[index];
                    final bool isVerified = req['status'] == 'Verified';

                    return Container(
                      margin: const EdgeInsets.only(bottom: 16),
                      padding: const EdgeInsets.all(20),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        border: Border.all(
                          color: isVerified ? _darkGreen.withOpacity(0.3) : Colors.orange.shade300, 
                          width: isVerified ? 1 : 1.5
                        ),
                        borderRadius: BorderRadius.circular(4), // Flat UI
                      ),
                      child: Row(
                        children: [
                          // Status Icon / Avatar
                          Container(
                            height: 60,
                            width: 60,
                            decoration: BoxDecoration(
                              color: isVerified ? _darkGreen.withOpacity(0.1) : Colors.orange.withOpacity(0.1),
                              borderRadius: BorderRadius.circular(4),
                              border: Border.all(color: isVerified ? _darkGreen.withOpacity(0.2) : Colors.orange.withOpacity(0.2))
                            ),
                            child: Icon(
                              isVerified ? Icons.school_rounded : Icons.history_edu_rounded, 
                              color: isVerified ? _darkGreen : Colors.orange.shade800, 
                              size: 32
                            ),
                          ),
                          const SizedBox(width: 20),
                          
                          // Alumni Info
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Row(
                                  children: [
                                    Text(req['name'], style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16, color: Colors.black87)),
                                    const SizedBox(width: 12),
                                    Container(
                                      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
                                      decoration: BoxDecoration(
                                        color: isVerified ? _darkGreen.withOpacity(0.1) : Colors.orange.withOpacity(0.1),
                                        borderRadius: BorderRadius.circular(2),
                                      ),
                                      child: Text(
                                        req['status'].toUpperCase(),
                                        style: TextStyle(
                                          color: isVerified ? _darkGreen : Colors.orange.shade800,
                                          fontWeight: FontWeight.bold,
                                          fontSize: 10,
                                          letterSpacing: 0.5,
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                                const SizedBox(height: 6),
                                Text(
                                  '${req['course']} • Class of ${req['gradYear']}', 
                                  style: TextStyle(color: Colors.grey.shade700, fontWeight: FontWeight.w500)
                                ),
                                const SizedBox(height: 4),
                                Text(
                                  req['email'], 
                                  style: TextStyle(color: Colors.grey.shade500, fontSize: 13)
                                ),
                              ],
                            ),
                          ),
                          
                          // Action Buttons
                          if (!isVerified) ...[
                            TextButton.icon(
                              onPressed: () => _confirmRejection(index, req['name']),
                              icon: const Icon(Icons.cancel_outlined, color: Colors.redAccent, size: 20),
                              label: const Text('Reject', style: TextStyle(color: Colors.redAccent, fontWeight: FontWeight.bold)),
                            ),
                            const SizedBox(width: 12),
                            ElevatedButton.icon(
                              onPressed: () => _confirmApproval(index, req['name']),
                              style: ElevatedButton.styleFrom(
                                backgroundColor: _darkGreen,
                                foregroundColor: Colors.white,
                                elevation: 0,
                                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
                              ),
                              icon: const Icon(Icons.check_circle_outline, size: 20),
                              label: const Text('Approve', style: TextStyle(fontWeight: FontWeight.bold)),
                            ),
                          ] else ...[
                            OutlinedButton.icon(
                              onPressed: () => _confirmRevoke(index, req['name']),
                              icon: const Icon(Icons.person_remove_outlined, size: 18),
                              label: const Text('Revoke Status'),
                              style: OutlinedButton.styleFrom(
                                foregroundColor: Colors.orange.shade700, 
                                side: BorderSide(color: Colors.orange.shade300),
                                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
                              ),
                            )
                          ]
                        ],
                      ),
                    );
                  },
                ),
          ),
        ],
      ),
    );
  }

  // =====================================================================
  // HELPER WIDGETS
  // =====================================================================
  
  Widget _buildEmptyState() {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 32),
      child: Container(
        width: double.infinity,
        padding: const EdgeInsets.symmetric(vertical: 64),
        decoration: BoxDecoration(
          color: Colors.grey.shade50,
          borderRadius: BorderRadius.circular(4),
          border: Border.all(color: Colors.grey.shade200, width: 1),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(Icons.done_all_rounded, size: 48, color: Colors.grey.shade400),
            const SizedBox(height: 16),
            Text('No Pending Requests', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.grey.shade800)),
            const SizedBox(height: 8),
            Text('All alumni verification requests have been processed.', style: TextStyle(color: Colors.grey.shade600)),
          ],
        ),
      ),
    );
  }
}