import 'package:flutter/material.dart';
import 'package:superport/models/equipment_unified_model.dart';
import 'package:superport/screens/equipment/widgets/equipment_summary_row.dart';

// 다중 선택 장비 요약 카드
class EquipmentMultiSummaryCard extends StatelessWidget {
  final List<Map<String, dynamic>> selectedEquipments;
  const EquipmentMultiSummaryCard({
    super.key,
    required this.selectedEquipments,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: const EdgeInsets.symmetric(vertical: 8.0),
          child: Text(
            '선택된 장비 목록 (${selectedEquipments.length}개)',
            style: Theme.of(
              context,
            ).textTheme.titleMedium?.copyWith(fontWeight: FontWeight.bold),
          ),
        ),
        ...selectedEquipments.map((equipmentData) {
          final equipment = equipmentData['equipment'] as Equipment;
          return EquipmentSingleSummaryCard(equipment: equipment);
        }),
      ],
    );
  }
}

// 단일 장비 요약 카드
class EquipmentSingleSummaryCard extends StatelessWidget {
  final Equipment equipment;
  const EquipmentSingleSummaryCard({super.key, required this.equipment});

  // 날짜 포맷 유틸리티
  String _formatDate(DateTime? date) {
    if (date == null) return '정보 없음';
    return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
  }

  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 3,
      margin: const EdgeInsets.only(bottom: 12),
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(
                  child: Row(
                    children: [
                      Icon(
                        Icons.inventory,
                        color: Theme.of(context).primaryColor,
                      ),
                      const SizedBox(width: 8),
                      Expanded(
                        child: Text(
                          equipment.name.isNotEmpty ? equipment.name : '이름 없음',
                          style: Theme.of(
                            context,
                          ).textTheme.titleMedium?.copyWith(
                            fontWeight: FontWeight.bold,
                            color: Theme.of(context).primaryColor,
                          ),
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  padding: const EdgeInsets.symmetric(
                    horizontal: 8,
                    vertical: 4,
                  ),
                  decoration: BoxDecoration(
                    color: Colors.blue.shade100,
                    borderRadius: BorderRadius.circular(12),
                    border: Border.all(color: Colors.blue.shade300),
                  ),
                  child: Text(
                    '수량: 1',
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 12,
                      color: Colors.blue.shade800,
                    ),
                  ),
                ),
              ],
            ),
            const Divider(thickness: 1.5),
            EquipmentSummaryRow(
              label: '제조사',
              value:
                  equipment.manufacturer.isNotEmpty
                      ? equipment.manufacturer
                      : '정보 없음',
            ),
            EquipmentSummaryRow(
              label: '모델명',
              value:
                  equipment.modelName.isNotEmpty
                      ? equipment.modelName
                      : '정보 없음',
            ),
            EquipmentSummaryRow(
              label: '시리얼 번호',
              value:
                  (equipment.serialNumber != null &&
                          equipment.serialNumber!.isNotEmpty)
                      ? equipment.serialNumber!
                      : '정보 없음',
            ),
            EquipmentSummaryRow(
              label: '출고 수량',
              value: '1',
            ),
            EquipmentSummaryRow(
              label: '입고일',
              value: _formatDate(equipment.inDate),
            ),
            // 워런티 라이센스 필드는 백엔드에서 제거됨
            EquipmentSummaryRow(
              label: '워런티 시작일',
              value: _formatDate(equipment.warrantyStartDate),
            ),
            EquipmentSummaryRow(
              label: '워런티 종료일',
              value: _formatDate(equipment.warrantyEndDate),
            ),
          ],
        ),
      ),
    );
  }
}
