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

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

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

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

    return Scaffold(
      appBar: AppBar(
        title: const Text('Notifications'),
      ),
      body: ListView(
        padding: const EdgeInsets.all(16.0),
        children: [
          SwitchListTile(
            title: const Text('Period Estimate'),
            subtitle: const Text(
                'Get notified when your period is predicted to start soon.'),
            value: userProfile.notifyPeriodEstimate,
            onChanged: (value) async {
              await ref.read(userProfileProvider.notifier).updateProfile(
                  userProfile.copyWith(notifyPeriodEstimate: value));
            },
          ),
          const Divider(),
          SwitchListTile(
            title: const Text('Period Start'),
            subtitle: const Text(
                'Get notified when a period starts (or husband needs to know).'),
            value: userProfile.notifyPeriodStart,
            onChanged: (value) async {
              await ref.read(userProfileProvider.notifier).updateProfile(
                  userProfile.copyWith(notifyPeriodStart: value));
            },
          ),
          const Divider(),
          SwitchListTile(
            title: const Text('Low Supply Alert'),
            subtitle:
                const Text('Get notified when pad inventory is running low.'),
            value: userProfile.notifyLowSupply,
            onChanged: (value) async {
              await ref
                  .read(userProfileProvider.notifier)
                  .updateProfile(userProfile.copyWith(notifyLowSupply: value));
            },
          ),
          if (userProfile.isPadTrackingEnabled) ...[
            const Divider(),
            Padding(
              padding:
                  const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
              child: Text(
                'Pad Change Reminders',
                style: Theme.of(context).textTheme.titleMedium?.copyWith(
                      color: Theme.of(context).colorScheme.primary,
                      fontWeight: FontWeight.bold,
                    ),
              ),
            ),
            CheckboxListTile(
              title: const Text('2 Hours Before'),
              value: userProfile.notifyPad2Hours,
              onChanged: (value) async {
                if (value != null) {
                  await ref.read(userProfileProvider.notifier).updateProfile(
                      userProfile.copyWith(notifyPad2Hours: value));
                }
              },
            ),
            CheckboxListTile(
              title: const Text('1 Hour Before'),
              value: userProfile.notifyPad1Hour,
              onChanged: (value) async {
                if (value != null) {
                  await ref.read(userProfileProvider.notifier).updateProfile(
                      userProfile.copyWith(notifyPad1Hour: value));
                }
              },
            ),
            CheckboxListTile(
              title: const Text('30 Minutes Before'),
              value: userProfile.notifyPad30Mins,
              onChanged: (value) async {
                if (value != null) {
                  await ref.read(userProfileProvider.notifier).updateProfile(
                      userProfile.copyWith(notifyPad30Mins: value));
                }
              },
            ),
            CheckboxListTile(
              title: const Text('Change Now (Time\'s Up)'),
              subtitle:
                  const Text('Get notified when it\'s recommended to change.'),
              value: userProfile.notifyPadNow,
              onChanged: (value) async {
                if (value != null) {
                  await ref
                      .read(userProfileProvider.notifier)
                      .updateProfile(userProfile.copyWith(notifyPadNow: value));
                }
              },
            ),
          ],
        ],
      ),
    );
  }
}
