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

import 'package:flutter/material.dart';

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

  @override
  State<ClubDirectorySubTab> createState() => _ClubDirectorySubTabState();
}

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

  // Mock data - Active and Suspended only
  final List<Map<String, dynamic>> _clubs = [
    {
      'id': 'CLUB-001',
      'name': 'Software Developers Society',
      'president': 'Virnold Emmanuel',
      'members': 145,
      'status': 'Active',
    },
    {
      'id': 'CLUB-004',
      'name': 'Drama & Arts Society',
      'president': 'Mwape K.',
      'members': 52,
      'status': 'Suspended',
    }
  ];

  void _confirmSuspension(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.block_rounded, color: Colors.orange),
            const SizedBox(width: 8),
            const Text('Suspend Club', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
          ],
        ),
        content: Text(
          'Are you sure you want to suspend "$name"? They will lose posting privileges and be hidden from the active directory.',
          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(() => _clubs[index]['status'] = 'Suspended');
              Navigator.pop(ctx);
              _showSnackBar('"$name" has been suspended.', Colors.black87);
            },
            child: const Text('SUSPEND'),
          ),
        ],
      ),
    );
  }
  
  void _confirmRestore(int index, String name) {
    setState(() => _clubs[index]['status'] = 'Active');
    _showSnackBar('"$name" has been restored to Active status.', _darkGreen);
  }

  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: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Text(
                  'Campus Clubs Directory',
                  style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: Colors.black87),
                ),
                const SizedBox(height: 8),
                Text(
                  'Manage active and suspended student organizations.',
                  style: TextStyle(fontSize: 14, color: Colors.grey.shade600),
                ),
              ],
            ),
          ),
          
          Expanded(
            child: _clubs.isEmpty
              ? _buildEmptyState()
              : ListView.builder(
                  padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 16),
                  itemCount: _clubs.length,
                  itemBuilder: (context, index) {
                    final club = _clubs[index];
                    final bool isActive = club['status'] == 'Active';

                    Color statusBgColor = isActive ? _darkGreen.withOpacity(0.1) : Colors.red.withOpacity(0.1);
                    Color statusTextColor = isActive ? _darkGreen : Colors.red.shade700;
                    Color borderColor = isActive ? Colors.grey.shade300 : Colors.red.shade200;

                    return Container(
                      margin: const EdgeInsets.only(bottom: 16),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        border: Border.all(color: borderColor, width: isActive ? 1 : 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)
                                        ),
                                      ],
                                    ),
                                  ],
                                ),
                                Container(
                                  padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
                                  decoration: BoxDecoration(
                                    color: statusBgColor,
                                    borderRadius: BorderRadius.circular(2),
                                  ),
                                  child: Text(
                                    club['status'].toUpperCase(),
                                    style: TextStyle(color: statusTextColor, fontWeight: FontWeight.bold, fontSize: 11, letterSpacing: 0.5),
                                  ),
                                )
                              ],
                            ),
                            
                            const SizedBox(height: 20),
                            const Divider(height: 1),
                            const SizedBox(height: 20),
                            
                            Row(
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: [
                                if (isActive) ...[
                                  OutlinedButton.icon(
                                    onPressed: () {},
                                    style: OutlinedButton.styleFrom(
                                      foregroundColor: _darkBlue,
                                      side: BorderSide(color: Colors.grey.shade300),
                                      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
                                    ),
                                    icon: const Icon(Icons.admin_panel_settings_outlined, size: 18),
                                    label: const Text('Manage Admins', style: TextStyle(fontWeight: FontWeight.bold)),
                                  ),
                                  const SizedBox(width: 12),
                                  OutlinedButton.icon(
                                    onPressed: () => _confirmSuspension(index, club['name']),
                                    style: OutlinedButton.styleFrom(
                                      foregroundColor: Colors.orange.shade700, 
                                      side: BorderSide(color: Colors.orange.shade300),
                                      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
                                    ),
                                    icon: const Icon(Icons.block_rounded, size: 18),
                                    label: const Text('Suspend'),
                                  ),
                                ] else ...[
                                  ElevatedButton.icon(
                                    onPressed: () => _confirmRestore(index, club['name']),
                                    style: ElevatedButton.styleFrom(
                                      backgroundColor: Colors.grey.shade800,
                                      foregroundColor: Colors.white,
                                      elevation: 0,
                                      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
                                    ),
                                    icon: const Icon(Icons.restore, size: 18),
                                    label: const Text('Restore 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.groups_outlined, size: 48, color: Colors.grey.shade400),
            const SizedBox(height: 16),
            Text('No Clubs Found', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.grey.shade800)),
            const SizedBox(height: 8),
            Text('There are currently no active clubs in the directory.', style: TextStyle(color: Colors.grey.shade600)),
          ],
        ),
      ),
    );
  }
}