import 'package:flutter/material.dart';
import '../../../../core/constants/ui_constants.dart';
import '../../../../core/theme/colors.dart';
import '../../domain/entities/price_document.dart';

/// Document card widget displaying price policy or price list document
class DocumentCard extends StatelessWidget {
  final PriceDocument document;
  final VoidCallback onDownload;

  const DocumentCard({
    super.key,
    required this.document,
    required this.onDownload,
  });

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

    return Container(
      decoration: BoxDecoration(
        color: colorScheme.surface,
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: colorScheme.surfaceContainerHighest),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withValues(alpha: 0.05),
            blurRadius: 4,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          borderRadius: BorderRadius.circular(12),
          onTap: onDownload,
          child: Padding(
            padding: const EdgeInsets.all(AppSpacing.md),
            child: LayoutBuilder(
              builder: (context, constraints) {
                // Responsive layout: column on mobile, row on larger screens
                final isNarrow = constraints.maxWidth < 600;

                if (isNarrow) {
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        children: [
                          _buildIcon(colorScheme),
                          const SizedBox(width: AppSpacing.md),
                          Expanded(child: _buildInfo(colorScheme)),
                        ],
                      ),
                      const SizedBox(height: AppSpacing.md),
                      SizedBox(
                        width: double.infinity,
                        child: _buildDownloadButton(colorScheme),
                      ),
                    ],
                  );
                }

                return Row(
                  children: [
                    _buildIcon(colorScheme),
                    const SizedBox(width: AppSpacing.md),
                    Expanded(child: _buildInfo(colorScheme)),
                    const SizedBox(width: AppSpacing.md),
                    _buildDownloadButton(colorScheme),
                  ],
                );
              },
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildIcon(ColorScheme colorScheme) {
    final iconData = document.isPdf ? Icons.picture_as_pdf : Icons.table_chart;
    final iconColor = document.isPdf
        ? Colors.red.shade600
        : Colors.green.shade600;

    return Container(
      width: 50,
      height: 50,
      decoration: BoxDecoration(
        color: colorScheme.surfaceContainerLowest,
        borderRadius: BorderRadius.circular(8),
      ),
      child: Icon(iconData, size: 28, color: iconColor),
    );
  }

  Widget _buildInfo(ColorScheme colorScheme) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          document.title,
          style: TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.w600,
            color: colorScheme.onSurface,
          ),
        ),
        const SizedBox(height: 4),
        Row(
          children: [
            Icon(
              Icons.calendar_today,
              size: 13,
              color: colorScheme.onSurfaceVariant,
            ),
            const SizedBox(width: 4),
            Text(
              document.formattedDateWithPrefix,
              style: TextStyle(fontSize: 13, color: colorScheme.onSurfaceVariant),
            ),
          ],
        ),
        const SizedBox(height: 6),
        Text(
          document.title,
          style: TextStyle(
            fontSize: 14,
            color: colorScheme.onSurfaceVariant,
            height: 1.4,
          ),
          maxLines: 2,
          overflow: TextOverflow.ellipsis,
        ),
      ],
    );
  }

  Widget _buildDownloadButton(ColorScheme colorScheme) {
    final isDownloaded = document.filePath != null;
    final buttonColor = isDownloaded ? AppColors.success : colorScheme.primary;
    final buttonIcon = isDownloaded ? Icons.folder_open : Icons.download;
    final buttonText = isDownloaded ? 'Mở file' : 'Tải về';

    return ElevatedButton.icon(
      onPressed: onDownload,
      style: ElevatedButton.styleFrom(
        backgroundColor: buttonColor,
        foregroundColor: AppColors.white,
        elevation: 0,
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
      ),
      icon: Icon(buttonIcon, size: 18),
      label: Text(
        buttonText,
        style: const TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
      ),
    );
  }
}
