import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:superport/utils/constants.dart';
import 'package:superport/core/services/lookups_service.dart';
import 'package:superport/screens/common/theme_shadcn.dart';

// 장비 상태에 따라 칩(Chip) 위젯을 반환하는 함수형 위젯
class EquipmentStatusChip extends StatelessWidget {
  final String status;

  const EquipmentStatusChip({super.key, required this.status});

  @override
  Widget build(BuildContext context) {
    // 캐시된 상태 정보 조회 시도
    String statusText = status;
    Color backgroundColor = ShadcnTheme.secondary;

    try {
      final lookupsService = GetIt.instance<LookupsService>();
      final statusResult = lookupsService.getEquipmentStatusById(status);
      
      if (statusResult.isRight()) {
        statusResult.fold(
          (failure) => null,
          (statusItem) {
            if (statusItem != null) {
              statusText = statusItem.name;
            }
          },
        );
      }
    } catch (e) {
      // LookupsService가 등록되지 않았거나 사용할 수 없는 경우 폴백 로직 사용
    }

    // 상태별 색상 지정 (하드코딩된 매핑을 폴백으로 유지)
    switch (status) {
      case EquipmentStatus.in_:
      case 'in':
        backgroundColor = ShadcnTheme.equipmentIn;
        if (statusText == status) statusText = '입고';
        break;
      case EquipmentStatus.out:
      case 'out':
        backgroundColor = ShadcnTheme.equipmentOut;
        if (statusText == status) statusText = '출고';
        break;
      case EquipmentStatus.rent:
      case 'rent':
        backgroundColor = ShadcnTheme.equipmentRent;
        if (statusText == status) statusText = '대여';
        break;
      case EquipmentStatus.repair:
      case 'repair':
        backgroundColor = ShadcnTheme.equipmentRepair;
        if (statusText == status) statusText = '수리중';
        break;
      case EquipmentStatus.damaged:
      case 'damaged':
        backgroundColor = ShadcnTheme.error;
        if (statusText == status) statusText = '손상';
        break;
      case EquipmentStatus.lost:
      case 'lost':
        backgroundColor = ShadcnTheme.purple;
        if (statusText == status) statusText = '분실';
        break;
      case EquipmentStatus.disposed:
      case 'disposed':
        backgroundColor = ShadcnTheme.equipmentDisposal;
        if (statusText == status) statusText = '폐기';
        break;
      case EquipmentStatus.etc:
      case 'etc':
        backgroundColor = ShadcnTheme.secondary;
        if (statusText == status) statusText = '기타';
        break;
      default:
        backgroundColor = ShadcnTheme.equipmentUnknown;
        if (statusText == status) statusText = '알 수 없음';
    }

    // 칩 위젯 반환
    return Chip(
      label: Text(
        statusText,
        style: ShadcnTheme.labelSmall.copyWith(color: ShadcnTheme.primaryForeground),
      ),
      backgroundColor: backgroundColor,
      visualDensity: VisualDensity.compact,
      padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 0),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(ShadcnTheme.radiusFull),
        side: BorderSide(color: backgroundColor.withValues(alpha: 0.2)),
      ),
    );
  }
}
