import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart';
import 'dart:io';

import '../../models/am_registration_data.dart';
import '../../models/card_assets.dart';
import '../../widgets/professional_info_form_screen.dart';

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

  @override
  State<AmRegisterStep2Screen> createState() => _AmRegisterStep2ScreenState();
}

class _AmRegisterStep2ScreenState extends State<AmRegisterStep2Screen> {
  String? _photoPathFramework;
  File? _photoFile;

  Future<void> _pickPhoto() async {
    // TODO: Remplacer par la vraie logique ImagePicker
    // final imagePicker = ImagePicker();
    // final pickedFile = await imagePicker.pickImage(source: ImageSource.gallery);
    // if (pickedFile != null) {
    //   setState(() {
    //     _photoFile = File(pickedFile.path);
    //     _photoPathFramework = pickedFile.path;
    //   });
    // } else {
      setState(() {
        _photoPathFramework = 'assets/images/icon_assmat.png';
        _photoFile = null;
      });
    // }
    print("Photo sélectionnée: $_photoPathFramework");
  }

  @override
  Widget build(BuildContext context) {
    final registrationData = Provider.of<AmRegistrationData>(context, listen: false);
    
    // Préparer les données initiales
    ProfessionalInfoData initialData = ProfessionalInfoData(
      photoPath: registrationData.photoPath,
      photoConsent: registrationData.photoConsent,
      dateOfBirth: registrationData.dateOfBirth,
      birthCity: registrationData.birthCity,
      birthCountry: registrationData.birthCountry,
      nir: registrationData.nir,
      agrementNumber: registrationData.agrementNumber,
      capacity: registrationData.capacity,
    );

    // Données de test : Marie DUBOIS (jeu de test 03_seed_test_data.sql / docs/test-data)
    if (registrationData.dateOfBirth == null && registrationData.nir.isEmpty) {
      initialData = ProfessionalInfoData(
        photoPath: 'assets/images/icon_assmat.png',
        photoConsent: true,
        dateOfBirth: DateTime(1980, 6, 8),
        birthCity: 'Bezons',
        birthCountry: 'France',
        nir: '280062A00100191',
        agrementNumber: 'AGR-2019-095001',
        capacity: 4,
      );
    }

    return ProfessionalInfoFormScreen(
      stepText: 'Étape 2/4',
      title: 'Vos informations professionnelles',
      cardColor: CardColorHorizontal.green,
      initialData: initialData,
      previousRoute: '/am-register-step1',
      onPickPhoto: _pickPhoto,
      onSubmit: (data) {
        registrationData.updateProfessionalInfo(
          photoPath: _photoPathFramework ?? data.photoPath,
          photoConsent: data.photoConsent,
          dateOfBirth: data.dateOfBirth,
          birthCity: data.birthCity,
          birthCountry: data.birthCountry,
          nir: data.nir,
          agrementNumber: data.agrementNumber,
          capacity: data.capacity,
        );
        context.go('/am-register-step3');
      },
    );
  }
}