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

import 'package:doormile/shared/state/app_state.dart';
import 'package:doormile/core/theme/app_colors.dart';
import 'package:doormile/core/theme/app_theme.dart';
import 'package:doormile/core/widgets/common.dart';

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

  @override
  Widget build(BuildContext context) {
    final state = context.watch<AppState>();
    
    // Dynamically calculate stats from real orders
    final totalDeliveries = state.orders.length;
    final totalSpent = state.orders.fold<double>(0, (sum, order) => sum + order.amount);

    return AppBackground(
      child: Column(
        children: [
          const ImmersiveHeader(
            title: 'Summary',
            subtitle: 'Your delivery and eco-impact stats',
            icon: Icons.bar_chart_rounded,
          ),
          Expanded(
            child: SingleChildScrollView(
              padding: const EdgeInsets.only(bottom: 100),
              child: Column(
                children: [
                  const SizedBox(height: 24),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SectionHeader(title: 'Overview', action: 'This Month', onAction: () {}),
                  const SizedBox(height: 12),
                  Row(
                    children: [
                      _StatCard(
                        title: 'Total Deliveries',
                        value: '$totalDeliveries',
                        icon: Icons.inventory_2_rounded,
                        color: AppColors.primary,
                      ),
                      const SizedBox(width: 16),
                      _StatCard(
                        title: 'Total Spent',
                        value: '₹${totalSpent.toStringAsFixed(0)}',
                        icon: Icons.payments_rounded,
                        color: AppColors.tertiary,
                      ),
                    ],
                  ),

                  const SizedBox(height: 24),
                  SectionHeader(title: 'Top Locations', action: 'Manage', onAction: () => Navigator.pushNamed(context, '/addresses')),
                  const SizedBox(height: 12),
                  AppCard(
                    onTap: () => Navigator.pushNamed(context, '/addresses'),
                    padding: const EdgeInsets.all(0),
                    child: Column(
                      children: [
                        _LocationRow(title: 'Home', subtitle: 'Indiranagar, Bengaluru', isFirst: true),
                        const Divider(height: 1, color: AppColors.surfaceContainer),
                        _LocationRow(title: 'Office', subtitle: 'HSR Layout, Bengaluru', isFirst: false),
                      ],
                    ),
                  ),
                ],
              ),
            ),
                  ],
                ),
              ),
            ),
          ],
        ),
      );
  }
}

class _StatCard extends StatelessWidget {
  final String title;
  final String value;
  final IconData icon;
  final Color color;

  const _StatCard({required this.title, required this.value, required this.icon, required this.color});

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: Container(
        padding: const EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(20),
          border: Border.all(color: AppColors.surfaceContainerHigh, width: 1),
          boxShadow: [
            BoxShadow(color: Colors.black.withValues(alpha: 0.03), blurRadius: 10, offset: const Offset(0, 4)),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              padding: const EdgeInsets.all(8),
              decoration: BoxDecoration(
                color: color.withValues(alpha: 0.1),
                borderRadius: BorderRadius.circular(12),
              ),
              child: Icon(icon, color: color, size: 20),
            ),
            const SizedBox(height: 16),
            Text(title, style: AppTheme.caption.copyWith(fontWeight: FontWeight.w600)),
            const SizedBox(height: 4),
            Text(value, style: AppTheme.headlineMd.copyWith(color: AppColors.onSurface)),
          ],
        ),
      ),
    );
  }
}



class _LocationRow extends StatelessWidget {
  final String title;
  final String subtitle;
  final bool isFirst;
  const _LocationRow({required this.title, required this.subtitle, required this.isFirst});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.fromLTRB(16, isFirst ? 16 : 12, 16, isFirst ? 12 : 16),
      child: Row(
        children: [
          Container(
            padding: const EdgeInsets.all(10),
            decoration: BoxDecoration(
              color: AppColors.surfaceContainerLowest,
              shape: BoxShape.circle,
              border: Border.all(color: AppColors.surfaceContainerHigh),
            ),
            child: const Icon(Icons.business_rounded, size: 20, color: AppColors.primary),
          ),
          const SizedBox(width: 16),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(title, style: AppTheme.labelMd.copyWith(fontWeight: FontWeight.w600)),
                const SizedBox(height: 2),
                Text(subtitle, style: AppTheme.caption),
              ],
            ),
          ),
          const Icon(Icons.chevron_right_rounded, color: AppColors.onSurfaceVariant),
        ],
      ),
    );
  }
}
