import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:em2rp/models/equipment_model.dart';
import 'package:em2rp/utils/colors.dart';

/// Widget pour afficher les dates
class EquipmentDatesSection extends StatelessWidget {
  final EquipmentModel equipment;

  const EquipmentDatesSection({
    super.key,
    required this.equipment,
  });

  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 2,
      child: Padding(
        padding: const EdgeInsets.all(20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                const Icon(Icons.calendar_today, color: AppColors.rouge),
                const SizedBox(width: 8),
                Text(
                  'Dates',
                  style: Theme.of(context).textTheme.titleLarge?.copyWith(
                        fontWeight: FontWeight.bold,
                      ),
                ),
              ],
            ),
            const Divider(height: 24),
            if (equipment.purchaseDate != null)
              _buildInfoRow(
                'Date d\'achat',
                DateFormat('dd/MM/yyyy').format(equipment.purchaseDate!),
              ),
            if (equipment.lastMaintenanceDate != null)
              _buildInfoRow(
                'Dernière maintenance',
                DateFormat('dd/MM/yyyy').format(equipment.lastMaintenanceDate!),
              ),
            if (equipment.nextMaintenanceDate != null)
              _buildInfoRow(
                'Prochaine maintenance',
                DateFormat('dd/MM/yyyy').format(equipment.nextMaintenanceDate!),
                valueColor: equipment.nextMaintenanceDate!.isBefore(DateTime.now())
                    ? Colors.red
                    : null,
              ),
            _buildInfoRow(
              'Créé le',
              DateFormat('dd/MM/yyyy à HH:mm').format(equipment.createdAt),
            ),
            _buildInfoRow(
              'Modifié le',
              DateFormat('dd/MM/yyyy à HH:mm').format(equipment.updatedAt),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildInfoRow(
    String label,
    String value, {
    Color? valueColor,
  }) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8.0),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(
            width: 180,
            child: Text(
              label,
              style: TextStyle(
                fontWeight: FontWeight.w500,
                color: Colors.grey[700],
              ),
            ),
          ),
          Expanded(
            child: Text(
              value,
              style: TextStyle(
                color: valueColor,
                fontWeight: FontWeight.w600,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

