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

import 'responsive_section.dart';

/// 페이지 상단 타이틀/설명/액션을 일관되게 출력하는 헤더.
class PageHeader extends StatelessWidget {
  const PageHeader({
    super.key,
    required this.title,
    this.subtitle,
    this.leading,
    this.actions,
    this.trailing,
  });

  final String title;
  final String? subtitle;
  final Widget? leading;
  final List<Widget>? actions;
  final Widget? trailing;

  @override
  Widget build(BuildContext context) {
    final theme = ShadTheme.of(context);

    final actionWidgets = actions ?? const <Widget>[];

    final trailingSection = (actionWidgets.isEmpty && trailing == null)
        ? const SizedBox.shrink()
        : Align(
            alignment: Alignment.centerRight,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              mainAxisSize: MainAxisSize.min,
              children: [
                if (actionWidgets.isNotEmpty)
                  Wrap(
                    spacing: 12,
                    runSpacing: 12,
                    alignment: WrapAlignment.end,
                    children: actionWidgets,
                  ),
                if (actionWidgets.isNotEmpty && trailing != null)
                  const SizedBox(height: 12),
                if (trailing != null) trailing!,
              ],
            ),
          );

    return ResponsiveStackedRow(
      breakpoint: 640,
      gap: 12,
      leading: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          if (leading != null) ...[leading!, const SizedBox(width: 16)],
          Flexible(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(title, style: theme.textTheme.h2),
                if (subtitle != null) ...[
                  const SizedBox(height: 6),
                  Text(subtitle!, style: theme.textTheme.muted),
                ],
              ],
            ),
          ),
        ],
      ),
      trailing: trailingSection,
    );
  }
}
