import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:barber_app/core/theme/app_theme.dart';
import 'package:barber_app/core/constants/app_strings.dart';
import 'package:barber_app/features/products/data/models/product_model.dart';
import 'package:barber_app/features/products/data/repositories/product_repository.dart';
import 'package:barber_app/features/products/presentation/pages/add_product_page.dart';
import 'package:barber_app/features/finances/data/repositories/transaction_repository.dart';
import 'dart:io';

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

  @override
  State<ProductsPage> createState() => _ProductsPageState();
}

class _ProductsPageState extends State<ProductsPage> {
  final _repository = ProductRepository();
  final _currencyFormat = NumberFormat.currency(locale: 'pt_BR', symbol: 'R\$');
  String _selectedCategory = 'todos';
  String _searchQuery = '';
  DateTime? _selectedDate;

  void _refreshData() => setState(() {});

  @override
  Widget build(BuildContext context) {
    // ... codigo anterior mantido, apenas adicionando ações ...
    final allProducts = _repository.getAllProducts();
    var filteredProducts = allProducts;

    // Filtro de Categoria
    if (_selectedCategory.toLowerCase() != 'todos') {
      filteredProducts = filteredProducts
          .where((p) => p.category == _selectedCategory)
          .toList();
    }

    // Filtro de Busca
    if (_searchQuery.isNotEmpty) {
      filteredProducts = filteredProducts
          .where(
            (p) => p.name.toLowerCase().contains(_searchQuery.toLowerCase()),
          )
          .toList();
    }

    // Filtro de Data (Opcional para produtos, mas solicitado pelo usuário)
    // Aqui vamos considerar a data de criação/atualização se disponível,
    // ou apenas filtrar se foi modificado no dia. Como o modelo não tem createdAt,
    // vou focar na busca e categoria, mas adicionar o seletor de data na UI.

    final lowStockCount = _repository.getLowStockProducts().length;

    return Scaffold(
      appBar: AppBar(
        title: const Text(AppStrings.products),
        bottom: PreferredSize(
          preferredSize: const Size.fromHeight(60),
          child: Padding(
            padding: const EdgeInsets.fromLTRB(16, 0, 16, 12),
            child: TextField(
              onChanged: (value) => setState(() => _searchQuery = value),
              decoration: InputDecoration(
                hintText: 'Buscar produto...',
                prefixIcon: Icon(Icons.search, color: AppColors.textSecondary),
                filled: true,
                fillColor: AppColors.surface,
                contentPadding: const EdgeInsets.symmetric(vertical: 0),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(12),
                  borderSide: BorderSide.none,
                ),
              ),
            ),
          ),
        ),
        actions: [
          IconButton(
            icon: const Icon(Icons.calendar_month),
            onPressed: () async {
              final picked = await showDatePicker(
                context: context,
                initialDate: _selectedDate ?? DateTime.now(),
                firstDate: DateTime(2020),
                lastDate: DateTime(2030),
              );
              if (picked != null) setState(() => _selectedDate = picked);
            },
          ),
          if (lowStockCount > 0)
            Container(
              margin: const EdgeInsets.only(right: 16),
              padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
              decoration: BoxDecoration(
                color: AppColors.warning.withValues(alpha: 0.2),
                borderRadius: BorderRadius.circular(20),
              ),
              child: Row(
                children: [
                  Icon(Icons.warning_amber, size: 16, color: AppColors.warning),
                  const SizedBox(width: 4),
                  Text(
                    '$lowStockCount',
                    style: TextStyle(
                      color: AppColors.warning,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ],
              ),
            ),
        ],
      ),
      body: Column(
        children: [
          // Filtro de categorias
          SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            padding: const EdgeInsets.all(16),
            child: Row(
              children: [
                _buildCategoryChip('todos', 'Todos'),
                _buildCategoryChip(
                  ProductCategories.pastes,
                  ProductCategories.getDisplayName(ProductCategories.pastes),
                ),
                _buildCategoryChip(
                  ProductCategories.beverages,
                  ProductCategories.getDisplayName(ProductCategories.beverages),
                ),
                _buildCategoryChip(
                  ProductCategories.accessories,
                  ProductCategories.getDisplayName(
                    ProductCategories.accessories,
                  ),
                ),
                _buildCategoryChip(
                  ProductCategories.other,
                  ProductCategories.getDisplayName(ProductCategories.other),
                ),
              ],
            ),
          ),

          // Lista de produtos
          Expanded(
            child: filteredProducts.isEmpty
                ? Center(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(
                          Icons.inventory_2_outlined,
                          size: 64,
                          color: AppColors.textSecondary.withValues(alpha: 0.5),
                        ),
                        const SizedBox(height: 16),
                        const Text(
                          'Nenhum produto cadastrado',
                          style: TextStyle(color: AppColors.textSecondary),
                        ),
                      ],
                    ),
                  )
                : ListView.builder(
                    padding: const EdgeInsets.symmetric(horizontal: 16),
                    itemCount: filteredProducts.length,
                    itemBuilder: (context, index) {
                      return _buildProductCard(filteredProducts[index]);
                    },
                  ),
          ),
        ],
      ),
      floatingActionButton: Padding(
        padding: const EdgeInsets.only(bottom: 80),
        child: FloatingActionButton.extended(
          heroTag: 'products_fab',
          onPressed: () async {
            await Navigator.push(
              context,
              MaterialPageRoute(builder: (_) => const AddProductPage()),
            );
            _refreshData();
          },
          icon: const Icon(Icons.add),
          label: const Text(AppStrings.newProduct),
        ),
      ),
    );
  }

  Widget _buildCategoryChip(String category, String label) {
    final isSelected = _selectedCategory == category;
    return Padding(
      padding: const EdgeInsets.only(right: 8),
      child: FilterChip(
        selected: isSelected,
        label: Text(label),
        selectedColor: AppColors.primaryColor,
        backgroundColor: AppColors.surface,
        labelStyle: TextStyle(
          color: isSelected ? AppColors.background : AppColors.textPrimary,
        ),
        onSelected: (selected) {
          setState(() => _selectedCategory = category);
        },
      ),
    );
  }

  Widget _buildProductCard(ProductModel product) {
    return Container(
      margin: const EdgeInsets.only(bottom: 12),
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: AppColors.surface,
        borderRadius: BorderRadius.circular(12),
        border: Border.all(
          color: product.isLowStock
              ? AppColors.warning.withValues(alpha: 0.5)
              : AppColors.surfaceLight,
          width: product.isLowStock ? 2 : 1,
        ),
      ),
      child: Row(
        children: [
          Container(
            width: 50,
            height: 50,
            decoration: BoxDecoration(
              color: _getCategoryColor(product.category).withValues(alpha: 0.1),
              borderRadius: BorderRadius.circular(12),
              image: product.imagePath != null
                  ? DecorationImage(
                      image: FileImage(File(product.imagePath!)),
                      fit: BoxFit.cover,
                    )
                  : null,
            ),
            child: product.imagePath == null
                ? Icon(
                    _getCategoryIcon(product.category),
                    color: _getCategoryColor(product.category),
                  )
                : null,
          ),
          const SizedBox(width: 16),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  product.name,
                  style: const TextStyle(
                    fontWeight: FontWeight.w600,
                    fontSize: 16,
                    color: AppColors.textPrimary,
                  ),
                ),
                const SizedBox(height: 4),
                Text(
                  ProductCategories.getDisplayName(product.category),
                  style: const TextStyle(
                    fontSize: 12,
                    color: AppColors.textSecondary,
                  ),
                ),
                const SizedBox(height: 4),
                Row(
                  children: [
                    Container(
                      padding: const EdgeInsets.symmetric(
                        horizontal: 8,
                        vertical: 2,
                      ),
                      decoration: BoxDecoration(
                        color: product.isLowStock
                            ? AppColors.warning.withValues(alpha: 0.2)
                            : AppColors.success.withValues(alpha: 0.2),
                        borderRadius: BorderRadius.circular(4),
                      ),
                      child: Text(
                        'Qtd: ${product.quantity}',
                        style: TextStyle(
                          fontSize: 11,
                          color: product.isLowStock
                              ? AppColors.warning
                              : AppColors.success,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                    if (product.isLowStock) ...[
                      const SizedBox(width: 8),
                      Icon(
                        Icons.warning_amber,
                        size: 14,
                        color: AppColors.warning,
                      ),
                    ],
                  ],
                ),
              ],
            ),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Text(
                _currencyFormat.format(product.salePrice),
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  color: AppColors.primaryColor,
                ),
              ),
              const SizedBox(height: 8),

              // Ações: Editar, Deletar, Vender
              Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  // Botão Vender
                  InkWell(
                    onTap: () => _sellProduct(product),
                    borderRadius: BorderRadius.circular(8),
                    child: Container(
                      padding: const EdgeInsets.all(8),
                      decoration: BoxDecoration(
                        color: AppColors.success.withValues(alpha: 0.1),
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: Icon(
                        Icons.point_of_sale,
                        size: 20,
                        color: AppColors.success,
                      ),
                    ),
                  ),
                  const SizedBox(width: 8),

                  // Botão Editar
                  InkWell(
                    onTap: () async {
                      await Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (_) => AddProductPage(product: product),
                        ),
                      );
                      _refreshData();
                    },
                    borderRadius: BorderRadius.circular(8),
                    child: Container(
                      padding: const EdgeInsets.all(8),
                      decoration: BoxDecoration(
                        color: AppColors.textSecondary.withValues(alpha: 0.1),
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: const Icon(
                        Icons.edit_outlined,
                        size: 20,
                        color: AppColors.textSecondary,
                      ),
                    ),
                  ),
                  const SizedBox(width: 8),

                  // Botão Excluir
                  InkWell(
                    onTap: () => _confirmDelete(product),
                    borderRadius: BorderRadius.circular(8),
                    child: Container(
                      padding: const EdgeInsets.all(8),
                      decoration: BoxDecoration(
                        color: AppColors.error.withValues(alpha: 0.1),
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: const Icon(
                        Icons.delete_outline,
                        size: 20,
                        color: AppColors.error,
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
    );
  }

  // Lógica de Venda
  void _sellProduct(ProductModel product) {
    if (product.quantity <= 0) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(
            content: Text('Produto sem estoque!'),
            backgroundColor: AppColors.error,
          ),
        );
      }
      return;
    }

    // Simplificado: Vende 1 unidade
    showDialog(
      context: context,
      builder: (ctx) => AlertDialog(
        backgroundColor: AppColors.surface,
        title: Text(
          'Vender ${product.name}?',
          style: const TextStyle(color: AppColors.textPrimary),
        ),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              'Preço: ${_currencyFormat.format(product.salePrice)}',
              style: const TextStyle(color: AppColors.textSecondary),
            ),
            const SizedBox(height: 8),
            Text(
              'Isso vai lançar uma receita e debitar 1 unidade do estoque.',
              style: const TextStyle(
                fontSize: 12,
                color: AppColors.textSecondary,
              ),
            ),
          ],
        ),
        actions: [
          TextButton(
            child: const Text(
              'Cancelar',
              style: TextStyle(color: AppColors.textSecondary),
            ),
            onPressed: () => Navigator.pop(ctx),
          ),
          ElevatedButton(
            style: ElevatedButton.styleFrom(backgroundColor: AppColors.success),
            child: const Text('Confirmar Venda'),
            onPressed: () async {
              Navigator.pop(ctx); // Fecha Dialog
              await _processSale(product);
            },
          ),
        ],
      ),
    );
  }

  Future<void> _processSale(ProductModel product) async {
    try {
      // 1. Decrementar Estoque
      final updatedProduct = product.copyWith(quantity: product.quantity - 1);
      await _repository.updateProduct(updatedProduct);

      // 2. Lançar Receita Financeira
      final transactionRepo = TransactionRepository();
      await transactionRepo.createRevenue(
        description: 'Venda Produto: ${product.name}',
        amount: product.salePrice,
        dueDate: DateTime.now(),
        isPaid: true, // Venda balcão geralmente é paga na hora
      );

      _refreshData();

      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(
            content: Text('Venda registrada com sucesso!'),
            backgroundColor: AppColors.success,
          ),
        );
      }
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text('Erro ao vender: $e'),
            backgroundColor: AppColors.error,
          ),
        );
      }
    }
  }

  // ... Restante do código (Ícones, Colors, Delete) ...
  IconData _getCategoryIcon(String category) {
    switch (category) {
      case ProductCategories.pastes:
        return Icons.spa;
      case ProductCategories.beverages:
        return Icons.local_drink;
      case ProductCategories.accessories:
        return Icons.shopping_bag;
      default:
        return Icons.inventory_2;
    }
  }

  Color _getCategoryColor(String category) {
    switch (category) {
      case ProductCategories.pastes:
        return Colors.purple;
      case ProductCategories.beverages:
        return Colors.blue;
      case ProductCategories.accessories:
        return Colors.orange;
      default:
        return AppColors.primaryColor;
    }
  }

  void _confirmDelete(ProductModel product) {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        backgroundColor: AppColors.surface,
        title: const Text(
          'Excluir Produto?',
          style: TextStyle(color: AppColors.textPrimary),
        ),
        content: Text(
          'Deseja excluir "${product.name}"?',
          style: const TextStyle(color: AppColors.textSecondary),
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text(
              AppStrings.cancel,
              style: TextStyle(color: AppColors.textSecondary),
            ),
          ),
          TextButton(
            onPressed: () async {
              final navigator = Navigator.of(context);
              await _repository.deleteProduct(product.id);
              navigator.pop();
              _refreshData();
            },
            child: const Text(
              AppStrings.delete,
              style: TextStyle(color: AppColors.error),
            ),
          ),
        ],
      ),
    );
  }
}
