import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../models/user_profile.dart';
import '../../providers/user_provider.dart';

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

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

    if (userProfile == null) {
      return const Scaffold(body: Center(child: CircularProgressIndicator()));
    }

    return Scaffold(
      appBar: AppBar(
        title: const Text('Cycle Goal'),
      ),
      body: RadioGroup<FertilityGoal>(
        groupValue: userProfile.fertilityGoal,
        onChanged: (FertilityGoal? newValue) {
          if (newValue != null) {
            final currentProfile = ref.read(userProfileProvider);
            if (currentProfile != null) {
              ref.read(userProfileProvider.notifier).updateProfile(
                    currentProfile.copyWith(fertilityGoal: newValue),
                  );
            }
          }
        },
        child: ListView(
          padding: const EdgeInsets.all(16.0),
          children: [
            const Text(
              'What is your current goal?',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 8),
            const Text(
              'Select your primary goal to get personalized insights and predictions.',
              style: TextStyle(fontSize: 14, color: Colors.grey),
            ),
            const SizedBox(height: 24),
            _buildGoalOption(
              context,
              ref,
              title: 'Track Cycle Only',
              subtitle: 'Monitor period and health without fertility focus',
              value: FertilityGoal.justTracking,
              groupValue: userProfile.fertilityGoal,
              icon: Icons.calendar_today,
            ),
            _buildGoalOption(
              context,
              ref,
              title: 'Achieve Pregnancy',
              subtitle: 'Identify fertile window and ovulation',
              value: FertilityGoal.tryingToConceive,
              groupValue: userProfile.fertilityGoal,
              icon: Icons.child_friendly,
            ),
            _buildGoalOption(
              context,
              ref,
              title: 'Avoid Pregnancy',
              subtitle: 'Track fertility for natural family planning',
              value: FertilityGoal.tryingToAvoid,
              groupValue: userProfile.fertilityGoal,
              icon: Icons.security,
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildGoalOption(
    BuildContext context,
    WidgetRef ref, {
    required String title,
    required String subtitle,
    required FertilityGoal value,
    required FertilityGoal? groupValue,
    required IconData icon,
  }) {
    final isSelected = value == groupValue;

    return Card(
      elevation: isSelected ? 2 : 0,
      margin: const EdgeInsets.symmetric(vertical: 8),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
        side: isSelected
            ? BorderSide(color: Theme.of(context).colorScheme.primary, width: 2)
            : BorderSide.none,
      ),
      child: RadioListTile<FertilityGoal>(
        value: value,
        title: Text(title, style: const TextStyle(fontWeight: FontWeight.w600)),
        subtitle: Text(subtitle),
        secondary: Icon(icon,
            color: isSelected ? Theme.of(context).colorScheme.primary : null),
        activeColor: Theme.of(context).colorScheme.primary,
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      ),
    );
  }
}
