import 'package:flutter/material.dart';

import 'package:provider/provider.dart';
import 'package:pweb/generated/i18n/app_localizations.dart';

import 'package:pweb/providers/upload_history.dart';


class UploadHistorySection extends StatelessWidget {
  const UploadHistorySection({super.key});

  static const double _smallBox = 5;
  static const double _radius = 6;

  @override
  Widget build(BuildContext context) {
    final provider = context.watch<UploadHistoryProvider>();
    final theme = Theme.of(context);
    final l10 = AppLocalizations.of(context)!;
    

    if (provider.isLoading) {
      return const Center(child: CircularProgressIndicator());
    }
    if (provider.error != null) {
      return Text(l10.notificationError(provider.error ?? l10.noErrorInformation));
    }
    final items = provider.data ?? [];

    return Column(
      children: [
        Row(
          children: [
            const Icon(Icons.history),
            const SizedBox(width: _smallBox),
            Text(l10.uploadHistory, style: theme.textTheme.bodyLarge),
          ],
        ),
        DataTable(
          columns: [
            DataColumn(label: Text(l10.fileNameColumn)),
            DataColumn(label: Text(l10.colStatus)),
            DataColumn(label: Text(l10.dateColumn)),
            DataColumn(label: Text(l10.details)),
          ],
          rows: items.map((file) {
            final isError = file.status == "Error";
            final statusColor = isError ? Colors.red : Colors.green;
            return DataRow(
              cells: [
                DataCell(Text(file.name)),
                DataCell(Container(
                  padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                  decoration: BoxDecoration(
                    color: statusColor.withAlpha(20),
                    borderRadius: BorderRadius.circular(_radius),
                  ),
                  child: Text(file.status, style: TextStyle(color: statusColor)),
                )),
                DataCell(Text(file.time)),
                DataCell(TextButton(onPressed: () {}, child: Text(l10.showDetails))),
              ],
            );
          }).toList(),
        ),
      ],
    );
  }
}
