// lib/ui/screens/settings_screen.dart
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../core/feature_flags.dart';
import '../../game/models/season.dart';
import '../../l10n/gen/app_localizations.dart';
import '../../state/providers.dart';
import '../widgets/season_background.dart';

class SettingsScreen extends ConsumerWidget {
  const SettingsScreen({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final l10n = AppLocalizations.of(context)!;
    final soundOn = ref.watch(soundEnabledProvider);
    final musicOn = ref.watch(musicEnabledProvider);

    return Stack(
      fit: StackFit.expand,
      children: [
        const SeasonBackground(theme: SeasonTheme.fallback),
        Scaffold(
          backgroundColor: Colors.transparent,
          appBar: AppBar(
            backgroundColor: Colors.transparent,
            title: Text(l10n.settings),
          ),
          body: ListView(
            padding: const EdgeInsets.all(16),
            children: [
              SwitchListTile(
                title: Text(l10n.soundAndVibration),
                value: soundOn,
                onChanged: (v) =>
                    ref.read(soundEnabledProvider.notifier).set(v),
              ),
              SwitchListTile(
                title: Text(l10n.music),
                value: musicOn,
                onChanged: (v) =>
                    ref.read(musicEnabledProvider.notifier).set(v),
              ),
              // The "Remove Ads" purchase is gated off at launch (no merchant
              // agreement); the app ships ad-supported only. See kIapEnabled.
              if (kIapEnabled) ..._iapTiles(context, ref, l10n),
              const SizedBox(height: 24),
              Center(
                child: Text(
                  'Block Seasons · v1.0.0',
                  style: TextStyle(
                    color: Colors.white.withValues(alpha: 0.4),
                    fontSize: 12,
                  ),
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  List<Widget> _iapTiles(
      BuildContext context, WidgetRef ref, AppLocalizations l10n) {
    final adsRemoved = ref.watch(adsRemovedProvider);
    final iap = ref.read(iapServiceProvider);

    ref.listen<bool>(adsRemovedProvider, (prev, next) {
      if (next && !(prev ?? false)) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text(l10n.adsRemovedThanks)),
        );
      }
    });

    return [
      const Divider(),
      ListTile(
        title: Text(l10n.removeAds),
        subtitle: Text(l10n.removeAdsDescription),
        trailing: adsRemoved
            ? const Icon(Icons.check_circle, color: Colors.green)
            : Text(iap.product?.price ?? ''),
        onTap: adsRemoved
            ? null
            : () async {
                if (!iap.available || iap.product == null) {
                  ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(content: Text(l10n.purchaseUnavailable)),
                  );
                  return;
                }
                await iap.buyRemoveAds();
              },
      ),
      const Divider(),
      ListTile(
        leading: const Icon(Icons.restore),
        title: Text(l10n.restorePurchases),
        onTap: () => iap.restorePurchases(),
      ),
    ];
  }
}
