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

part of '../task_timeline_card.dart';

/// Private implementation type for `_CardText` 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 _CardText extends StatelessWidget {
  /// Creates a `_CardText` 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 _CardText({
    required this.card,
    required this.color,
    required this.metrics,
  });

  /// 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;

  /// 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 titleStyle = TextStyle(
      color: card.isCompleted
          ? FocusFlowTokens.completedText
          : FocusFlowTokens.textPrimary,
      fontSize: metrics.titleSize,
      fontWeight: FontWeight.w800,
      decoration: card.isCompleted ? TextDecoration.lineThrough : null,
      decorationColor: FocusFlowTokens.completedText,
      decorationThickness: 2 * metrics.scale,
    );
    return LayoutBuilder(
      builder: (context, constraints) {
        final textBlock = Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              card.title,
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
              style: titleStyle,
            ),
            SizedBox(height: metrics.titleMetaGap),
            Text(
              card.subtitle,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                color: card.isCompleted ? FocusFlowTokens.completedText : color,
                fontSize: metrics.metaSize,
                fontStyle: card.visualKind == TaskVisualKind.freeSlot
                    ? FontStyle.italic
                    : FontStyle.normal,
                fontWeight: card.visualKind == TaskVisualKind.flexible
                    ? FontWeight.w700
                    : FontWeight.w500,
              ),
            ),
            if (card.visualKind == TaskVisualKind.freeSlot) ...[
              SizedBox(height: metrics.freeSlotTimeGap),
              Text(
                card.timeText,
                style: TextStyle(
                  color: color,
                  fontSize: metrics.metaSize,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ],
          ],
        );
        if (!constraints.hasBoundedWidth || !constraints.hasBoundedHeight) {
          return textBlock;
        }
        return FittedBox(
          alignment: Alignment.centerLeft,
          fit: BoxFit.scaleDown,
          child: SizedBox(width: constraints.maxWidth, child: textBlock),
        );
      },
    );
  }
}
