import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../../core/auth/onboarding_intent_provider.dart';
import '../../../core/theme/halo_tokens.dart';
import '../../../core/theme/widgets/widgets.dart';
import '../../onboarding/usp_seen_provider.dart';

/// Result of the post-name Verif Choice Sheet. Caller routes to the matching
/// onboarding sub-flow.
enum VerifChoice { verified, anonymous }

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

  /// Show the sheet and return the user's choice (`null` if dismissed).
  static Future<VerifChoice?> show(BuildContext context) {
    return HaloBottomSheet.show<VerifChoice>(
      context,
      child: const VerifChoiceSheet(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        const Text(
          'Mau curhat sebagai siapa?',
          style: TextStyle(
            fontFamily: HaloTokens.fontDisplay,
            fontSize: 22,
            height: 28 / 22,
            fontWeight: FontWeight.w700,
            color: HaloTokens.ink,
          ),
        ),
        const SizedBox(height: HaloSpacing.s8),
        const Text(
          'Verifikasi nomor HP biar bisa dapet diskon sesi pertama dan riwayat curhatmu kesimpan. Atau langsung curhat anonim, nggak perlu daftar.',
          style: TextStyle(
            fontFamily: HaloTokens.fontBody,
            fontSize: 14,
            height: 20 / 14,
            color: HaloTokens.inkSoft,
          ),
        ),
        const SizedBox(height: HaloSpacing.s24),
        HaloButton(
          label: 'verifikasi nomor HP',
          fullWidth: true,
          onPressed: () =>
              Navigator.of(context).pop(VerifChoice.verified),
        ),
        const SizedBox(height: HaloSpacing.s12),
        HaloButton(
          label: 'curhat anonim',
          variant: HaloButtonVariant.secondary,
          fullWidth: true,
          onPressed: () =>
              Navigator.of(context).pop(VerifChoice.anonymous),
        ),
      ],
    );
  }
}

/// Helper: route to the right onboarding sub-flow for a verif choice.
///
/// Phase 4 (2026-05-12): the S5 ESP screen is retired and S5b USP is now a
/// one-time gate. If the user has already seen USP (local SharedPreferences
/// flag, OR-merged with `customers.usp_seen` on login), we skip USP entirely
/// and jump to the per-branch next step.
Future<void> routeForVerifChoice(
  BuildContext context,
  WidgetRef ref,
  VerifChoice choice,
) async {
  final seen = await ref.read(uspSeenProvider.future);
  if (!context.mounted) return;
  switch (choice) {
    case VerifChoice.verified:
      // §2 transaction CTA path — router consumes this post-OTP and routes
      // to /payment/entry (S6 paywall vs PickMethod via first_session_discount).
      ref.read(onboardingIntentProvider.notifier).state =
          OnboardingIntent.onboarding;
      context.push(seen ? '/auth/register' : '/onboarding/verif/usp');
      break;
    case VerifChoice.anonymous:
      // `/onboarding/anon/method` redirects to `/payment/method-pick`; use the
      // canonical destination here.
      context.push(seen ? '/payment/method-pick' : '/onboarding/anon/usp');
      break;
  }
}
