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

import 'package:flutter/material.dart';

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

  @override
  State<PendingHousingSubTab> createState() => _PendingHousingSubTabState();
}

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

  // Mock data representing unverified housing
  final List<Map<String, dynamic>> _housing = [
    {
      'id': 'HSG-001',
      'name': 'Riverside Student Hostel',
      'landlord': 'Mr. Phiri',
      'distance': '1.2 km from campus',
      'verified': false,
    },
    {
      'id': 'HSG-003',
      'name': 'Ndola Heights Boarding',
      'landlord': 'Ms. Banda',
      'distance': '2.0 km from campus',
      'verified': false,
    }
  ];

  void _confirmVerification(int index, String name) {
    showDialog(
      context: context,
      builder: (ctx) => AlertDialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)), 
        backgroundColor: Colors.white,
        title: Row(
          children: [
            Icon(Icons.verified_rounded, color: _darkGreen),
            const SizedBox(width: 8),
            const Text('Verify Property', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18)),
          ],
        ),
        content: Text(
          'Are you sure you want to verify "$name"? This will mark the property as inspected and move it to 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: _darkGreen,
              foregroundColor: Colors.white,
              elevation: 0,
              shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
            ),
            onPressed: () {
              setState(() => _housing.removeAt(index));
              Navigator.pop(ctx);
              _showSnackBar('"$name" is now officially verified.', _darkGreen);
            },
            child: const Text('VERIFY PROPERTY'),
          ),
        ],
      ),
    );
  }

  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 Verification',
                      style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: Colors.black87),
                    ),
                    const SizedBox(height: 8),
                    Text(
                      'Review submitted housing properties requiring inspection.',
                      style: TextStyle(fontSize: 14, color: Colors.grey.shade600),
                    ),
                  ],
                ),
              ],
            ),
          ),
          
          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: Colors.orange.shade300, width: 1.5),
                        borderRadius: BorderRadius.circular(4), 
                      ),
                      child: IntrinsicHeight(
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.stretch,
                          children: [
                            Container(
                              width: 4,
                              decoration: BoxDecoration(
                                color: Colors.orange.shade400,
                                borderRadius: const 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: Colors.grey.shade100,
                                        borderRadius: BorderRadius.circular(4),
                                        border: Border.all(color: Colors.transparent),
                                      ),
                                      child: Icon(Icons.apartment_rounded, color: Colors.grey.shade500, size: 28),
                                    ),
                                    const SizedBox(width: 24),
                                    
                                    // Property Details
                                    Expanded(
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        mainAxisAlignment: MainAxisAlignment.center,
                                        children: [
                                          Text(house['name'], style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 17, color: Colors.black87)),
                                          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
                                    ElevatedButton.icon(
                                      onPressed: () => _confirmVerification(index, house['name']),
                                      style: ElevatedButton.styleFrom(
                                        backgroundColor: _darkBlue,
                                        foregroundColor: Colors.white,
                                        elevation: 0,
                                        padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
                                        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
                                      ),
                                      icon: const Icon(Icons.check_circle_outline, size: 18),
                                      label: const Text('Verify Property', 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 housing properties pending verification.', style: TextStyle(color: Colors.grey.shade600)),
          ],
        ),
      ),
    );
  }
}