import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:kasy_kit/components/components.dart';
import 'package:kasy_kit/core/bottom_menu/kasy_bart_navigation.dart';
import 'package:kasy_kit/core/theme/theme.dart';
import 'package:kasy_kit/features/notifications/providers/models/notification.dart';
import 'package:kasy_kit/features/notifications/repositories/notifications_repository.dart';
import 'package:kasy_kit/i18n/translations.g.dart';

class EmptyNotifications extends ConsumerStatefulWidget {
  const EmptyNotifications({super.key});

  @override
  ConsumerState<EmptyNotifications> createState() => _EmptyNotificationsState();
}

class _EmptyNotificationsState extends ConsumerState<EmptyNotifications> {
  late Future<NotificationPermission> _permissionFuture;

  @override
  void initState() {
    super.initState();
    _permissionFuture = _loadPermission();
  }

  Future<NotificationPermission> _loadPermission() {
    return ref.read(notificationRepositoryProvider).getPermissionStatus();
  }

  Future<void> _onAskPressed(NotificationPermission permission) async {
    await permission.maybeAsk();
    if (!mounted) return;
    // The native permission dialog pulls the app out of focus; restore the
    // bottom bar defensively before refreshing the local state.
    kasyShowBottomBar(context);
    setState(() {
      _permissionFuture = _loadPermission();
    });
  }

  @override
  Widget build(BuildContext context) {
    final tr = context.t.notifications;
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: KasySpacing.md),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const SizedBox(height: KasySpacing.xxxl),
          Container(
            width: 80,
            height: 80,
            decoration: BoxDecoration(
              color: context.colors.primary.withValues(alpha: 0.10),
              shape: BoxShape.circle,
            ),
            child: Icon(
              KasyIcons.notificationOff,
              size: 36,
              color: context.colors.primary.withValues(alpha: 0.6),
            ),
          ),
          const SizedBox(height: KasySpacing.md),
          Text(
            tr.empty_title,
            textAlign: TextAlign.center,
            style: context.textTheme.titleMedium?.copyWith(
              color: context.colors.onSurface,
              fontWeight: FontWeight.w600,
            ),
          ),
          const SizedBox(height: KasySpacing.xs),
          Text(
            tr.empty_subtitle,
            textAlign: TextAlign.center,
            style: context.textTheme.bodyMedium?.copyWith(
              color: context.colors.muted,
            ),
          ),
          const SizedBox(height: KasySpacing.xl),
          FutureBuilder<NotificationPermission>(
            future: _permissionFuture,
            builder: (context, snapshot) {
              final permission = snapshot.data;
              if (permission == null) return const SizedBox.shrink();
              if (permission is NotificationPermissionGranted) {
                return const SizedBox.shrink();
              }
              final isLocked =
                  permission is NotificationPermissionPermanentlyDenied;
              return KasyButton(
                label: isLocked ? tr.empty_cta_open_settings : tr.empty_cta,
                variant: KasyButtonVariant.soft,
                icon: isLocked
                    ? KasyIcons.settings
                    : KasyIcons.notificationAdd,
                onPressed: () => _onAskPressed(permission),
              );
            },
          ),
          const SizedBox(height: KasySpacing.xxxl),
        ],
      ),
    );
  }
}
