import 'package:flutter/material.dart';
import '../core/theme/app_colors.dart';
import '../core/theme/app_typography.dart';

void showUpgradePrompt(BuildContext context, {String? feature}) {
  showModalBottomSheet(
    context: context,
    backgroundColor: Colors.transparent,
    isScrollControlled: true,
    builder: (_) => _UpgradeSheet(feature: feature),
  );
}

class _UpgradeSheet extends StatelessWidget {
  final String? feature;
  const _UpgradeSheet({this.feature});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: AppColors.navy,
        borderRadius: const BorderRadius.vertical(top: Radius.circular(24)),
        border: Border.all(color: AppColors.glassBorderStrong),
      ),
      padding: EdgeInsets.fromLTRB(24, 8, 24, MediaQuery.of(context).padding.bottom + 24),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          // Handle
          Container(
            width: 40, height: 4,
            margin: const EdgeInsets.only(bottom: 24),
            decoration: BoxDecoration(
              color: AppColors.glassBorder,
              borderRadius: BorderRadius.circular(2),
            ),
          ),

          // Icon
          Container(
            width: 72, height: 72,
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              gradient: AppColors.goldGradient,
              boxShadow: [BoxShadow(color: AppColors.gold.withValues(alpha: 0.25), blurRadius: 24)],
            ),
            child: const Icon(Icons.rocket_launch_rounded, color: AppColors.navy, size: 32),
          ),
          const SizedBox(height: 20),

          Text('Daily Limit Reached', style: AppTypography.headingLg.copyWith(color: Colors.white)),
          const SizedBox(height: 8),

          Text(
            feature != null
                ? 'You\'ve used all your free $feature for today. Upgrade to Pro for unlimited access.'
                : 'You\'ve reached your free plan limit for today. Upgrade to Pro for unlimited access.',
            style: AppTypography.bodyMd.copyWith(color: AppColors.textSecondary, height: 1.5),
            textAlign: TextAlign.center,
          ),
          const SizedBox(height: 24),

          // Benefits
          Container(
            padding: const EdgeInsets.all(16),
            decoration: BoxDecoration(
              color: AppColors.glassSurface,
              borderRadius: BorderRadius.circular(16),
              border: Border.all(color: AppColors.glassBorder),
            ),
            child: Column(
              children: [
                _benefitRow(Icons.all_inclusive_rounded, 'Unlimited AI queries'),
                const SizedBox(height: 10),
                _benefitRow(Icons.description_rounded, 'Unlimited document scans'),
                const SizedBox(height: 10),
                _benefitRow(Icons.phone_rounded, 'Unlimited voice calls'),
                const SizedBox(height: 10),
                _benefitRow(Icons.speed_rounded, 'Priority response speed'),
                const SizedBox(height: 10),
                _benefitRow(Icons.history_rounded, 'Extended chat history'),
              ],
            ),
          ),
          const SizedBox(height: 24),

          // Upgrade button
          GestureDetector(
            onTap: () {
              Navigator.pop(context);
              Navigator.of(context).pushNamed('/stats');
            },
            child: Container(
              width: double.infinity,
              height: 52,
              decoration: BoxDecoration(
                gradient: AppColors.goldGradient,
                borderRadius: BorderRadius.circular(14),
                boxShadow: [
                  BoxShadow(color: AppColors.gold.withValues(alpha: 0.4), blurRadius: 20, offset: const Offset(0, 6)),
                ],
              ),
              child: Center(
                child: Text('Upgrade to Pro', style: AppTypography.button.copyWith(color: AppColors.navy, fontWeight: FontWeight.w700)),
              ),
            ),
          ),
          const SizedBox(height: 12),

          // Dismiss
          GestureDetector(
            onTap: () => Navigator.pop(context),
            child: Padding(
              padding: const EdgeInsets.all(8),
              child: Text('Maybe later', style: AppTypography.bodySm.copyWith(color: AppColors.textMuted)),
            ),
          ),
        ],
      ),
    );
  }

  Widget _benefitRow(IconData icon, String text) {
    return Row(
      children: [
        Icon(icon, color: AppColors.gold, size: 18),
        const SizedBox(width: 12),
        Text(text, style: AppTypography.bodyMd.copyWith(color: Colors.white)),
      ],
    );
  }
}
