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

import '../../domain/suggestion.dart';

/// Card-style row showing one of the current user's past suggestions.
///
/// Truncates the body to three lines, prints a friendly relative-ish date,
/// and renders a status chip whose color matches the suggestion lifecycle.
class SuggestionListTile extends StatelessWidget {
  const SuggestionListTile({super.key, required this.suggestion});

  final Suggestion suggestion;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final colors = theme.colorScheme;
    final dateLabel =
        DateFormat.yMMMd().add_jm().format(suggestion.submittedAt);

    return Card(
      margin: const EdgeInsets.symmetric(vertical: 6),
      child: Padding(
        padding: const EdgeInsets.all(14),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Expanded(
                  child: Text(
                    suggestion.text,
                    maxLines: 3,
                    overflow: TextOverflow.ellipsis,
                    style: theme.textTheme.bodyLarge,
                  ),
                ),
                const SizedBox(width: 8),
                _StatusChip(status: suggestion.status),
              ],
            ),
            const SizedBox(height: 10),
            Row(
              children: <Widget>[
                Icon(
                  Icons.schedule,
                  size: 14,
                  color: colors.onSurfaceVariant,
                ),
                const SizedBox(width: 4),
                Text(
                  dateLabel,
                  style: theme.textTheme.bodySmall?.copyWith(
                    color: colors.onSurfaceVariant,
                  ),
                ),
                if (suggestion.isAnonymous) ...<Widget>[
                  const SizedBox(width: 12),
                  Icon(
                    Icons.visibility_off_outlined,
                    size: 14,
                    color: colors.onSurfaceVariant,
                  ),
                  const SizedBox(width: 4),
                  Text(
                    'Anonymous',
                    style: theme.textTheme.bodySmall?.copyWith(
                      color: colors.onSurfaceVariant,
                    ),
                  ),
                ],
              ],
            ),
          ],
        ),
      ),
    );
  }
}

class _StatusChip extends StatelessWidget {
  const _StatusChip({required this.status});

  final SuggestionStatus status;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final colors = theme.colorScheme;

    final (Color background, Color foreground, String label) = switch (status) {
      SuggestionStatus.pending => (
          colors.surfaceContainerHighest,
          colors.onSurfaceVariant,
          'Pending',
        ),
      SuggestionStatus.reviewed => (
          Colors.amber.withValues(alpha: 0.18),
          Colors.amber.shade300,
          'Reviewed',
        ),
      SuggestionStatus.implemented => (
          Colors.green.withValues(alpha: 0.18),
          Colors.green.shade300,
          'Implemented',
        ),
    };

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
      decoration: BoxDecoration(
        color: background,
        borderRadius: BorderRadius.circular(12),
      ),
      child: Text(
        label,
        style: theme.textTheme.labelSmall?.copyWith(
          color: foreground,
          fontWeight: FontWeight.w600,
        ),
      ),
    );
  }
}
