import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/theme/theme.dart';
import 'package:flux/features/master_data/products/blocs/product_cubit.dart';
import 'package:flux/features/master_data/products/ui/brand_selector.dart';
import 'package:flux/features/master_data/products/ui/models_list.dart';
import 'package:go_router/go_router.dart';

class ProductsScreen extends StatelessWidget {
  const ProductsScreen({super.key});

  @override
  Widget build(BuildContext context) {
    // Carichiamo i brand appena la pagina viene creata
    context.read<ProductsCubit>().loadBrands();

    return Scaffold(
      backgroundColor: context.background,
      appBar: AppBar(
        backgroundColor: context.background,
        elevation: 0,
        centerTitle: false,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () => context.pop(), // Torna alla Dashboard
        ),
        title: Text(
          "Anagrafica Prodotti",
          style: TextStyle(
            color: context.primaryText,
            fontWeight: FontWeight.bold,
            fontSize: 20,
          ),
        ),
      ),
      body: BlocConsumer<ProductsCubit, ProductState>(
        listener: (context, state) {
          if (state.status == ProductStatus.error) {
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(content: Text(state.errorMessage ?? 'Errore')),
            );
          }
        },
        builder: (context, state) {
          return Padding(
            padding: const EdgeInsets.all(24.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  "Marche e Modelli",
                  style: TextStyle(
                    fontSize: 28,
                    fontWeight: FontWeight.bold,
                    color: context.accent,
                  ),
                ),
                const SizedBox(height: 24),

                // SEZIONE BRAND
                BrandSelector(state),

                const SizedBox(height: 32),

                // SEZIONE MODELLI
                Expanded(child: ModelsList(state)),
              ],
            ),
          );
        },
      ),
    );
  }
}
