import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:slrpg_app/l10n/app_localizations.dart';
import '../../../../core/theme/app_theme.dart';
import '../../../gamification/data/repositories/quest_repository.dart';
import '../../../../shared/data/local/app_database.dart';

class QuestBoardWidget extends ConsumerWidget {
  const QuestBoardWidget({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final questRepo = ref.watch(questRepositoryProvider);
    final l10n = AppLocalizations.of(context)!;

    return StreamBuilder<List<QuestCollection>>(
      stream: questRepo.watchQuests(),
      builder: (context, snapshot) {
        final allQuests = snapshot.data ?? [];
        final activeQuests = allQuests
            .where(
                (q) => !q.isClaimed && (q.type == 'daily' || q.type == 'story'))
            .take(3)
            .toList();

        if (activeQuests.isEmpty) return const SizedBox.shrink();

        return Container(
          margin: const EdgeInsets.symmetric(horizontal: 24, vertical: 8),
          padding: const EdgeInsets.all(16),
          decoration: BoxDecoration(
            color: AppTheme.surfaceColor,
            borderRadius: BorderRadius.circular(12),
            border: Border.all(color: Colors.white10),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    l10n.questDailyBounties,
                    style: Theme.of(context).textTheme.labelSmall?.copyWith(
                        color: AppTheme.secondaryColor,
                        letterSpacing: 1.5,
                        fontWeight: FontWeight.bold),
                  ),
                  GestureDetector(
                    onTap: () => context.go('/quests'),
                    child: Text(l10n.questViewAll,
                        style:
                            const TextStyle(fontSize: 10, color: Colors.grey)),
                  ),
                ],
              ),
              const SizedBox(height: 12),
              ...activeQuests.map((q) => _MiniQuestRow(quest: q)).toList(),
            ],
          ),
        );
      },
    );
  }
}

class _MiniQuestRow extends StatelessWidget {
  final QuestCollection quest;
  const _MiniQuestRow({required this.quest});

  @override
  Widget build(BuildContext context) {
    final progress = (quest.currentValue / quest.targetValue).clamp(0.0, 1.0);
    final isComplete = quest.isCompleted;

    return Padding(
      padding: const EdgeInsets.only(bottom: 8),
      child: Row(
        children: [
          Icon(
            isComplete ? Icons.check_circle : Icons.circle_outlined,
            size: 16,
            color: isComplete ? AppTheme.successColor : Colors.grey,
          ),
          const SizedBox(width: 8),
          Expanded(
            child: Text(
              quest.title,
              style: TextStyle(
                fontSize: 12,
                color: isComplete ? Colors.grey : Colors.white,
                decoration: isComplete ? TextDecoration.lineThrough : null,
              ),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
          ),
          if (!isComplete)
            SizedBox(
              width: 40,
              height: 4,
              child: LinearProgressIndicator(
                value: progress,
                backgroundColor: Colors.white10,
                color: AppTheme.secondaryColor,
              ),
            ),
        ],
      ),
    );
  }
}
