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

import 'package:household_keeper/features/home/presentation/calendar_day_list.dart';
import 'package:household_keeper/features/home/presentation/calendar_providers.dart';
import 'package:household_keeper/features/home/presentation/calendar_strip.dart';
import 'package:household_keeper/features/tasks/presentation/sort_dropdown.dart';
import 'package:household_keeper/l10n/app_localizations.dart';

/// The app's primary screen: a horizontal calendar strip at the top with a
/// day task list below.
///
/// Replaces the former stacked overdue/today/tomorrow daily plan layout.
/// Users navigate by tapping day cards to see that day's tasks.
class HomeScreen extends ConsumerStatefulWidget {
  const HomeScreen({super.key});

  @override
  ConsumerState<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends ConsumerState<HomeScreen> {
  late final CalendarStripController _stripController =
      CalendarStripController();

  /// Whether to show the floating "Heute" button.
  /// True when the user has scrolled away from today's card.
  bool _showTodayButton = false;

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context);

    return Scaffold(
      appBar: AppBar(
        title: Text(l10n.tabHome),
        actions: const [SortDropdown()],
      ),
      body: Stack(
        children: [
          Column(
            children: [
              CalendarStrip(
                controller: _stripController,
                onTodayVisibilityChanged: (visible) {
                  setState(() => _showTodayButton = !visible);
                },
              ),
              const Expanded(child: CalendarDayList()),
            ],
          ),
          if (_showTodayButton)
            Positioned(
              bottom: 16,
              left: 0,
              right: 0,
              child: Center(
                child: FloatingActionButton.extended(
                  onPressed: () {
                    final now = DateTime.now();
                    final today = DateTime(now.year, now.month, now.day);
                    ref
                        .read(selectedDateProvider.notifier)
                        .selectDate(today);
                    _stripController.scrollToToday();
                  },
                  icon: const Icon(Icons.today),
                  label: Text(l10n.calendarTodayButton),
                ),
              ),
            ),
        ],
      ),
    );
  }
}
