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

import '../../domain/policy_check_result.dart';
import 'governance_status_chip.dart';

/// A detail panel that shows the full information for the selected
/// [PolicyCheckResult].
///
/// Includes the title, status, category, summary, full detail text,
/// and last-evaluated date.
class PolicyDetailPanel extends StatelessWidget {
  final PolicyCheckResult check;

  /// Optional callback to navigate to the related operational page
  /// based on the check's category.
  final void Function(String category)? onViewRelatedPage;

  const PolicyDetailPanel({super.key, required this.check, this.onViewRelatedPage});

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

    return KcCard(
      child: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // ── Header ─────────────────────────────────────────────────
            Row(
              children: [
                Expanded(child: Text(check.title, style: theme.textTheme.headlineMedium)),
                const SizedBox(width: KcSpacing.sm),
                GovernanceStatusChip(status: check.status),
              ],
            ),
            const SizedBox(height: KcSpacing.md),

            // ── Metadata ───────────────────────────────────────────────
            _MetadataRow(label: 'Check ID', value: check.id),
            _MetadataRow(label: 'Category', value: check.category),
            _MetadataRow(label: 'Last Evaluated', value: _formatDate(check.lastEvaluated)),
            const SizedBox(height: KcSpacing.md),

            // ── Summary ────────────────────────────────────────────────
            Text('Summary', style: theme.textTheme.titleLarge),
            const SizedBox(height: KcSpacing.sm),
            Text(check.summary, style: theme.textTheme.bodyLarge),
            const SizedBox(height: KcSpacing.md),

            // ── Detail ─────────────────────────────────────────────────
            Text('Detail', style: theme.textTheme.titleLarge),
            const SizedBox(height: KcSpacing.sm),
            Text(check.detail, style: theme.textTheme.bodyMedium),

            // ── Related page link ──────────────────────────────────────
            if (onViewRelatedPage != null) ...[
              const SizedBox(height: KcSpacing.lg),
              GestureDetector(
                onTap: () => onViewRelatedPage!(check.category),
                child: Text(
                  'Go to ${check.category} →',
                  style: theme.textTheme.bodySmall?.copyWith(
                    color: KcColors.denimBlue,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),
            ],
          ],
        ),
      ),
    );
  }

  static String _formatDate(DateTime date) {
    return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
  }
}

class _MetadataRow extends StatelessWidget {
  final String label;
  final String value;

  const _MetadataRow({required this.label, required this.value});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: KcSpacing.xs),
      child: Row(
        children: [
          SizedBox(
            width: 130,
            child: Text(
              label,
              style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                color: KcColors.neutral,
                fontWeight: FontWeight.w600,
              ),
            ),
          ),
          Expanded(child: Text(value, style: Theme.of(context).textTheme.bodyMedium)),
        ],
      ),
    );
  }
}
