import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:p_tits_pas/services/configuration_service.dart';
import 'package:p_tits_pas/utils/email_utils.dart';
import 'package:p_tits_pas/widgets/admin/relais_management_panel.dart';

/// Panneau Paramètres admin : Email (SMTP), Personnalisation, Avancé.
class ParametresPanel extends StatefulWidget {
  /// Si true, après sauvegarde on redirige vers le login (première config). Sinon on reste sur la page.
  final bool redirectToLoginAfterSave;
  final int selectedSettingsTabIndex;

  const ParametresPanel({
    super.key,
    this.redirectToLoginAfterSave = false,
    this.selectedSettingsTabIndex = 0,
  });

  @override
  State<ParametresPanel> createState() => _ParametresPanelState();
}

class _ParametresPanelState extends State<ParametresPanel> {
  final _formKey = GlobalKey<FormState>();
  bool _isLoading = true;
  String? _loadError;
  bool _isSaving = false;
  String? _message;

  final Map<String, TextEditingController> _controllers = {};
  bool _smtpSecure = false;
  bool _smtpAuthRequired = false;

  @override
  void initState() {
    super.initState();
    _createControllers();
    _loadConfiguration();
  }

  void _createControllers() {
    final keys = [
      'smtp_host',
      'smtp_port',
      'smtp_user',
      'smtp_password',
      'email_from_name',
      'email_from_address',
      'app_name',
      'app_url',
      'app_logo_url',
      'password_reset_token_expiry_days',
      'jwt_expiry_hours',
      'max_upload_size_mb',
    ];
    for (final k in keys) {
      _controllers[k] = TextEditingController();
    }
  }

  Future<void> _loadConfiguration() async {
    setState(() {
      _isLoading = true;
      _loadError = null;
    });
    try {
      final list = await ConfigurationService.getAll();
      if (!mounted) return;
      for (final item in list) {
        final c = _controllers[item.cle];
        if (c != null && item.valeur != null && item.valeur != '***********') {
          c.text = item.valeur!;
        }
        if (item.cle == 'smtp_secure') {
          _smtpSecure = item.valeur == 'true';
        }
        if (item.cle == 'smtp_auth_required') {
          _smtpAuthRequired = item.valeur == 'true';
        }
      }
      setState(() {
        _isLoading = false;
      });
    } catch (e) {
      if (mounted) {
        setState(() {
          _isLoading = false;
          _loadError = e.toString().replaceAll('Exception: ', '');
        });
      }
    }
  }

  @override
  void dispose() {
    for (final c in _controllers.values) {
      c.dispose();
    }
    super.dispose();
  }

  Map<String, dynamic> _buildPayload() {
    final payload = <String, dynamic>{};
    payload['smtp_host'] = _controllers['smtp_host']!.text.trim();
    final port = int.tryParse(_controllers['smtp_port']!.text.trim());
    if (port != null) payload['smtp_port'] = port;
    payload['smtp_secure'] = _smtpSecure;
    payload['smtp_auth_required'] = _smtpAuthRequired;
    payload['smtp_user'] = _controllers['smtp_user']!.text.trim();
    final pwd = _controllers['smtp_password']!.text.trim();
    if (pwd.isNotEmpty && pwd != '***********') {
      payload['smtp_password'] = pwd;
    }
    payload['email_from_name'] = _controllers['email_from_name']!.text.trim();
    payload['email_from_address'] =
        _controllers['email_from_address']!.text.trim();
    payload['app_name'] = _controllers['app_name']!.text.trim();
    payload['app_url'] = _controllers['app_url']!.text.trim();
    payload['app_logo_url'] = _controllers['app_logo_url']!.text.trim();
    final tokenDays = int.tryParse(
        _controllers['password_reset_token_expiry_days']!.text.trim());
    if (tokenDays != null) {
      payload['password_reset_token_expiry_days'] = tokenDays;
    }
    final jwtHours =
        int.tryParse(_controllers['jwt_expiry_hours']!.text.trim());
    if (jwtHours != null) {
      payload['jwt_expiry_hours'] = jwtHours;
    }
    final maxMb = int.tryParse(_controllers['max_upload_size_mb']!.text.trim());
    if (maxMb != null) {
      payload['max_upload_size_mb'] = maxMb;
    }
    return payload;
  }

  /// Sauvegarde en base sans completeSetup (utilisé avant test SMTP).
  Future<void> _saveBulkOnly() async {
    await ConfigurationService.updateBulk(_buildPayload());
  }

  /// Sauvegarde la config, marque le setup comme terminé. Si première config, redirige vers le login.
  Future<void> _save() async {
    final redirectAfter = widget.redirectToLoginAfterSave;
    setState(() {
      _message = null;
      _isSaving = true;
    });
    try {
      await ConfigurationService.updateBulk(_buildPayload());
      if (!mounted) return;
      await ConfigurationService.completeSetup();
      if (!mounted) return;
      setState(() {
        _isSaving = false;
        _message = 'Configuration enregistrée.';
      });
      if (!mounted) return;
      if (redirectAfter) {
        GoRouter.of(context).go('/login');
      }
    } catch (e) {
      if (mounted) {
        setState(() {
          _isSaving = false;
          _message = e.toString().replaceAll('Exception: ', '');
        });
      }
    }
  }

  Future<void> _testSmtp() async {
    final email = await showDialog<String>(
      context: context,
      builder: (ctx) {
        final c = TextEditingController();
        return AlertDialog(
          title: const Text('Tester la connexion SMTP'),
          content: TextField(
            controller: c,
            decoration: const InputDecoration(
              labelText: 'Email pour recevoir le test',
              hintText: 'admin@example.com',
            ),
            keyboardType: TextInputType.emailAddress,
            autocorrect: false,
            enableSuggestions: false,
            inputFormatters: const [EmailMaxLengthFormatter()],
          ),
          actions: [
            TextButton(
              onPressed: () => Navigator.pop(ctx),
              child: const Text('Annuler'),
            ),
            FilledButton(
              onPressed: () {
                final t = c.text.trim();
                if (t.isEmpty) {
                  return;
                }
                final err = validateEmail(t, allowEmpty: true);
                if (err != null) {
                  ScaffoldMessenger.of(ctx).showSnackBar(
                    SnackBar(content: Text(err)),
                  );
                  return;
                }
                Navigator.pop(ctx, t);
              },
              child: const Text('Envoyer'),
            ),
          ],
        );
      },
    );
    if (email == null || !mounted) return;
    setState(() => _message = null);
    try {
      await _saveBulkOnly();
      if (!mounted) return;
      final msg = await ConfigurationService.testSmtp(email);
      if (!mounted) return;
      setState(() => _message = msg);
    } catch (e) {
      if (mounted) {
        setState(() => _message = e.toString().replaceAll('Exception: ', ''));
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    if (widget.selectedSettingsTabIndex == 1) {
      return const RelaisManagementPanel();
    }

    if (_isLoading) {
      return const Center(child: CircularProgressIndicator());
    }
    if (_loadError != null) {
      return Center(
        child: Padding(
          padding: const EdgeInsets.all(24),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(_loadError!, style: TextStyle(color: Colors.red.shade700)),
              const SizedBox(height: 16),
              FilledButton(
                onPressed: _loadConfiguration,
                child: const Text('Réessayer'),
              ),
            ],
          ),
        ),
      );
    }

    final isSuccess = _message != null &&
        (_message!.startsWith('Configuration') ||
            _message!.startsWith('Connexion'));

    return Form(
      key: _formKey,
      child: SingleChildScrollView(
        padding: const EdgeInsets.all(24),
        child: Center(
          child: ConstrainedBox(
            constraints: const BoxConstraints(maxWidth: 720),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                if (_message != null) ...[
                  _MessageBanner(message: _message!, isSuccess: isSuccess),
                  const SizedBox(height: 20),
                ],
                _buildSectionCard(
                  context,
                  icon: Icons.email_outlined,
                  title: 'Configuration Email (SMTP)',
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      _buildField(
                        'smtp_host',
                        'Serveur SMTP',
                        hint: 'mail.example.com',
                      ),
                      const SizedBox(height: 14),
                      _buildField(
                        'smtp_port',
                        'Port SMTP',
                        keyboard: TextInputType.number,
                        hint: '25, 465, 587',
                      ),
                      const SizedBox(height: 14),
                      Padding(
                        padding: const EdgeInsets.only(bottom: 14),
                        child: Row(
                          children: [
                            Checkbox(
                              value: _smtpSecure,
                              onChanged: (v) =>
                                  setState(() => _smtpSecure = v ?? false),
                              activeColor: const Color(0xFF9CC5C0),
                            ),
                            const Text('SSL/TLS (secure)'),
                            const SizedBox(width: 24),
                            Checkbox(
                              value: _smtpAuthRequired,
                              onChanged: (v) => setState(
                                () => _smtpAuthRequired = v ?? false,
                              ),
                              activeColor: const Color(0xFF9CC5C0),
                            ),
                            const Text('Authentification requise'),
                          ],
                        ),
                      ),
                      _buildField('smtp_user', 'Utilisateur SMTP'),
                      const SizedBox(height: 14),
                      _buildField(
                        'smtp_password',
                        'Mot de passe SMTP',
                        obscure: true,
                      ),
                      const SizedBox(height: 14),
                      _buildField('email_from_name', 'Nom expéditeur'),
                      const SizedBox(height: 14),
                      _buildField(
                        'email_from_address',
                        'Email expéditeur',
                        hint: 'no-reply@example.com',
                      ),
                      const SizedBox(height: 18),
                      Align(
                        alignment: Alignment.centerRight,
                        child: OutlinedButton.icon(
                          onPressed: _isSaving ? null : _testSmtp,
                          icon: const Icon(Icons.send_outlined, size: 18),
                          label: const Text('Tester la connexion SMTP'),
                          style: OutlinedButton.styleFrom(
                            foregroundColor: const Color(0xFF2D6A4F),
                            side: const BorderSide(
                              color: Color(0xFF9CC5C0),
                            ),
                            padding: const EdgeInsets.symmetric(
                              horizontal: 20,
                              vertical: 12,
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                const SizedBox(height: 24),
                _buildSectionCard(
                  context,
                  icon: Icons.palette_outlined,
                  title: 'Personnalisation',
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      _buildField('app_name', 'Nom de l\'application'),
                      const SizedBox(height: 14),
                      _buildField(
                        'app_url',
                        'URL de l\'application',
                        hint: 'https://app.example.com',
                      ),
                      const SizedBox(height: 14),
                      _buildField(
                        'app_logo_url',
                        'URL du logo',
                        hint: '/assets/logo.png',
                      ),
                    ],
                  ),
                ),
                const SizedBox(height: 24),
                _buildSectionCard(
                  context,
                  icon: Icons.settings_outlined,
                  title: 'Paramètres avancés',
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      _buildField(
                        'password_reset_token_expiry_days',
                        'Validité token MDP (jours)',
                        keyboard: TextInputType.number,
                      ),
                      const SizedBox(height: 14),
                      _buildField(
                        'jwt_expiry_hours',
                        'Validité session JWT (heures)',
                        keyboard: TextInputType.number,
                      ),
                      const SizedBox(height: 14),
                      _buildField(
                        'max_upload_size_mb',
                        'Taille max upload (MB)',
                        keyboard: TextInputType.number,
                      ),
                    ],
                  ),
                ),
                const SizedBox(height: 28),
                SizedBox(
                  height: 48,
                  child: FilledButton(
                    onPressed: _isSaving ? null : _save,
                    style: FilledButton.styleFrom(
                      backgroundColor: const Color(0xFF9CC5C0),
                      foregroundColor: Colors.white,
                    ),
                    child: _isSaving
                        ? const SizedBox(
                            width: 24,
                            height: 24,
                            child: CircularProgressIndicator(
                              strokeWidth: 2,
                              color: Colors.white,
                            ),
                          )
                        : const Text('Sauvegarder la configuration'),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildSectionCard(BuildContext context,
      {required IconData icon, required String title, required Widget child}) {
    return Card(
      elevation: 2,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      child: Padding(
        padding: const EdgeInsets.all(24),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Icon(icon, size: 22, color: const Color(0xFF9CC5C0)),
                const SizedBox(width: 10),
                Text(
                  title,
                  style: Theme.of(context).textTheme.titleMedium?.copyWith(
                        fontWeight: FontWeight.bold,
                        color: const Color(0xFF2D6A4F),
                      ),
                ),
              ],
            ),
            const SizedBox(height: 20),
            child,
          ],
        ),
      ),
    );
  }

  Widget _buildField(String key, String label,
      {bool obscure = false, TextInputType? keyboard, String? hint}) {
    final c = _controllers[key];
    if (c == null) return const SizedBox.shrink();
    return TextFormField(
      controller: c,
      obscureText: obscure,
      keyboardType: keyboard,
      enabled: !_isSaving,
      decoration: InputDecoration(
        labelText: label,
        hintText: hint,
        border: const OutlineInputBorder(),
        contentPadding:
            const EdgeInsets.symmetric(horizontal: 12, vertical: 12),
      ),
    );
  }
}

class _MessageBanner extends StatelessWidget {
  final String message;
  final bool isSuccess;

  const _MessageBanner({required this.message, required this.isSuccess});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
      decoration: BoxDecoration(
        color: isSuccess ? Colors.green.shade50 : Colors.red.shade50,
        borderRadius: BorderRadius.circular(10),
        border: Border.all(
          color: isSuccess ? Colors.green.shade200 : Colors.red.shade200,
        ),
      ),
      child: Row(
        children: [
          Icon(
            isSuccess ? Icons.check_circle_outline : Icons.error_outline,
            size: 22,
            color: isSuccess ? Colors.green.shade700 : Colors.red.shade700,
          ),
          const SizedBox(width: 12),
          Expanded(
            child: Text(
              message,
              style: TextStyle(
                color: isSuccess ? Colors.green.shade900 : Colors.red.shade900,
                fontSize: 14,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
