import 'package:flutter/material.dart';

import '../../domain/bracket.dart';
import 'match_card.dart';

/// A single column in the bracket tree: a round label at the top, then a
/// vertical stack of [MatchCard]s positioned according to the bracket
/// geometry computed by [BracketTreeWidget].
///
/// The widget itself does not compute spacing — its parent supplies a
/// per-card vertical offset so all rounds align even when match counts
/// differ between columns.
class RoundColumn extends StatelessWidget {
  const RoundColumn({
    super.key,
    required this.round,
    required this.cardCenters,
    required this.columnWidth,
    required this.cardWidth,
    required this.cardHeight,
    required this.columnHeight,
  });

  final BracketRound round;

  /// Vertical center y-coordinate (in this column's local space) for each
  /// match card. Same length as `round.matches`.
  final List<double> cardCenters;

  final double columnWidth;
  final double cardWidth;
  final double cardHeight;
  final double columnHeight;

  static const double labelHeight = 32;

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

    return SizedBox(
      width: columnWidth,
      height: columnHeight,
      child: Column(
        children: [
          SizedBox(
            height: labelHeight,
            child: Center(
              child: Text(
                round.label,
                style: theme.textTheme.titleSmall?.copyWith(
                  fontWeight: FontWeight.w700,
                  color: theme.colorScheme.primary,
                  letterSpacing: 0.5,
                ),
              ),
            ),
          ),
          Expanded(
            child: Stack(
              clipBehavior: Clip.none,
              children: [
                for (var i = 0; i < round.matches.length; i++)
                  Positioned(
                    left: (columnWidth - cardWidth) / 2,
                    top: cardCenters[i] - cardHeight / 2,
                    width: cardWidth,
                    child: MatchCard(
                      match: round.matches[i],
                      width: cardWidth,
                      height: cardHeight,
                    ),
                  ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
