import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../../warehouse/domain/entities/warehouse_entity.dart';
import '../../../../core/constants/app_constants.dart';
import '../widgets/operation_card.dart';

/// Operation Selection Page
/// Allows users to choose between import and export operations for a selected warehouse
class OperationSelectionPage extends ConsumerWidget {
  final WarehouseEntity warehouse;

  const OperationSelectionPage({
    super.key,
    required this.warehouse,
  });

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final theme = Theme.of(context);
    final colorScheme = theme.colorScheme;

    return Scaffold(
      appBar: AppBar(
        title: const Text('Select Operation'),
        elevation: 0,
      ),
      body: SafeArea(
        child: Column(
          children: [
            // Warehouse information header
            Container(
              width: double.infinity,
              padding: const EdgeInsets.all(AppConstants.defaultPadding),
              decoration: BoxDecoration(
                color: colorScheme.primaryContainer.withValues(alpha: 0.3),
                border: Border(
                  bottom: BorderSide(
                    color: colorScheme.outline.withValues(alpha: 0.2),
                  ),
                ),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    'Warehouse',
                    style: theme.textTheme.labelMedium?.copyWith(
                      color: colorScheme.onSurfaceVariant,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  const SizedBox(height: 4),
                  Text(
                    warehouse.name,
                    style: theme.textTheme.titleLarge?.copyWith(
                      fontWeight: FontWeight.bold,
                      color: colorScheme.onSurface,
                    ),
                  ),
                  const SizedBox(height: 4),
                  Row(
                    children: [
                      Icon(
                        Icons.qr_code,
                        size: 16,
                        color: colorScheme.onSurfaceVariant,
                      ),
                      const SizedBox(width: 4),
                      Text(
                        'Code: ${warehouse.code}',
                        style: theme.textTheme.bodyMedium?.copyWith(
                          color: colorScheme.onSurfaceVariant,
                        ),
                      ),
                      const SizedBox(width: 16),
                      Icon(
                        Icons.inventory_2_outlined,
                        size: 16,
                        color: colorScheme.onSurfaceVariant,
                      ),
                      const SizedBox(width: 4),
                      Text(
                        'Items: ${warehouse.totalCount}',
                        style: theme.textTheme.bodyMedium?.copyWith(
                          color: colorScheme.onSurfaceVariant,
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),

            // Operation cards
            Expanded(
              child: Padding(
                padding: const EdgeInsets.symmetric(
                  vertical: AppConstants.largePadding,
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    // Import Products Card
                    OperationCard(
                      title: 'Import Products',
                      icon: Icons.arrow_downward_rounded,
                      backgroundColor: colorScheme.tertiaryContainer.withValues(alpha: 0.3),
                      iconColor: colorScheme.tertiary,
                      onTap: () => _navigateToProducts(
                        context,
                        warehouse,
                        'import',
                      ),
                    ),

                    const SizedBox(height: AppConstants.defaultPadding),

                    // Export Products Card
                    OperationCard(
                      title: 'Export Products',
                      icon: Icons.arrow_upward_rounded,
                      backgroundColor: colorScheme.primaryContainer.withValues(alpha: 0.3),
                      iconColor: colorScheme.primary,
                      onTap: () => _navigateToProducts(
                        context,
                        warehouse,
                        'export',
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// Navigate to products page with warehouse and operation type
  void _navigateToProducts(
    BuildContext context,
    WarehouseEntity warehouse,
    String operationType,
  ) {
    context.pushNamed(
      'products',
      extra: {
        'warehouse': warehouse,
        'warehouseName': warehouse.name,
        'operationType': operationType,
      },
    );
  }
}
