import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:lucide_icons/lucide_icons.dart';
import 'package:core_localization/core_localization.dart';
import 'package:design_system/design_system.dart';

import 'package:staff_home/src/presentation/blocs/home_cubit.dart';
import 'package:staff_home/src/presentation/navigation/home_navigator.dart';
import 'package:staff_home/src/presentation/widgets/home_page/empty_state_widget.dart';
import 'package:staff_home/src/presentation/widgets/home_page/home_header.dart';
import 'package:staff_home/src/presentation/widgets/home_page/pending_payment_card.dart';
import 'package:staff_home/src/presentation/widgets/home_page/placeholder_banner.dart';
import 'package:staff_home/src/presentation/widgets/home_page/quick_action_item.dart';
import 'package:staff_home/src/presentation/widgets/home_page/recommended_shift_card.dart';
import 'package:staff_home/src/presentation/widgets/home_page/section_header.dart';
import 'package:staff_home/src/presentation/widgets/shift_card.dart';
import 'package:staff_home/src/presentation/widgets/worker/auto_match_toggle.dart';
import 'package:staff_home/src/presentation/widgets/worker/benefits_widget.dart';
import 'package:staff_home/src/presentation/widgets/worker/improve_yourself_widget.dart';
import 'package:staff_home/src/presentation/widgets/worker/more_ways_widget.dart';

/// The home page for the staff worker application.
///
/// This page displays the worker's dashboard including today's shifts,
/// tomorrow's shifts, recommended shifts, benefits, and other relevant
/// information. It follows Clean Architecture principles with state
/// managed by [HomeCubit].
/// The home page for the staff worker application.
///
/// This page displays the worker's dashboard including today's shifts,
/// tomorrow's shifts, recommended shifts, benefits, and other relevant
/// information. It follows Clean Architecture principles with state
/// managed by [HomeCubit].
class WorkerHomePage extends StatelessWidget {
  /// Creates a [WorkerHomePage].
  const WorkerHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    final i18n = t.staff.home;
    final bannersI18n = i18n.banners;
    final quickI18n = i18n.quick_actions;
    final sectionsI18n = i18n.sections;
    final emptyI18n = i18n.empty_states;

    return BlocProvider<HomeCubit>(
      create: (context) => Modular.get<HomeCubit>()..loadShifts(),
      child: Scaffold(
        body: SafeArea(
          child: SingleChildScrollView(
            padding: const EdgeInsets.only(bottom: UiConstants.space6),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const HomeHeader(),
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: UiConstants.space4),
                  child: Column(
                    children: [
                      BlocBuilder<HomeCubit, HomeState>(
                        buildWhen: (previous, current) =>
                            previous.isProfileComplete !=
                            current.isProfileComplete,
                        builder: (context, state) {
                          if (state.isProfileComplete) return const SizedBox();
                          return PlaceholderBanner(
                            title: bannersI18n.complete_profile_title,
                            subtitle: bannersI18n.complete_profile_subtitle,
                            bg: UiColors.bgHighlight,
                            accent: UiColors.primary,
                            onTap: () {
                              Modular.to.pushWorkerProfile();
                            },
                          );
                        },
                      ),
                      const SizedBox(height: UiConstants.space6),
                      PlaceholderBanner(
                        title: bannersI18n.availability_title,
                        subtitle: bannersI18n.availability_subtitle,
                        bg: UiColors.accent.withOpacity(0.1),
                        accent: UiColors.accent,
                        onTap: () => Modular.to.pushAvailability(),
                      ),
                      const SizedBox(height: UiConstants.space6),

                      // Auto Match Toggle
                      BlocBuilder<HomeCubit, HomeState>(
                        buildWhen: (previous, current) =>
                            previous.autoMatchEnabled !=
                            current.autoMatchEnabled,
                        builder: (context, state) {
                          return AutoMatchToggle(
                            enabled: state.autoMatchEnabled,
                            onToggle: (val) => BlocProvider.of<HomeCubit>(
                              context,
                              listen: false,
                            ).toggleAutoMatch(val),
                          );
                        },
                      ),
                      const SizedBox(height: UiConstants.space6),

                      // Quick Actions
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Expanded(
                            child: QuickActionItem(
                              icon: LucideIcons.search,
                              label: quickI18n.find_shifts,
                              onTap: () => Modular.to.pushShifts(),
                            ),
                          ),
                          Expanded(
                            child: QuickActionItem(
                              icon: LucideIcons.calendar,
                              label: quickI18n.availability,
                              onTap: () => Modular.to.pushAvailability(),
                            ),
                          ),
                          Expanded(
                            child: QuickActionItem(
                              icon: LucideIcons.messageSquare,
                              label: quickI18n.messages,
                              onTap: () => Modular.to.pushMessages(),
                            ),
                          ),
                          Expanded(
                            child: QuickActionItem(
                              icon: LucideIcons.dollarSign,
                              label: quickI18n.earnings,
                              onTap: () => Modular.to.navigateToPayments(),
                            ),
                          ),
                        ],
                      ),
                      const SizedBox(height: 24),

                      // Today's Shifts
                      BlocBuilder<HomeCubit, HomeState>(
                        builder: (context, state) {
                          final shifts = state.todayShifts;
                          return Column(
                            children: [
                              SectionHeader(
                                title: sectionsI18n.todays_shift,
                                action: shifts.isNotEmpty
                                    ? sectionsI18n.scheduled_count(
                                        count: shifts.length,
                                      )
                                    : null,
                              ),
                              if (state.status == HomeStatus.loading)
                                const Center(
                                  child: SizedBox(
                                    height: 40,
                                    width: 40,
                                    child: CircularProgressIndicator(),
                                  ),
                                )
                              else if (shifts.isEmpty)
                                EmptyStateWidget(
                                  message: emptyI18n.no_shifts_today,
                                  actionLink: emptyI18n.find_shifts_cta,
                                  onAction: () =>
                                      Modular.to.pushShifts(tab: 'find'),
                                )
                              else
                                Column(
                                  children: shifts
                                      .map(
                                        (shift) => ShiftCard(
                                          shift: shift,
                                          compact: true,
                                        ),
                                      )
                                      .toList(),
                                ),
                            ],
                          );
                        },
                      ),
                      const SizedBox(height: 24),

                      // Tomorrow's Shifts
                      BlocBuilder<HomeCubit, HomeState>(
                        builder: (context, state) {
                          final shifts = state.tomorrowShifts;
                          return Column(
                            children: [
                              SectionHeader(title: sectionsI18n.tomorrow),
                              if (shifts.isEmpty)
                                EmptyStateWidget(
                                  message: emptyI18n.no_shifts_tomorrow,
                                )
                              else
                                Column(
                                  children: shifts
                                      .map(
                                        (shift) => ShiftCard(
                                          shift: shift,
                                          compact: true,
                                        ),
                                      )
                                      .toList(),
                                ),
                            ],
                          );
                        },
                      ),
                      const SizedBox(height: 24),

                      // Pending Payment Card
                      const PendingPaymentCard(),
                      const SizedBox(height: 24),

                      // Recommended Shifts
                      SectionHeader(
                        title: sectionsI18n.recommended_for_you,
                        action: sectionsI18n.view_all,
                        onAction: () => Modular.to.pushShifts(tab: 'find'),
                      ),
                      BlocBuilder<HomeCubit, HomeState>(
                        builder: (context, state) {
                          if (state.recommendedShifts.isEmpty) {
                            return EmptyStateWidget(
                              message: emptyI18n.no_recommended_shifts,
                            );
                          }
                          return SizedBox(
                            height: 160,
                            child: ListView.builder(
                              scrollDirection: Axis.horizontal,
                              itemCount: state.recommendedShifts.length,
                              clipBehavior: Clip.none,
                              itemBuilder: (context, index) => Padding(
                                padding: const EdgeInsets.only(right: 12),
                                child: RecommendedShiftCard(
                                  shift: state.recommendedShifts[index],
                                ),
                              ),
                            ),
                          );
                        },
                      ),
                      const SizedBox(height: 24),

                      const BenefitsWidget(),
                      const SizedBox(height: 24),

                      const ImproveYourselfWidget(),
                      const SizedBox(height: 24),

                      const MoreWaysToUseKrowWidget(),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
