// lib/features/admin/tabs/housing/verified_housing_sub_tab.dart

import 'package:flutter/material.dart';

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

  @override
  State<VerifiedHousingSubTab> createState() => _VerifiedHousingSubTabState();
}

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

  // Mock data representing backend verified housing directory
  final List<Map<String, dynamic>> _housing = [
    {
      'id': 'HSG-002',
      'name': 'Campus View Apartments',
      'landlord': 'Mrs. Chanda',
      'distance': '0.5 km from campus',
      'verified': true,
    },
    {
      'id': 'HSG-004',
      'name': 'Sunshine Student Villas',
      'landlord': 'Mr. Mwila',
      'distance': '0.8 km from campus',
      'verified': true,
    }
  ];

  void _confirmRevocation(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.remove_circle_outline, color: Colors.redAccent),
            const SizedBox(width: 8),
            const Text('Revoke Verification', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
          ],
        ),
        content: Text(
          'Are you sure you want to revoke the verified status for "$name"? It will be removed from the active directory and returned to pending.',
          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(() => _housing.removeAt(index));
              Navigator.pop(ctx);
              _showSnackBar('Verification revoked for "$name".', 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)),
      ),
    );
  }

  @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(
                      'Verified Properties',
                      style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: Colors.black87),
                    ),
                    const SizedBox(height: 8),
                    Text(
                      'Manage off-campus accommodation officially verified for students.',
                      style: TextStyle(fontSize: 14, color: Colors.grey.shade600),
                    ),
                  ],
                ),
                ElevatedButton.icon(
                  onPressed: () {}, // Add property logic here
                  style: ElevatedButton.styleFrom(
                    backgroundColor: _darkBlue,
                    foregroundColor: Colors.white,
                    elevation: 0,
                    padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
                    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
                  ),
                  icon: const Icon(Icons.add_business_rounded, size: 20),
                  label: const Text('Add Property', style: TextStyle(fontWeight: FontWeight.bold)),
                ),
              ],
            ),
          ),
          
          Expanded(
            child: _housing.isEmpty
              ? _buildEmptyState()
              : ListView.builder(
                  padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 16),
                  itemCount: _housing.length,
                  itemBuilder: (context, index) {
                    final house = _housing[index];

                    return Container(
                      margin: const EdgeInsets.only(bottom: 16),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        border: Border.all(color: _darkGreen.withOpacity(0.3), width: 1),
                        borderRadius: BorderRadius.circular(4), 
                      ),
                      child: IntrinsicHeight(
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.stretch,
                          children: [
                            // Left Status Indicator Strip
                            Container(
                              width: 4,
                              decoration: const BoxDecoration(
                                color: _darkGreen,
                                borderRadius: BorderRadius.horizontal(left: Radius.circular(3)),
                              ),
                            ),
                            
                            Expanded(
                              child: Padding(
                                padding: const EdgeInsets.all(20),
                                child: Row(
                                  children: [
                                    // Icon Box
                                    Container(
                                      width: 64, height: 64,
                                      decoration: BoxDecoration(
                                        color: _darkGreen.withOpacity(0.05),
                                        borderRadius: BorderRadius.circular(4),
                                        border: Border.all(color: _darkGreen.withOpacity(0.2)),
                                      ),
                                      child: const Icon(Icons.apartment_rounded, color: _darkGreen, size: 28),
                                    ),
                                    const SizedBox(width: 24),
                                    
                                    // Property Details
                                    Expanded(
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        mainAxisAlignment: MainAxisAlignment.center,
                                        children: [
                                          Row(
                                            children: [
                                              Text(house['name'], style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 17, color: Colors.black87)),
                                              const SizedBox(width: 8),
                                              const Icon(Icons.verified_rounded, color: _darkBlue, size: 18),
                                            ],
                                          ),
                                          const SizedBox(height: 6),
                                          Text('Landlord: ${house['landlord']}', style: TextStyle(color: Colors.grey.shade800, fontWeight: FontWeight.w500, fontSize: 13)),
                                          const SizedBox(height: 4),
                                          Row(
                                            children: [
                                              Icon(Icons.location_on_outlined, size: 14, color: Colors.grey.shade500),
                                              const SizedBox(width: 4),
                                              Text(house['distance'], style: TextStyle(color: Colors.grey.shade600, fontSize: 13)),
                                            ],
                                          ),
                                        ],
                                      ),
                                    ),
                                    
                                    // Actions
                                    OutlinedButton.icon(
                                      onPressed: () => _confirmRevocation(index, house['name']),
                                      style: OutlinedButton.styleFrom(
                                        foregroundColor: Colors.redAccent,
                                        side: BorderSide(color: Colors.redAccent.shade100),
                                        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
                                        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
                                      ),
                                      icon: const Icon(Icons.remove_circle_outline, size: 18),
                                      label: const Text('Revoke Verification', 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.domain_disabled_rounded, size: 48, color: Colors.grey.shade400),
            const SizedBox(height: 16),
            Text('No Verified Properties', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.grey.shade800)),
            const SizedBox(height: 8),
            Text('There are currently no verified housing options in the directory.', style: TextStyle(color: Colors.grey.shade600)),
          ],
        ),
      ),
    );
  }
}