import 'package:freezed_annotation/freezed_annotation.dart';

part 'equipment_dto.freezed.dart';
part 'equipment_dto.g.dart';

@freezed
class EquipmentDto with _$EquipmentDto {
  const EquipmentDto._(); // Private constructor for getters

  const factory EquipmentDto({
    required int id,
    @JsonKey(name: 'companies_id') required int companiesId,
    @JsonKey(name: 'company_name', includeToJson: false) String? companyName, // JOIN 필드 - 응답에서만 제공
    @JsonKey(name: 'models_id') required int modelsId,
    @JsonKey(name: 'model_name', includeToJson: false) String? modelName, // JOIN 필드 - 응답에서만 제공
    @JsonKey(name: 'vendor_name', includeToJson: false) String? vendorName, // JOIN 필드 - 응답에서만 제공
    @JsonKey(name: 'warehouses_id') int? warehousesId,
    @JsonKey(name: 'warehouses_name', includeToJson: false) String? warehousesName, // JOIN 필드 - 응답에서만 제공
    @JsonKey(name: 'serial_number') required String serialNumber,
    String? barcode,
    @JsonKey(name: 'purchased_at') DateTime? purchasedAt,
    @JsonKey(name: 'purchase_price') @Default(0) int purchasePrice,
    @JsonKey(name: 'warranty_number') required String warrantyNumber,
    @JsonKey(name: 'warranty_started_at') required DateTime warrantyStartedAt,
    @JsonKey(name: 'warranty_ended_at') required DateTime warrantyEndedAt,
    String? remark,
    @JsonKey(name: 'is_deleted') @Default(false) bool isDeleted,
    @JsonKey(name: 'registered_at') DateTime? registeredAt,
    @JsonKey(name: 'updated_at') DateTime? updatedAt,
  }) = _EquipmentDto;

  // isActive 계산 속성 (is_deleted의 반대)
  bool get isActive => !isDeleted;

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

@freezed
class EquipmentRequestDto with _$EquipmentRequestDto {
  const factory EquipmentRequestDto({
    @JsonKey(name: 'companies_id') int? companiesId,               // 백엔드: Option<i32>
    @JsonKey(name: 'models_id') int? modelsId,                    // 백엔드: Option<i32>
    @JsonKey(name: 'serial_number') required String serialNumber,
    String? barcode,
    @JsonKey(name: 'purchased_at') required DateTime purchasedAt,  // UTC로 미리 변환해서 전달
    @JsonKey(name: 'purchase_price') @Default(0) int purchasePrice,
    @JsonKey(name: 'warranty_number') required String warrantyNumber,
    @JsonKey(name: 'warranty_started_at') required DateTime warrantyStartedAt,  // UTC로 미리 변환해서 전달
    @JsonKey(name: 'warranty_ended_at') required DateTime warrantyEndedAt,      // UTC로 미리 변환해서 전달
    String? remark,
  }) = _EquipmentRequestDto;

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

@freezed
class EquipmentUpdateRequestDto with _$EquipmentUpdateRequestDto {
  const factory EquipmentUpdateRequestDto({
    @JsonKey(name: 'companies_id') int? companiesId,
    @JsonKey(name: 'models_id') int? modelsId,
    @JsonKey(name: 'serial_number') String? serialNumber,
    String? barcode,
    @JsonKey(name: 'purchased_at') DateTime? purchasedAt,
    @JsonKey(name: 'purchase_price') int? purchasePrice,
    @JsonKey(name: 'warranty_number') String? warrantyNumber,
    @JsonKey(name: 'warranty_started_at') DateTime? warrantyStartedAt,
    @JsonKey(name: 'warranty_ended_at') DateTime? warrantyEndedAt,
    String? remark,
  }) = _EquipmentUpdateRequestDto;

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

@freezed
class EquipmentListResponse with _$EquipmentListResponse {
  const factory EquipmentListResponse({
    @JsonKey(name: 'data') required List<EquipmentDto> 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,
  }) = _EquipmentListResponse;

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