import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:gap/gap.dart';
import '../../ui/theme/app_theme.dart';
import '../../ui/widgets/structure_widgets.dart';
import '../../services/firestore_service.dart';
import '../../services/auth_service.dart';

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

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final prefsAsync = ref.watch(preferencesProvider);

    return Column(
      children: [
        Padding(
          padding: const EdgeInsets.all(24.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Row(
                children: [
                  Container(
                    width: 40,
                    height: 40,
                    decoration: const BoxDecoration(
                      color: AppTheme.energyLow,
                      shape: BoxShape.circle,
                    ),
                    child: const Center(
                      child: Text("🧔", style: TextStyle(fontSize: 24)),
                    ),
                  ),
                  const Gap(12),
                  Text("Preferences", style: AppTheme.textTheme.displayMedium),
                ],
              ),
              IconButton(
                // Logout button
                icon: const Icon(Icons.logout),
                onPressed: () {
                  ref.read(authServiceProvider).signOut();
                },
              ),
            ],
          ),
        ),

        Expanded(
          child: prefsAsync.when(
            data: (prefs) {
              return ListView(
                padding: const EdgeInsets.symmetric(horizontal: 24),
                children: [
                  AyuelCard(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          "Voice Settings",
                          style: AppTheme.textTheme.bodyLarge?.copyWith(
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        const Gap(16),
                        SwitchListTile(
                          title: const Text("Mute voice replies"),
                          subtitle: const Text("Ayuel will only display text."),
                          value: !prefs.voiceRepliesEnabled,
                          onChanged: (val) {
                            final newPrefs = prefs.copyWith(
                              voiceRepliesEnabled: !val,
                            );
                            ref
                                .read(firestoreServiceProvider)
                                .savePreferences(newPrefs);
                          },
                          activeColor: AppTheme.primary,
                        ),
                      ],
                    ),
                  ),
                  const Gap(16),

                  AyuelCard(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          "Boundaries",
                          style: AppTheme.textTheme.bodyLarge?.copyWith(
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        const Gap(16),
                        ...prefs.boundaries.entries.map((entry) {
                          return SwitchListTile(
                            title: Text(entry.key),
                            value: entry.value,
                            onChanged: (val) {
                              final newBoundaries = Map<String, bool>.from(
                                prefs.boundaries,
                              );
                              newBoundaries[entry.key] = val;
                              final newPrefs = prefs.copyWith(
                                boundaries: newBoundaries,
                              );
                              ref
                                  .read(firestoreServiceProvider)
                                  .savePreferences(newPrefs);
                            },
                            activeColor: AppTheme.primary,
                          );
                        }).toList(),
                      ],
                    ),
                  ),

                  const Gap(16),
                  // Reset Button logic could go here
                ],
              );
            },
            loading: () => const Center(child: CircularProgressIndicator()),
            error: (e, st) => Center(child: Text("Error: $e")),
          ),
        ),
      ],
    );
  }
}
