import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:superport/data/models/equipment_history_dto.dart';

part 'inventory_history_view_model.freezed.dart';
part 'inventory_history_view_model.g.dart';

/// 재고 이력 관리 화면 전용 ViewModel
/// 백엔드 여러 API를 조합한 최종 표시용 데이터
@freezed
class InventoryHistoryViewModel with _$InventoryHistoryViewModel {
  const InventoryHistoryViewModel._(); // Private constructor for getters
  
  const factory InventoryHistoryViewModel({
    // 기본 식별자
    @JsonKey(name: 'history_id') required int historyId,
    @JsonKey(name: 'equipment_id') required int equipmentId,
    
    // 화면 표시 필드들 (요구사항 기준)
    @JsonKey(name: 'equipment_name') required String equipmentName,     // 장비명 (백엔드 조합)
    @JsonKey(name: 'serial_number') required String serialNumber,      // 시리얼번호
    @JsonKey(name: 'location') required String location,               // 위치 (transaction_type에 따라 다르게)
    @JsonKey(name: 'changed_date') required DateTime changedDate,      // 변동일 (transacted_at)
    @JsonKey(name: 'remark') String? remark,                          // 비고
    
    // 추가 정보
    @JsonKey(name: 'transaction_type') required String transactionType, // I, O, R, D
    @JsonKey(name: 'quantity') required int quantity,
    
    // 원본 데이터 보존 (상세보기 시 필요)
    @JsonKey(includeFromJson: false, includeToJson: false) 
    EquipmentHistoryDto? originalHistory,
  }) = _InventoryHistoryViewModel;
  
  /// Transaction Type에 따른 표시명
  String get transactionTypeDisplay {
    switch (transactionType) {
      case 'I':
        return '입고';
      case 'O':
        return '출고';
      case 'R':
        return '대여';
      case 'D':
        return '폐기';
      default:
        return transactionType;
    }
  }
  
  /// 위치 유형 판단 (고객사/창고)
  bool get isCustomerLocation {
    return transactionType == 'O' || transactionType == 'R';
  }
  
  /// 날짜 포맷팅 (yyyy-MM-dd)
  String get formattedDate {
    return '${changedDate.year}-${changedDate.month.toString().padLeft(2, '0')}-${changedDate.day.toString().padLeft(2, '0')}';
  }
  
  factory InventoryHistoryViewModel.fromJson(Map<String, dynamic> json) =>
      _$InventoryHistoryViewModelFromJson(json);
}

/// 재고 이력 목록 응답
@freezed
class InventoryHistoryListResponse with _$InventoryHistoryListResponse {
  const factory InventoryHistoryListResponse({
    @JsonKey(name: 'data') required List<InventoryHistoryViewModel> items,
    @JsonKey(name: 'total') required int totalCount,
    @JsonKey(name: 'page') required int currentPage,
    @JsonKey(name: 'total_pages') required int totalPages,
    @JsonKey(name: 'page_size') int? pageSize,
  }) = _InventoryHistoryListResponse;

  factory InventoryHistoryListResponse.fromJson(Map<String, dynamic> json) =>
      _$InventoryHistoryListResponseFromJson(json);
}

/// 검색/필터 파라미터
@freezed
class InventoryHistoryQuery with _$InventoryHistoryQuery {
  const factory InventoryHistoryQuery({
    int? page,
    @JsonKey(name: 'page_size') int? pageSize,
    @JsonKey(name: 'search_keyword') String? searchKeyword,
    @JsonKey(name: 'transaction_type') String? transactionType,
    @JsonKey(name: 'equipment_id') int? equipmentId,
    @JsonKey(name: 'warehouse_id') int? warehouseId,
    @JsonKey(name: 'company_id') int? companyId,
    @JsonKey(name: 'date_from') DateTime? dateFrom,
    @JsonKey(name: 'date_to') DateTime? dateTo,
  }) = _InventoryHistoryQuery;

  factory InventoryHistoryQuery.fromJson(Map<String, dynamic> json) =>
      _$InventoryHistoryQueryFromJson(json);
}