// lib/features/admin/tabs/admin_about_us_tab.dart
// ignore_for_file: unused_import

import 'package:flutter/material.dart';

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

  @override
  State<AdminAboutUsTab> createState() => _AdminAboutUsTabState();
}

class _AdminAboutUsTabState extends State<AdminAboutUsTab> {
  // Standard Color Palette
  static const Color _standardGold = Color.fromARGB(255, 3, 92, 43);
  static const Color _darkGreen = Color.fromARGB(255, 4, 63, 31); 

  final _formKey = GlobalKey<FormState>();
  
  late TextEditingController _historyController;
  late TextEditingController _missionController;
  late TextEditingController _visionController;

  bool _isLoading = false;

  @override
  void initState() {
    super.initState();
    // Pre-filling with active data for editing
    _historyController = TextEditingController(
      text: 'ROCKVIEW UNIVERSITY - NDOLA CAMPUS is a premier institution dedicated to excellence in education, innovation, and community development. Established to bridge the gap between academic theory and practical industry requirements...'
    );
    _missionController = TextEditingController(
      text: 'To provide accessible, high-quality education that empowers students to become innovative leaders and professionals.'
    );
    _visionController = TextEditingController(
      text: 'To be the leading center of academic excellence and technological advancement in the region.'
    );
  }

  @override
  void dispose() {
    _historyController.dispose();
    _missionController.dispose();
    _visionController.dispose();
    super.dispose();
  }

  void _saveChanges() async {
    if (_formKey.currentState!.validate()) {
      setState(() => _isLoading = true);

      // Simulate API call to backend
      await Future.delayed(const Duration(seconds: 2));

      if (mounted) {
        setState(() => _isLoading = false);
        
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: const Text('Institution details updated successfully.', style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold)),
            backgroundColor: _darkGreen,
            behavior: SnackBarBehavior.floating,
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
          ),
        );
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    final screenWidth = MediaQuery.of(context).size.width;
    final isDesktop = screenWidth > 900;

    return SingleChildScrollView(
      padding: const EdgeInsets.all(32.0),
      child: Form(
        key: _formKey,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(
              'Manage Institution Details', 
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: Colors.black87)
            ),
            const SizedBox(height: 8),
            Text(
              'Update the university history, mission, and vision statements shown in the student-facing application.', 
              style: TextStyle(fontSize: 14, color: Colors.grey.shade600)
            ),
            const SizedBox(height: 32),
            
            // --- MAIN EDITING CONTAINER ---
            Container(
              padding: const EdgeInsets.all(24),
              decoration: BoxDecoration(
                color: Colors.white,
                border: Border.all(color: Colors.grey.shade300, width: 1), // Flat UI border
                borderRadius: BorderRadius.circular(4), 
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  _buildEditorSection(
                    title: 'University History / Overview',
                    controller: _historyController,
                    hint: 'Enter the general description of the university here...',
                    maxLines: 8,
                  ),
                  const SizedBox(height: 24),
                  
                  // Responsive Layout for Mission & Vision
                  if (isDesktop)
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Expanded(
                          child: _buildEditorSection(
                            title: 'Our Mission',
                            controller: _missionController,
                            hint: 'Enter mission statement...',
                            maxLines: 5,
                          )
                        ),
                        const SizedBox(width: 24),
                        Expanded(
                          child: _buildEditorSection(
                            title: 'Our Vision',
                            controller: _visionController,
                            hint: 'Enter vision statement...',
                            maxLines: 5,
                          )
                        ),
                      ],
                    )
                  else
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        _buildEditorSection(
                          title: 'Our Mission',
                          controller: _missionController,
                          hint: 'Enter mission statement...',
                          maxLines: 5,
                        ),
                        const SizedBox(height: 24),
                        _buildEditorSection(
                          title: 'Our Vision',
                          controller: _visionController,
                          hint: 'Enter vision statement...',
                          maxLines: 5,
                        ),
                      ],
                    ),
                  
                  const SizedBox(height: 32),
                  const Divider(height: 1, thickness: 1),
                  const SizedBox(height: 24),
                  
                  // --- SAVE BUTTON ---
                  Align(
                    alignment: Alignment.centerRight,
                    child: SizedBox(
                      height: 48,
                      child: ElevatedButton.icon(
                        onPressed: _isLoading ? null : _saveChanges,
                        style: ElevatedButton.styleFrom(
                          backgroundColor: _darkGreen,
                          foregroundColor: Colors.white,
                          elevation: 0, // Flat UI
                          padding: const EdgeInsets.symmetric(horizontal: 32),
                          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
                        ),
                        icon: _isLoading 
                          ? const SizedBox(width: 18, height: 18, child: CircularProgressIndicator(color: Colors.white, strokeWidth: 2))
                          : const Icon(Icons.save_outlined, size: 20),
                        label: Text(
                          _isLoading ? 'SAVING...' : 'SAVE CHANGES', 
                          style: const TextStyle(fontSize: 14, fontWeight: FontWeight.bold, letterSpacing: 1.0)
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  // =====================================================================
  // FLAT CUSTOM EDITOR WIDGET
  // =====================================================================
  Widget _buildEditorSection({
    required String title, 
    required TextEditingController controller,
    required String hint, 
    required int maxLines
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(title, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16, color: Colors.black87)),
        const SizedBox(height: 12),
        TextFormField(
          controller: controller,
          maxLines: maxLines,
          cursorColor: _darkGreen,
          validator: (value) => value!.trim().isEmpty ? 'This field cannot be empty' : null,
          decoration: InputDecoration(
            hintText: hint,
            hintStyle: TextStyle(color: Colors.grey.shade400),
            filled: true,
            fillColor: Colors.grey.shade50,
            contentPadding: const EdgeInsets.all(16),
            border: OutlineInputBorder(borderRadius: BorderRadius.circular(4), borderSide: BorderSide(color: Colors.grey.shade300)),
            enabledBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(4), borderSide: BorderSide(color: Colors.grey.shade300)),
            focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(4), borderSide: const BorderSide(color: _darkGreen, width: 2)),
            errorBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(4), borderSide: const BorderSide(color: Colors.redAccent)),
          ),
        ),
      ],
    );
  }
}