import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';

import '../../events/application/events_notifier.dart';
import '../application/brackets_notifier.dart';
import 'widgets/bracket_tree_widget.dart';

/// Full-screen view of a single bracket. Hosts the [BracketTreeWidget] in the
/// body and shows the parent event's title in the AppBar subtitle when
/// available.
class BracketDetailScreen extends ConsumerWidget {
  const BracketDetailScreen({super.key, required this.bracketId});

  final String bracketId;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final bracket = ref.watch(bracketByIdProvider(bracketId));
    final theme = Theme.of(context);
    final scheme = theme.colorScheme;

    if (bracket == null) {
      return Scaffold(
        appBar: AppBar(
          title: const Text('Bracket'),
          leading: IconButton(
            icon: const Icon(Icons.arrow_back),
            onPressed: () => context.go('/brackets'),
          ),
        ),
        body: const Center(child: Text('Bracket not found.')),
      );
    }

    final event = ref.watch(eventByIdProvider(bracket.eventId));

    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () => context.go('/brackets'),
        ),
        title: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text(
              bracket.name,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: theme.textTheme.titleMedium?.copyWith(
                fontWeight: FontWeight.w700,
              ),
            ),
            if (event != null)
              Text(
                event.title,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: theme.textTheme.labelSmall?.copyWith(
                  color: scheme.onSurfaceVariant,
                ),
              ),
          ],
        ),
        actions: [
          IconButton(
            icon: const Icon(Icons.share_outlined),
            tooltip: 'Share bracket',
            onPressed: () {
              ScaffoldMessenger.of(context).showSnackBar(
                const SnackBar(
                  content: Text('Sharing brackets is coming soon.'),
                ),
              );
            },
          ),
        ],
      ),
      body: Column(
        children: [
          Expanded(
            child: Center(
              child: BracketTreeWidget(bracket: bracket),
            ),
          ),
          Container(
            width: double.infinity,
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
            decoration: BoxDecoration(
              color: scheme.surfaceContainerHighest,
              border: Border(
                top: BorderSide(color: scheme.outlineVariant),
              ),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Icon(
                  Icons.swap_horiz,
                  size: 18,
                  color: scheme.onSurfaceVariant,
                ),
                const SizedBox(width: 6),
                Text(
                  'Scroll horizontally to see all rounds',
                  style: theme.textTheme.bodySmall?.copyWith(
                    color: scheme.onSurfaceVariant,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
