import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:provider/provider.dart';
import 'package:go_router/go_router.dart';
import 'dart:math' as math;

import '../../models/am_registration_data.dart';
import '../../models/card_assets.dart';
import '../../config/display_config.dart';
import '../../services/auth_service.dart';
import '../../services/reprise_session.dart';
import '../../utils/reprise_payload.dart';
import '../../widgets/hover_relief_widget.dart';
import '../../widgets/image_button.dart';
import '../../widgets/custom_navigation_button.dart';
import '../../widgets/personal_info_form_screen.dart';
import '../../widgets/professional_info_form_screen.dart';
import '../../widgets/presentation_form_screen.dart';

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

  @override
  _AmRegisterStep4ScreenState createState() => _AmRegisterStep4ScreenState();
}

class _AmRegisterStep4ScreenState extends State<AmRegisterStep4Screen> {
  bool _isSubmitting = false;

  Future<void> _submitAMRegistration(AmRegistrationData registrationData) async {
    if (_isSubmitting) return;
    if (RepriseSession.isActive) {
      if (!registrationData.isRepriseSubmitReady) {
        if (!mounted) return;
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text(
              'Vérifiez vos coordonnées et acceptez les conditions.',
              style: GoogleFonts.merienda(fontSize: 14),
            ),
            backgroundColor: Colors.red.shade700,
          ),
        );
        return;
      }
    } else if (!registrationData.isRegistrationComplete) {
      if (!mounted) return;
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text(
            'Le dossier est incomplet. Vérifiez chaque étape (photo, consentement, lieux de naissance, etc.).',
            style: GoogleFonts.merienda(fontSize: 14),
          ),
          backgroundColor: Colors.red.shade700,
        ),
      );
      return;
    }
    setState(() => _isSubmitting = true);
    try {
      if (RepriseSession.isActive) {
        await AuthService.resoumettreReprise(
          await ReprisePayload.amPatch(
            registrationData,
            RepriseSession.token!,
            existingPhotoUrl: RepriseSession.photoUrlForApi,
          ),
        );
        RepriseSession.clear();
        if (!mounted) return;
        _showRepriseConfirmationModal(context);
        return;
      }
      await AuthService.registerAM(registrationData);
      if (!mounted) return;
      _showConfirmationModal(context);
    } catch (e) {
      if (!mounted) return;
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text(e is Exception ? e.toString().replaceFirst('Exception: ', '') : 'Erreur lors de l\'inscription'),
          backgroundColor: Colors.red.shade700,
        ),
      );
    } finally {
      if (mounted) setState(() => _isSubmitting = false);
    }
  }

  @override
  Widget build(BuildContext context) {
    final registrationData = Provider.of<AmRegistrationData>(context);
    final screenSize = MediaQuery.of(context).size;
    final config = DisplayConfig.fromContext(context, mode: DisplayMode.readonly);

    return Scaffold(
      body: Stack(
        children: [
          Positioned.fill(
            child: Image.asset('assets/images/paper2.png', fit: BoxFit.cover, repeat: ImageRepeat.repeatY),
          ),
          Center(
            child: SingleChildScrollView(
              padding: const EdgeInsets.symmetric(vertical: 40.0),
              child: Padding(
                padding: EdgeInsets.symmetric(
                  horizontal: config.isMobile ? 0 : screenSize.width / 4.0
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text('Étape 4/4', style: GoogleFonts.merienda(fontSize: 16, color: Colors.black54)),
                    const SizedBox(height: 20),
                    Text('Récapitulatif de votre demande', style: GoogleFonts.merienda(fontSize: 22, fontWeight: FontWeight.bold, color: Colors.black87), textAlign: TextAlign.center),
                    const SizedBox(height: 30),
                    
                    // Carte 1: Informations personnelles
                    _buildPersonalInfo(context, registrationData),
                    const SizedBox(height: 30),
                    
                    // Carte 2: Informations professionnelles
                    _buildProfessionalInfo(context, registrationData),
                    const SizedBox(height: 30),
                    
                    // Carte 3: Présentation
                    _buildPresentation(context, registrationData),
                    const SizedBox(height: 40),
                    
                    // Boutons Mobile (Retour + Soumettre) ou Bouton Soumettre Desktop
                    if (config.isMobile)
                      Padding(
                        padding: EdgeInsets.symmetric(horizontal: screenSize.width * 0.05),
                        child: Row(
                          children: [
                            Expanded(
                              child: HoverReliefWidget(
                                child: CustomNavigationButton(
                                  text: 'Précédent',
                                  style: NavigationButtonStyle.purple,
                                  onPressed: () {
                                    if (context.canPop()) {
                                      context.pop();
                                    } else {
                                      context.go('/am-register-step3');
                                    }
                                  },
                                  width: double.infinity,
                                  height: 50,
                                  fontSize: 16,
                                ),
                              ),
                            ),
                            const SizedBox(width: 16),
                            Expanded(
                              child: HoverReliefWidget(
                                child: CustomNavigationButton(
                                  text: _isSubmitting ? 'Envoi...' : 'Soumettre',
                                  style: NavigationButtonStyle.green,
                                  onPressed: () => _submitAMRegistration(registrationData),
                                  width: double.infinity,
                                  height: 50,
                                  fontSize: 16,
                                ),
                              ),
                            ),
                          ],
                        ),
                      )
                    else
                        ImageButton(
                        bg: 'assets/images/bg_green.png',
                        text: _isSubmitting ? 'Envoi...' : 'Soumettre ma demande',
                        textColor: const Color(0xFF2D6A4F),
                        width: 350,
                        height: 50,
                        fontSize: 18,
                        onPressed: () => _submitAMRegistration(registrationData),
                      ),
                  ],
                ),
              ),
            ),
          ),
          // Chevrons desktop uniquement
          if (!config.isMobile)
            Positioned(
              top: screenSize.height / 2 - 20,
              left: 40,
              child: IconButton(
                icon: Transform(
                  alignment: Alignment.center,
                  transform: Matrix4.rotationY(math.pi),
                  child: Image.asset('assets/images/chevron_right.png', height: 40),
                ),
                onPressed: () {
                  if (context.canPop()) {
                    context.pop();
                  } else {
                    context.go('/am-register-step3');
                  }
                },
                tooltip: 'Retour',
              ),
            ),
        ],
      ),
    );
  }

  Widget _buildPersonalInfo(BuildContext context, AmRegistrationData data) {
    return PersonalInfoFormScreen(
      mode: DisplayMode.readonly,
      embedContentOnly: true,
      stepText: '',
      title: 'Informations personnelles',
      cardColor: CardColorHorizontal.blue,
      initialData: PersonalInfoData(
        firstName: data.firstName,
        lastName: data.lastName,
        phone: data.phone,
        email: data.email,
        address: data.streetAddress,
        postalCode: data.postalCode,
        city: data.city,
      ),
      onSubmit: (d, {hasSecondPerson, sameAddress}) {}, // No-op en readonly
      previousRoute: '',
      onEdit: () => context.go('/am-register-step1'),
    );
  }

  Widget _buildProfessionalInfo(BuildContext context, AmRegistrationData data) {
    return ProfessionalInfoFormScreen(
      mode: DisplayMode.readonly,
      embedContentOnly: true,
      stepText: '',
      title: 'Informations professionnelles',
      cardColor: CardColorHorizontal.green,
      initialData: ProfessionalInfoData(
        photoPath: data.photoPath,
        photoBytes: data.photoBytes,
        photoFilename: data.photoFilename,
        dateOfBirth: data.dateOfBirth,
        birthCity: data.birthCity,
        birthCountry: data.birthCountry,
        nir: data.nir,
        agrementNumber: data.agrementNumber,
        agreementDate: data.agreementDate,
        capacity: data.capacity,
        placesAvailable: data.placesAvailable,
        photoConsent: data.photoConsent,
      ),
      onSubmit: (d) {},
      previousRoute: '',
      onEdit: () => context.go('/am-register-step2'),
    );
  }

  Widget _buildPresentation(BuildContext context, AmRegistrationData data) {
    return PresentationFormScreen(
      mode: DisplayMode.readonly,
      embedContentOnly: true,
      stepText: '',
      title: 'Présentation & CGU',
      cardColor: CardColorHorizontal.peach,
      textFieldHint: '',
      initialText: data.presentationText,
      initialCguAccepted: data.cguAccepted,
      previousRoute: '',
      onSubmit: (t, c) {},
      onEdit: () => context.go('/am-register-step3'),
    );
  }

  void _showRepriseConfirmationModal(BuildContext context) {
    showDialog<void>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext dialogContext) {
        return AlertDialog(
          title: Text(
            'Dossier resoumis',
            style: GoogleFonts.merienda(fontWeight: FontWeight.bold),
          ),
          content: Text(
            'Vos modifications ont été enregistrées. Votre dossier est de nouveau en attente de validation.',
            style: GoogleFonts.merienda(fontSize: 14),
          ),
          actions: <Widget>[
            TextButton(
              child: Text('OK', style: GoogleFonts.merienda(fontWeight: FontWeight.bold)),
              onPressed: () {
                Navigator.of(dialogContext).pop();
                context.go('/login');
              },
            ),
          ],
        );
      },
    );
  }

  void _showConfirmationModal(BuildContext context) {
    showDialog<void>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext dialogContext) {
        return AlertDialog(
          title: Text(
            'Demande enregistrée',
            style: GoogleFonts.merienda(fontWeight: FontWeight.bold),
          ),
          content: Text(
            'Votre dossier a bien été pris en compte. Un gestionnaire le validera bientôt.',
            style: GoogleFonts.merienda(fontSize: 14),
          ),
          actions: <Widget>[
            TextButton(
              child: Text('OK', style: GoogleFonts.merienda(fontWeight: FontWeight.bold)),
              onPressed: () {
                Navigator.of(dialogContext).pop();
                context.go('/login');
              },
            ),
          ],
        );
      },
    );
  }
}
