// lib/features/admin/tabs/clubs/pending_clubs_sub_tab.dart

import 'package:flutter/material.dart';

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

  @override
  State<PendingClubsSubTab> createState() => _PendingClubsSubTabState();
}

class _PendingClubsSubTabState extends State<PendingClubsSubTab> {
  static const Color _darkGreen = Color.fromARGB(255, 16, 116, 41);
  static const Color _darkBlue = Color.fromARGB(255, 12, 53, 106);

  // Mock data - Pending only
  final List<Map<String, dynamic>> _pendingClubs = [
    {
      'id': 'CLUB-002',
      'name': 'Campus Photography Club',
      'president': 'Alice B.',
      'members': 12,
      'status': 'Pending Approval',
    },
    {
      'id': 'CLUB-003',
      'name': 'ICT Innovators Guild',
      'president': 'David Phiri',
      'members': 34,
      'status': 'Pending Approval',
    }
  ];

  void _confirmApproval(int index, String name) {
    showDialog(
      context: context,
      builder: (ctx) => AlertDialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
        backgroundColor: Colors.white,
        title: Row(
          children: [
            Icon(Icons.check_circle_outline, color: _darkGreen),
            const SizedBox(width: 8),
            const Text('Approve Club', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
          ],
        ),
        content: Text(
          'Are you sure you want to officially approve the "$name"? They will be listed publicly on the student app.',
          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(() => _pendingClubs.removeAt(index));
              Navigator.pop(ctx);
              _showSnackBar('"$name" has been approved and moved to the directory.', _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.close_rounded, color: Colors.redAccent),
            const SizedBox(width: 8),
            const Text('Reject Registration', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
          ],
        ),
        content: Text(
          'Are you sure you want to reject the registration request for "$name"? This will remove them 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(() => _pendingClubs.removeAt(index));
              Navigator.pop(ctx);
              _showSnackBar('Registration for "$name" was rejected.', Colors.black87);
            },
            child: const Text('REJECT'),
          ),
        ],
      ),
    );
  }

  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 Scaffold(
      backgroundColor: Colors.transparent,
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: const EdgeInsets.fromLTRB(32, 32, 32, 16),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const Text(
                      'Pending Registrations',
                      style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: Colors.black87),
                    ),
                    const SizedBox(height: 8),
                    Text(
                      'Review and approve new club applications.',
                      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(
                        '${_pendingClubs.length} in Queue',
                        style: TextStyle(color: Colors.orange.shade800, fontWeight: FontWeight.bold),
                      )
                    ],
                  ),
                )
              ],
            ),
          ),
          
          Expanded(
            child: _pendingClubs.isEmpty
              ? _buildEmptyState()
              : ListView.builder(
                  padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 16),
                  itemCount: _pendingClubs.length,
                  itemBuilder: (context, index) {
                    final club = _pendingClubs[index];

                    return Container(
                      margin: const EdgeInsets.only(bottom: 16),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        border: Border.all(color: Colors.orange.shade300, width: 1.5),
                        borderRadius: BorderRadius.circular(4),
                      ),
                      child: Padding(
                        padding: const EdgeInsets.all(24),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Row(
                                  children: [
                                    Container(
                                      padding: const EdgeInsets.all(12),
                                      decoration: BoxDecoration(
                                        color: _darkBlue.withOpacity(0.05),
                                        borderRadius: BorderRadius.circular(4),
                                        border: Border.all(color: _darkBlue.withOpacity(0.1)),
                                      ),
                                      child: const Icon(Icons.groups_rounded, color: _darkBlue, size: 28),
                                    ),
                                    const SizedBox(width: 20),
                                    Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Text(club['name'], style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18, color: Colors.black87)),
                                        const SizedBox(height: 6),
                                        Text(
                                          'President: ${club['president']}  •  ${club['members']} Members', 
                                          style: TextStyle(color: Colors.grey.shade600, fontSize: 13, fontWeight: FontWeight.w500)
                                        ),
                                      ],
                                    ),
                                  ],
                                ),
                              ],
                            ),
                            
                            const SizedBox(height: 20),
                            const Divider(height: 1),
                            const SizedBox(height: 20),
                            
                            Row(
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: [
                                TextButton.icon(
                                  onPressed: () => _confirmRejection(index, club['name']),
                                  icon: const Icon(Icons.close_rounded, color: Colors.redAccent, size: 20),
                                  label: const Text('Reject', style: TextStyle(color: Colors.redAccent, fontWeight: FontWeight.bold)),
                                ),
                                const SizedBox(width: 16),
                                ElevatedButton.icon(
                                  onPressed: () => _confirmApproval(index, club['name']),
                                  style: ElevatedButton.styleFrom(
                                    backgroundColor: _darkGreen,
                                    foregroundColor: Colors.white,
                                    elevation: 0,
                                    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
                                    padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
                                  ),
                                  icon: const Icon(Icons.check_circle_outline, size: 18),
                                  label: const Text('Approve Club', style: TextStyle(fontWeight: FontWeight.bold)),
                                ),
                              ],
                            )
                          ],
                        ),
                      ),
                    );
                  },
                ),
          ),
        ],
      ),
    );
  }

  Widget _buildEmptyState() {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 16),
      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.check_circle_outline_rounded, size: 48, color: Colors.grey.shade400),
            const SizedBox(height: 16),
            Text('Queue Empty', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.grey.shade800)),
            const SizedBox(height: 8),
            Text('There are no pending club registrations at this time.', style: TextStyle(color: Colors.grey.shade600)),
          ],
        ),
      ),
    );
  }
}