import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:kasy_kit/core/theme/theme.dart';
import 'package:kasy_kit/features/onboarding/providers/onboarding_provider.dart';
import 'package:kasy_kit/features/onboarding/ui/widgets/onboarding_background.dart';
import 'package:kasy_kit/i18n/translations.g.dart';

class OnboardingLoader extends ConsumerStatefulWidget {
  final VoidCallback onCompleted;

  const OnboardingLoader({super.key, required this.onCompleted});

  @override
  ConsumerState<ConsumerStatefulWidget> createState() =>
      _OnboardingJournalLoaderState();
}

class _OnboardingJournalLoaderState extends ConsumerState<OnboardingLoader> {
  @override
  void initState() {
    super.initState();
    Future.delayed(const Duration(milliseconds: 3500), () async {
      await ref.onboardingNotifier.onOnboardingCompleted();
      widget.onCompleted();
    });
  }

  @override
  Widget build(BuildContext context) {
    final translations = Translations.of(context).onboarding.loading;
    return OnboardingBackground(
      child: SafeArea(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Flexible(
              flex: 0,
              child: Padding(
                padding: const EdgeInsets.fromLTRB(
                  KasySpacing.md,
                  KasySpacing.xl,
                  KasySpacing.md,
                  0,
                ),
                child: Text(
                  translations.title,
                  textAlign: TextAlign.center,
                  style: context.textTheme.headlineMedium?.copyWith(
                    color: context.colors.onBackground,
                  ),
                ),
              ),
            ),
            Flexible(
              flex: 0,
              child: Padding(
                padding: const EdgeInsets.fromLTRB(
                  KasySpacing.xl,
                  KasySpacing.md,
                  KasySpacing.xl,
                  0,
                ),
                child: Text(
                  translations.subtitle,
                  textAlign: TextAlign.center,
                  style: context.textTheme.bodyMedium?.copyWith(
                    color: context.colors.muted,
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ),
            ),
            Padding(
              // Intentionally narrow (100px lateral) — visual design choice for loader
              padding: const EdgeInsets.fromLTRB(100, KasySpacing.xl, 100, 0),
              child: ClipRRect(
                borderRadius: KasyRadius.fullBorderRadius,
                child: LinearProgressIndicator(
                  minHeight: 12,
                  valueColor: AlwaysStoppedAnimation<Color>(
                    context.colors.primary,
                  ),
                  backgroundColor: context.colors.primary.withValues(alpha: 0.1),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
