// SPDX-FileCopyrightText: 2026 FocusFlow contributors
// SPDX-License-Identifier: AGPL-3.0-only

part of '../task_timeline_card.dart';

/// Private implementation type for `_CompactCardText` in this library.
/// It keeps supporting state and behavior scoped to the owning file so the public API stays focused on the scheduler concepts that callers use directly.
class _CompactCardText extends StatelessWidget {
  /// Creates a `_CompactCardText` instance with the values required by its invariants.
  /// Callers should pass already-normalized scheduler data because validation and defaulting belong at the model or service boundary, not in arbitrary UI code.
  const _CompactCardText({
    required this.card,
    required this.color,
    required this.metrics,
    required this.onStatusPressed,
  });

  /// Stores the `card` value for this object.
  /// This field is part of the explicit state that downstream scheduling, persistence, or UI code reads when composing behavior.
  final TimelineCardModel card;

  /// Stores the `color` value for this object.
  /// This field is part of the explicit state that downstream scheduling, persistence, or UI code reads when composing behavior.
  final Color color;

  /// Stores the `metrics` value for this object.
  /// This field is part of the explicit state that downstream scheduling, persistence, or UI code reads when composing behavior.
  final _CardMetrics metrics;

  /// Stores the `onStatusPressed` value for this object.
  /// This field is part of the explicit state that downstream scheduling, persistence, or UI code reads when composing behavior.
  final VoidCallback? onStatusPressed;

  /// Builds the widget subtree for this component.
  /// The method translates the current widget configuration and state into Flutter UI without mutating scheduler domain data.
  @override
  Widget build(BuildContext context) {
    final metaText = _compactMetaText(card);
    final titleStyle = TextStyle(
      color: card.isCompleted
          ? FocusFlowTokens.completedText
          : FocusFlowTokens.textPrimary,
      fontSize: metrics.titleSize,
      height: 1,
      fontWeight: FontWeight.w800,
      decoration: card.isCompleted ? TextDecoration.lineThrough : null,
      decorationColor: FocusFlowTokens.completedText,
      decorationThickness: math.max(1, 2 * metrics.scale),
    );
    final metaStyle = TextStyle(
      color: card.isCompleted ? FocusFlowTokens.completedText : color,
      fontSize: metrics.metaSize,
      height: 1,
      fontWeight: FontWeight.w700,
    );
    return LayoutBuilder(
      builder: (context, constraints) {
        final line = Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            if (card.visualKind != TaskVisualKind.freeSlot) ...[
              _StatusRing(
                card: card,
                color: color,
                metrics: metrics,
                onPressed: card.canToggleCompletion ? onStatusPressed : null,
              ),
              SizedBox(width: metrics.statusGap),
            ],
            Flexible(
              flex: 2,
              fit: FlexFit.loose,
              child: Text(
                card.title,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: titleStyle,
              ),
            ),
            SizedBox(width: metrics.compactTextGap),
            Flexible(
              flex: 1,
              fit: FlexFit.loose,
              child: Text(
                metaText,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: metaStyle,
              ),
            ),
            SizedBox(
              width: card.showPastTaskPushButton
                  ? metrics.compactPushTrailingReserve
                  : metrics.compactTrailingReserve,
            ),
          ],
        );
        if (!constraints.hasBoundedWidth || !constraints.hasBoundedHeight) {
          return line;
        }
        return FittedBox(
          alignment: Alignment.centerLeft,
          fit: BoxFit.scaleDown,
          child: SizedBox(width: constraints.maxWidth, child: line),
        );
      },
    );
  }

  /// Runs the `_compactMetaText` helper used inside this library.
  /// The helper centralizes a small piece of transformation or validation so higher-level scheduler flow remains easier to read.
  String _compactMetaText(TimelineCardModel card) {
    if (card.visualKind == TaskVisualKind.freeSlot &&
        card.timeText.isNotEmpty) {
      return card.timeText;
    }
    if (card.subtitle.isNotEmpty) {
      return card.subtitle;
    }
    return card.timeText;
  }
}
