import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:gap/gap.dart';
import 'package:intl/intl.dart';
import '../../ui/theme/app_theme.dart';
import '../../ui/widgets/structure_widgets.dart';
import '../../services/firestore_service.dart';
import '../../models/decision_log.dart';

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

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final logsAsync = ref.watch(decisionLogsProvider);

    return Column(
      children: [
        Padding(
          padding: const EdgeInsets.all(24.0),
          child: Row(
            children: [
              Container(
                width: 40,
                height: 40,
                decoration: const BoxDecoration(
                  color: AppTheme.energyLow,
                  shape: BoxShape.circle,
                ),
                child: const Center(
                  child: Text("🧔", style: TextStyle(fontSize: 24)),
                ),
              ),
              const Gap(12),
              Text("History", style: AppTheme.textTheme.displayMedium),
            ],
          ),
        ),
        Expanded(
          child: logsAsync.when(
            data: (logs) {
              if (logs.isEmpty) {
                return const Center(child: Text("No history yet."));
              }
              return ListView.builder(
                padding: const EdgeInsets.symmetric(horizontal: 24),
                itemCount: logs.length,
                itemBuilder: (context, index) {
                  final log = logs[index];
                  return _HistoryItem(log: log);
                },
              );
            },
            loading: () => const Center(child: CircularProgressIndicator()),
            error: (e, st) => Center(child: Text("Error loading logs: $e")),
          ),
        ),
      ],
    );
  }
}

class _HistoryItem extends StatelessWidget {
  final DecisionLog log;

  const _HistoryItem({required this.log});

  @override
  Widget build(BuildContext context) {
    final dateStr = DateFormat.MMMd().add_jm().format(log.createdAt);

    return Padding(
      padding: const EdgeInsets.only(bottom: 16.0),
      child: AyuelCard(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  dateStr,
                  style: AppTheme.textTheme.bodyMedium?.copyWith(fontSize: 12),
                ),
                if (log.energy.isNotEmpty)
                  Container(
                    padding: const EdgeInsets.symmetric(
                      horizontal: 8,
                      vertical: 2,
                    ),
                    decoration: BoxDecoration(
                      color: AppTheme.surface,
                      borderRadius: BorderRadius.circular(100),
                    ),
                    child: Text(
                      log.energy,
                      style: const TextStyle(fontSize: 10),
                    ),
                  ),
              ],
            ),
            const Gap(8),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Icon(
                  Icons.person,
                  size: 16,
                  color: AppTheme.textSecondary,
                ),
                const Gap(8),
                Expanded(
                  child: Text(
                    "You asked: ${log.transcript}",
                    style: AppTheme.textTheme.bodyMedium,
                  ),
                ),
              ],
            ),
            const Gap(8),
            const Divider(),
            const Gap(8),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Icon(
                  Icons.auto_awesome,
                  size: 16,
                  color: AppTheme.primary,
                ),
                const Gap(8),
                Expanded(
                  child: Text(
                    "suggested: ${log.suggestion}",
                    style: AppTheme.textTheme.bodyLarge,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
