import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:superport/core/utils/equipment_status_converter.dart';

part 'equipment_request.freezed.dart';
part 'equipment_request.g.dart';

/// NaiveDate 형식으로 변환하는 JsonConverter (날짜만, 시간 제외)
class NaiveDateConverter implements JsonConverter<DateTime?, String?> {
  const NaiveDateConverter();

  @override
  DateTime? fromJson(String? json) {
    return json != null ? DateTime.parse(json) : null;
  }

  @override
  String? toJson(DateTime? object) {
    // NaiveDate 형식으로 변환: "YYYY-MM-DD"
    return object?.toIso8601String().split('T')[0];
  }
}

/// Decimal 호환성을 위한 JsonConverter  
class DecimalConverter implements JsonConverter<double?, dynamic> {
  const DecimalConverter();

  @override
  double? fromJson(dynamic json) {
    if (json == null) return null;
    if (json is num) return json.toDouble();
    if (json is String) return double.tryParse(json);
    return null;
  }

  @override
  dynamic toJson(double? object) {
    // Rust Decimal과 호환을 위해 문자열로 전송
    return object?.toString();
  }
}

@freezed
class CreateEquipmentRequest with _$CreateEquipmentRequest {
  const factory CreateEquipmentRequest({
    @JsonKey(name: 'equipment_number') required String equipmentNumber,
    String? category1,
    String? category2,
    String? category3,
    required String manufacturer,
    @JsonKey(name: 'model_name') String? modelName,
    @JsonKey(name: 'serial_number') String? serialNumber,
    String? barcode,
    @JsonKey(name: 'purchase_date') @NaiveDateConverter() DateTime? purchaseDate,
    @JsonKey(name: 'purchase_price') @DecimalConverter() double? purchasePrice,
    @JsonKey(name: 'company_id') int? companyId,
    @JsonKey(name: 'warehouse_location_id') int? warehouseLocationId,
    @JsonKey(name: 'last_inspection_date') @NaiveDateConverter() DateTime? lastInspectionDate,
    @JsonKey(name: 'next_inspection_date') @NaiveDateConverter() DateTime? nextInspectionDate,
    String? remark,
  }) = _CreateEquipmentRequest;

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

@freezed
class UpdateEquipmentRequest with _$UpdateEquipmentRequest {
  const factory UpdateEquipmentRequest({
    @JsonKey(includeIfNull: false) String? category1,
    @JsonKey(includeIfNull: false) String? category2,
    @JsonKey(includeIfNull: false) String? category3,
    @JsonKey(includeIfNull: false) String? manufacturer,
    @JsonKey(name: 'model_name', includeIfNull: false) String? modelName,
    @JsonKey(name: 'serial_number', includeIfNull: false) String? serialNumber,
    @JsonKey(includeIfNull: false) String? barcode,
    @JsonKey(name: 'purchase_date', includeIfNull: false) @NaiveDateConverter() DateTime? purchaseDate,
    @JsonKey(name: 'purchase_price', includeIfNull: false) @DecimalConverter() double? purchasePrice,
    @JsonKey(includeIfNull: false) String? status,
    @JsonKey(name: 'company_id', includeIfNull: false) int? companyId,
    @JsonKey(name: 'warehouse_location_id', includeIfNull: false) int? warehouseLocationId,
    @JsonKey(name: 'last_inspection_date', includeIfNull: false) @NaiveDateConverter() DateTime? lastInspectionDate,
    @JsonKey(name: 'next_inspection_date', includeIfNull: false) @NaiveDateConverter() DateTime? nextInspectionDate,
    @JsonKey(includeIfNull: false) String? remark,
  }) = _UpdateEquipmentRequest;

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