import 'package:flutter/material.dart';

import '../../app/theme.dart';

/// Barra overlay punteggio: SET n | HOME x - AWAY y.
class ScoreOverlayBar extends StatelessWidget {
  const ScoreOverlayBar({
    super.key,
    required this.homeName,
    required this.awayName,
    required this.homePoints,
    required this.awayPoints,
    required this.currentSet,
    this.homeSets,
    this.awaySets,
    this.compact = false,
    this.lastDelta,
    this.pointsTarget,
  });

  final String homeName;
  final String awayName;
  final int homePoints;
  final int awayPoints;
  final int currentSet;
  final int? homeSets;
  final int? awaySets;
  final bool compact;
  final int? lastDelta;
  final int? pointsTarget;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final bg = Colors.black.withValues(alpha: 0.72);

    final bar = Container(
      padding: EdgeInsets.symmetric(
        horizontal: compact ? 10 : 14,
        vertical: compact ? 6 : 8,
      ),
      decoration: BoxDecoration(
        color: bg,
        borderRadius: BorderRadius.circular(compact ? 8 : 10),
        border: Border.all(color: Colors.white12),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            pointsTarget != null ? 'SET $currentSet → $pointsTarget' : 'SET $currentSet',
            style: theme.textTheme.labelLarge?.copyWith(
              color: AppTheme.accentYellow,
              fontSize: compact ? 11 : 13,
            ),
          ),
          Container(
            width: 1,
            height: compact ? 14 : 18,
            margin: const EdgeInsets.symmetric(horizontal: 10),
            color: Colors.white24,
          ),
          Text(
            homeName.toUpperCase(),
            style: theme.textTheme.labelLarge?.copyWith(
              fontSize: compact ? 11 : 13,
            ),
          ),
          const SizedBox(width: 6),
          Text(
            '$homePoints',
            style: theme.textTheme.titleLarge?.copyWith(
              fontWeight: FontWeight.w900,
              fontSize: compact ? 16 : 20,
            ),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 8),
            child: Text(
              '-',
              style: theme.textTheme.titleMedium?.copyWith(
                color: AppTheme.textSecondary,
              ),
            ),
          ),
          Text(
            '$awayPoints',
            style: theme.textTheme.titleLarge?.copyWith(
              fontWeight: FontWeight.w900,
              fontSize: compact ? 16 : 20,
            ),
          ),
          const SizedBox(width: 6),
          Text(
            awayName.toUpperCase(),
            style: theme.textTheme.labelLarge?.copyWith(
              fontSize: compact ? 11 : 13,
            ),
          ),
          if (homeSets != null && awaySets != null) ...[
            const SizedBox(width: 10),
            Text(
              '($homeSets-$awaySets)',
              style: theme.textTheme.bodyMedium?.copyWith(fontSize: 11),
            ),
          ],
          if (lastDelta != null && lastDelta != 0) ...[
            const SizedBox(width: 8),
            Text(
              lastDelta! > 0 ? '+$lastDelta' : '$lastDelta',
              style: theme.textTheme.labelLarge?.copyWith(
                color: lastDelta! > 0 ? AppTheme.successGreen : AppTheme.primaryRed,
                fontSize: compact ? 11 : 13,
              ),
            ),
          ],
        ],
      ),
    );

    return LayoutBuilder(
      builder: (context, constraints) {
        if (!constraints.hasBoundedWidth) {
          return bar;
        }
        return SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          child: bar,
        );
      },
    );
  }
}
