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

import 'package:staff_home/src/presentation/blocs/home_cubit.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/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';

/// 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 t = Translations.of(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>.value(
      value: Modular.get<HomeCubit>()..loadShifts(),
      child: Scaffold(
        body: SafeArea(
          child: SingleChildScrollView(
            padding: const EdgeInsets.only(bottom: UiConstants.space6),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                BlocBuilder<HomeCubit, HomeState>(
                  buildWhen: (previous, current) => previous.staffName != current.staffName,
                  builder: (context, state) {
                    return HomeHeader(userName: state.staffName);
                  },
                ),
                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.toProfile();
                            },
                          );
                        },
                      ),

                      const SizedBox(height: UiConstants.space6),

                      // Quick Actions
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Expanded(
                            child: QuickActionItem(
                              icon: UiIcons.search,
                              label: quickI18n.find_shifts,
                              onTap: () => Modular.to.toShifts(),
                            ),
                          ),
                          Expanded(
                            child: QuickActionItem(
                              icon: UiIcons.calendar,
                              label: quickI18n.availability,
                              onTap: () => Modular.to.toAvailability(),
                            ),
                          ),
                          Expanded(
                            child: QuickActionItem(
                              icon: UiIcons.dollar,
                              label: quickI18n.earnings,
                              onTap: () => Modular.to.toPayments(),
                            ),
                          ),
                        ],
                      ),
                      const SizedBox(height: UiConstants.space6),

                      // 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: UiConstants.space10,
                                    width: UiConstants.space10,
                                    child: CircularProgressIndicator(
                                      color: UiColors.primary,
                                    ),
                                  ),
                                )
                              else if (shifts.isEmpty)
                                EmptyStateWidget(
                                  message: emptyI18n.no_shifts_today,
                                  actionLink: emptyI18n.find_shifts_cta,
                                  onAction: () => Modular.to.toShifts(initialTab: 'find'),
                                )
                              else
                                Column(
                                  children: shifts
                                      .map(
                                        (shift) => ShiftCard(
                                          shift: shift,
                                          compact: true,
                                        ),
                                      )
                                      .toList(),
                                ),
                            ],
                          );
                        },
                      ),
                      const SizedBox(height: UiConstants.space6),

                      // 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: UiConstants.space6),

                      // Recommended Shifts
                      SectionHeader(
                        title: sectionsI18n.recommended_for_you,
                      ),
                      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: UiConstants.space3),
                                child: RecommendedShiftCard(
                                  shift: state.recommendedShifts[index],
                                ),
                              ),
                            ),
                          );
                        },
                      ),
                      const SizedBox(height: UiConstants.space6),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
