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

import '../../core/theme/app_theme.dart';
import 'game_repository.dart';
import 'tier.dart';

/// لیست میزها (tierها) با ورودی/جایزه؛ انتخاب → ورود به میز.
class TierListScreen extends StatefulWidget {
  final GameRepository repo;
  const TierListScreen({super.key, required this.repo});

  @override
  State<TierListScreen> createState() => _TierListScreenState();
}

class _TierListScreenState extends State<TierListScreen> {
  late Future<List<TableTier>> _future;

  @override
  void initState() {
    super.initState();
    _future = widget.repo.getTiers();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('انتخاب میز')),
      body: FutureBuilder<List<TableTier>>(
        future: _future,
        builder: (context, snap) {
          if (snap.connectionState != ConnectionState.done) {
            return const Center(child: CircularProgressIndicator());
          }
          if (snap.hasError || snap.data == null) {
            return Center(
              child: Column(mainAxisSize: MainAxisSize.min, children: [
                const Text('خطا در بارگذاری میزها'),
                TextButton(
                  onPressed: () =>
                      setState(() => _future = widget.repo.getTiers()),
                  child: const Text('تلاش مجدد'),
                ),
              ]),
            );
          }
          final tiers = snap.data!;
          return ListView.separated(
            padding: const EdgeInsets.all(16),
            itemCount: tiers.length,
            separatorBuilder: (_, __) => const SizedBox(height: 12),
            itemBuilder: (_, i) => _TierCard(tier: tiers[i]),
          );
        },
      ),
    );
  }
}

class _TierCard extends StatelessWidget {
  final TableTier tier;
  const _TierCard({required this.tier});

  @override
  Widget build(BuildContext context) {
    return InkWell(
      borderRadius: BorderRadius.circular(14),
      onTap: () => context.push('/game/${tier.id}?prize=${tier.prize}'),
      child: Container(
        padding: const EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: AppColors.panel,
          borderRadius: BorderRadius.circular(14),
          border: Border.all(color: AppColors.goldDark),
        ),
        child: Row(
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(tier.title,
                    style: const TextStyle(
                        color: AppColors.gold,
                        fontSize: 18,
                        fontWeight: FontWeight.bold)),
                const SizedBox(height: 4),
                Text('${tier.hands} دست',
                    style: const TextStyle(color: Colors.white60)),
              ],
            ),
            const Spacer(),
            Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                _row(Icons.login, 'ورودی', tier.entry),
                _row(Icons.emoji_events, 'جایزه', tier.prize),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _row(IconData icon, String label, int value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 2),
      child: Row(mainAxisSize: MainAxisSize.min, children: [
        Text('$label: $value',
            style: const TextStyle(color: AppColors.text, fontSize: 13)),
        const SizedBox(width: 4),
        Icon(icon, size: 16, color: AppColors.gold),
      ]),
    );
  }
}
