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 '../blocs/client_home_bloc.dart';
import '../blocs/client_home_event.dart';
import '../blocs/client_home_state.dart';
import 'header_icon_button.dart';

/// The header section of the client home page.
///
/// Displays the user's business name, avatar, and action buttons
/// (edit mode, notifications, settings).
class ClientHomeHeader extends StatelessWidget {

  /// Creates a [ClientHomeHeader].
  const ClientHomeHeader({
    required this.i18n,
    super.key,
  });
  /// The internationalization object for localized strings.
  final dynamic i18n;

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<ClientHomeBloc, ClientHomeState>(
      builder: (BuildContext context, ClientHomeState state) {
        final String businessName = state.businessName;
        final String? photoUrl = state.photoUrl;
        final String avatarLetter = businessName.trim().isNotEmpty
            ? businessName.trim()[0].toUpperCase()
            : 'C';

        return Padding(
          padding: const EdgeInsets.fromLTRB(
            UiConstants.space4,
            UiConstants.space4,
            UiConstants.space4,
            UiConstants.space3,
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Row(
                children: <Widget>[
                  Container(
                    width: 40,
                    height: 40,
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      border: Border.all(
                        color: UiColors.primary.withValues(alpha: 0.2),
                        width: 2,
                      ),
                    ),
                    child: CircleAvatar(
                      backgroundColor: UiColors.primary.withValues(alpha: 0.1),
                      backgroundImage:
                          photoUrl != null && photoUrl.isNotEmpty
                              ? NetworkImage(photoUrl)
                              : null,
                      child: photoUrl != null && photoUrl.isNotEmpty
                          ? null
                          : Text(
                              avatarLetter,
                              style: UiTypography.body2b.copyWith(
                                color: UiColors.primary,
                              ),
                            ),
                    ),
                  ),
                  const SizedBox(width: UiConstants.space3),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text(
                        i18n.dashboard.welcome_back,
                        style: UiTypography.footnote2r.textSecondary,
                      ),
                      Text(businessName, style: UiTypography.body1b),
                    ],
                  ),
                ],
              ),
              Row(
                spacing: UiConstants.space2,
                children: <Widget>[
                  HeaderIconButton(
                    icon: UiIcons.edit,
                    isActive: state.isEditMode,
                    onTap: () => BlocProvider.of<ClientHomeBloc>(
                      context,
                    ).add(ClientHomeEditModeToggled()),
                  ),
                  HeaderIconButton(
                    icon: UiIcons.settings,
                    onTap: () => Modular.to.toClientSettings(),
                  ),
                ],
              ),
            ],
          ),
        );
      },
    );
  }
}
