import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:google_fonts/google_fonts.dart';
import '../../theme/app_theme.dart';
import '../../providers/user_provider.dart';

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

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

    if (userProfile == null) {
      return Scaffold(
        appBar: AppBar(
          title: const Text('Sharing Settings'),
        ),
        body: const Center(child: CircularProgressIndicator()),
      );
    }

    return Scaffold(
      appBar: AppBar(
        title: const Text('Sharing Settings'),
      ),
      body: ListView(
        padding: const EdgeInsets.all(16.0),
        children: [
          ListTile(
            leading: const Icon(Icons.link),
            title: const Text('Link with Husband'),
            subtitle: Text(userProfile.partnerName != null
                ? 'Linked to ${userProfile.partnerName}'
                : 'Not linked'),
            trailing: const Icon(Icons.chevron_right),
            onTap: () => _showShareDialog(context, ref),
          ),
          const Divider(),
          SwitchListTile(
            title: const Text('Share Moods'),
            value: userProfile.shareMoods,
            onChanged: (value) {
              ref
                  .read(userProfileProvider.notifier)
                  .updateProfile(userProfile.copyWith(shareMoods: value));
            },
          ),
          SwitchListTile(
            title: const Text('Share Symptoms'),
            value: userProfile.shareSymptoms,
            onChanged: (value) {
              ref
                  .read(userProfileProvider.notifier)
                  .updateProfile(userProfile.copyWith(shareSymptoms: value));
            },
          ),
          SwitchListTile(
            title: const Text('Share Cravings'),
            value: userProfile.shareCravings,
            onChanged: (value) {
              ref
                  .read(userProfileProvider.notifier)
                  .updateProfile(userProfile.copyWith(shareCravings: value));
            },
          ),
          SwitchListTile(
            title: const Text('Share Energy Levels'),
            value: userProfile.shareEnergyLevels,
            onChanged: (value) {
              ref.read(userProfileProvider.notifier).updateProfile(
                  userProfile.copyWith(shareEnergyLevels: value));
            },
          ),
          SwitchListTile(
            title: const Text('Share Sleep Data'),
            value: userProfile.shareSleep,
            onChanged: (value) {
              ref
                  .read(userProfileProvider.notifier)
                  .updateProfile(userProfile.copyWith(shareSleep: value));
            },
          ),
          SwitchListTile(
            title: const Text('Share Intimacy Details'),
            value: userProfile.shareIntimacy,
            onChanged: (value) {
              ref
                  .read(userProfileProvider.notifier)
                  .updateProfile(userProfile.copyWith(shareIntimacy: value));
            },
          ),
        ],
      ),
    );
  }

  void _showShareDialog(BuildContext context, WidgetRef ref) {
    // Generate a simple pairing code
    final userProfile = ref.read(userProfileProvider);
    final pairingCode =
        userProfile?.id.substring(0, 6).toUpperCase() ?? 'ABC123';

    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Row(
          children: [
            Icon(Icons.share_outlined, color: AppColors.navyBlue),
            SizedBox(width: 8),
            Text('Share with Husband'),
          ],
        ),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              'Share this code with your husband so he can connect to your cycle data:',
              style:
                  GoogleFonts.outfit(fontSize: 14, color: AppColors.warmGray),
            ),
            const SizedBox(height: 24),
            Container(
              padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
              decoration: BoxDecoration(
                color: AppColors.navyBlue.withValues(alpha: 0.1),
                borderRadius: BorderRadius.circular(12),
                border: Border.all(
                    color: AppColors.navyBlue.withValues(alpha: 0.3)),
              ),
              child: SelectableText(
                pairingCode,
                style: GoogleFonts.outfit(
                  fontSize: 32,
                  fontWeight: FontWeight.bold,
                  letterSpacing: 4,
                  color: AppColors.navyBlue,
                ),
              ),
            ),
            const SizedBox(height: 16),
            Text(
              'He can enter this in his app under Settings > Connect with Wife.',
              style:
                  GoogleFonts.outfit(fontSize: 12, color: AppColors.warmGray),
              textAlign: TextAlign.center,
            ),
          ],
        ),
        actions: [
          ElevatedButton(
            onPressed: () => Navigator.pop(context),
            style: ElevatedButton.styleFrom(
              backgroundColor: AppColors.navyBlue,
              foregroundColor: Colors.white,
            ),
            child: const Text('Done'),
          ),
        ],
      ),
    );
  }
}
