import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/widgets/flux_text_field.dart';
import 'package:flux/core/widgets/shared_forms/attachments_section.dart';
import 'package:flux/features/attachments/blocs/attachments_bloc.dart';
import 'package:flux/features/customers/blocs/customer_form_cubit.dart';
import 'package:flux/features/customers/models/customer_model.dart'; // Uso il tuo widget!

class CustomerFormScreen extends StatefulWidget {
  final CustomerModel? customer;
  final String? customerId;

  const CustomerFormScreen({super.key, this.customer, this.customerId});

  @override
  State<CustomerFormScreen> createState() => _CustomerFormScreenState();
}

class _CustomerFormScreenState extends State<CustomerFormScreen> {
  final _formKey = GlobalKey<FormState>();

  // Controller inizializzati con i dati del cliente (se presenti)
  final TextEditingController _nomeController = TextEditingController();
  final TextEditingController _telefonoController = TextEditingController();
  final TextEditingController _emailController = TextEditingController();
  final TextEditingController _noteController = TextEditingController();
  bool _isInitialized = false;

  @override
  void initState() {
    super.initState();

    // 1. Lanciamo l'inizializzazione (che se è sincrona, finirà istantaneamente)
    context.read<CustomerFormCubit>().initForm(
      customerId: widget.customerId,
      existingCustomer: widget.customer,
    );

    // 2. Leggiamo lo stato SUBITO DOPO. Se è già ready, non aspettiamo il listener!
    final currentState = context.read<CustomerFormCubit>().state;
    if (currentState.status == CustomerFormStatus.ready && !_isInitialized) {
      _syncTextControllers(currentState.customer);
    }
  }

  @override
  void dispose() {
    _nomeController.dispose();
    _telefonoController.dispose();
    _emailController.dispose();
    _noteController.dispose();
    super.dispose();
  }

  void _syncTextControllers(CustomerModel customer) {
    if (_nomeController.text.isEmpty) {
      _nomeController.text = customer.name;
    }
    if (_telefonoController.text.isEmpty) {
      _telefonoController.text = customer.phoneNumber;
    }
    if (_emailController.text.isEmpty) {
      _emailController.text = customer.email;
    }
    if (_noteController.text.isEmpty) {
      _noteController.text = customer.note;
    }
    _isInitialized = true;
  }

  void _flushControllersToCubit() {
    context.read<CustomerFormCubit>().updateFields(
      name: _nomeController.text.trim(),
      phoneNumber: _telefonoController.text.trim(),
      email: _emailController.text.trim(),
      note: _noteController.text.trim(),
    );
  }

  void _saveCustomer() {
    if (_formKey.currentState!.validate()) {
      _flushControllersToCubit();
      context.read<CustomerFormCubit>().saveCustomer();
    }
  }

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<CustomerFormCubit, CustomerFormState>(
      listenWhen: (previous, current) => previous.status != current.status,
      listener: (context, state) {
        if (state.status == CustomerFormStatus.ready && !_isInitialized) {
          _syncTextControllers(state.customer);
        }
        if (state.status == CustomerFormStatus.success) {
          Navigator.of(context).pop(state.customer);
        } else if (state.status == CustomerFormStatus.failure) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text(state.errorMessage ?? 'Errore sconosciuto')),
          );
        }
      },
      builder: (context, state) {
        return Scaffold(
          appBar: AppBar(
            title: Text(
              state.customer.id == null
                  ? 'Nuovo Cliente'
                  : 'Modifica ${state.customer.name}',
            ),
            actions: [],
          ),
          body: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Form(
              key: _formKey,
              child: SingleChildScrollView(
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Row(
                      children: [
                        ChoiceChip(
                          label: const Text('Privato (Domestico)'),
                          selected: !state.customer.isBusiness,
                          selectedColor: Colors.blue.withValues(alpha: 0.2),
                          checkmarkColor: Colors.blue.shade700,
                          onSelected: (selected) {
                            if (selected) {
                              context.read<CustomerFormCubit>().updateFields(
                                isBusiness: false,
                              );
                            }
                          },
                        ),
                        const SizedBox(width: 12),
                        ChoiceChip(
                          label: const Text('Business (P.IVA)'),
                          selected: state.customer.isBusiness,
                          selectedColor: Colors.orange.withValues(alpha: 0.2),
                          checkmarkColor: Colors.orange.shade700,
                          onSelected: (selected) {
                            if (selected) {
                              context.read<CustomerFormCubit>().updateFields(
                                isBusiness: true,
                              );
                            }
                          },
                        ),
                      ],
                    ),
                    const Divider(height: 32),
                    FluxTextField(
                      label: 'Nome Completo',
                      autoFocus: true,
                      icon: Icons.person_outline,
                      controller: _nomeController,
                      keyboardType: TextInputType.name,
                    ),
                    const SizedBox(height: 16),
                    FluxTextField(
                      label: 'Telefono',
                      icon: Icons.phone_android_outlined,
                      controller: _telefonoController,
                      keyboardType: TextInputType.phone,
                    ),
                    const SizedBox(height: 16),
                    FluxTextField(
                      label: 'Email',
                      icon: Icons.alternate_email_outlined,
                      controller: _emailController,
                      keyboardType: TextInputType.emailAddress,
                    ),
                    const SizedBox(height: 16),
                    FluxTextField(
                      label: 'Note',
                      icon: Icons.notes_outlined,
                      controller: _noteController,
                      minLines: 3,
                    ),
                    const SizedBox(height: 8),
                    SwitchListTile(
                      title: const Text('Non disturbare'),
                      value: state.customer.doNotDisturb,
                      onChanged: (v) => context
                          .read<CustomerFormCubit>()
                          .updateDoNotDisturb(v),
                    ),
                    const Divider(height: 32),
                    BlocProvider<AttachmentsBloc>(
                      create: (context) => AttachmentsBloc(
                        parentType: AttachmentParentType.customer,
                        parentId: state.customer.id,
                      ),
                      child: SharedAttachmentsSection(
                        parentType: AttachmentParentType.customer,
                        parentId: state.customer.id,
                      ),
                    ),
                    const SizedBox(height: 24),
                    SizedBox(
                      width: double.infinity,
                      height: 50,
                      child: ElevatedButton(
                        onPressed: _saveCustomer,
                        style: ElevatedButton.styleFrom(
                          backgroundColor: Theme.of(context).primaryColor,
                          foregroundColor: Colors.white,
                        ),

                        child: Text(
                          widget.customer == null ? 'SALVA' : 'AGGIORNA',
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
