import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../core/models/address_create_request.dart';
import '../../core/models/colonia.dart';
import 'colonias_selector.dart';

class NewAddressPage extends ConsumerStatefulWidget {
  const NewAddressPage({super.key});

  @override
  ConsumerState<NewAddressPage> createState() => _NewAddressPageState();
}

class _NewAddressPageState extends ConsumerState<NewAddressPage> {
  final _formKey = GlobalKey<FormState>();
  final _labelController = TextEditingController();
  final _streetController = TextEditingController();
  Colonia? _selectedColonia;

  @override
  void dispose() {
    _labelController.dispose();
    _streetController.dispose();
    super.dispose();
  }

  void _saveAddress() {
    if (!(_formKey.currentState?.validate() ?? false)) {
      return;
    }
    if (_selectedColonia == null) {
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(const SnackBar(content: Text('Selecciona una colonia')));
      return;
    }

    final address = AddressCreateRequest(
      label: _labelController.text.trim(),
      street: _streetController.text.trim(),
      colonia: _selectedColonia!.nombre,
    );

    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('Domicilio listo: ${address.toJson()}')),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Nuevo domicilio')),
      body: SafeArea(
        child: ListView(
          padding: const EdgeInsets.all(24),
          children: [
            Form(
              key: _formKey,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  TextFormField(
                    controller: _labelController,
                    decoration: const InputDecoration(
                      labelText: 'Etiqueta',
                      hintText: 'Casa, trabajo, etc.',
                    ),
                    validator: (value) =>
                        (value == null || value.trim().isEmpty)
                        ? 'Ingresa una etiqueta'
                        : null,
                  ),
                  const SizedBox(height: 16),
                  TextFormField(
                    controller: _streetController,
                    decoration: const InputDecoration(
                      labelText: 'Calle',
                      hintText: 'Av. Principal 123',
                    ),
                    validator: (value) =>
                        (value == null || value.trim().isEmpty)
                        ? 'Ingresa la calle'
                        : null,
                  ),
                  const SizedBox(height: 16),
                  ColoniasSelector(
                    labelText: 'Colonia',
                    initialValue: _selectedColonia,
                    onChanged: (colonia) {
                      setState(() => _selectedColonia = colonia);
                    },
                  ),
                  const SizedBox(height: 24),
                  SizedBox(
                    height: 52,
                    child: FilledButton(
                      onPressed: _saveAddress,
                      child: const Text('Guardar domicilio'),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
