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

part 'warehouse_dto.freezed.dart';
part 'warehouse_dto.g.dart';

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

  const factory WarehouseDto({
    @JsonKey(name: 'id') int? id,
    @JsonKey(name: 'name') required String name,
    @JsonKey(name: 'zipcodes_zipcode') String? zipcodesZipcode,
    @JsonKey(name: 'zipcode_address') String? zipcodeAddress, // 백엔드 응답에 포함된 필드
    @JsonKey(name: 'remark') String? remark,
    @JsonKey(name: 'is_deleted') @Default(false) bool isDeleted,
    @JsonKey(name: 'registered_at') DateTime? registeredAt,
    @JsonKey(name: 'updated_at') DateTime? updatedAt,
    
    // Nested data (optional, populated in GET requests)
    @JsonKey(name: 'zipcode') ZipcodeDto? zipcode,
  }) = _WarehouseDto;

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

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

@freezed
class WarehouseRequestDto with _$WarehouseRequestDto {
  const factory WarehouseRequestDto({
    @JsonKey(name: 'name') required String name,
    @JsonKey(name: 'zipcodes_zipcode') String? zipcodesZipcode,
    @JsonKey(name: 'remark') String? remark,
  }) = _WarehouseRequestDto;

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

@freezed
class WarehouseUpdateRequestDto with _$WarehouseUpdateRequestDto {
  const factory WarehouseUpdateRequestDto({
    @JsonKey(name: 'name') String? name,
    @JsonKey(name: 'zipcodes_zipcode') String? zipcodesZipcode,
    @JsonKey(name: 'remark') String? remark,
  }) = _WarehouseUpdateRequestDto;

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

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

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

// Legacy support classes
@freezed
class WarehouseLocationListDto with _$WarehouseLocationListDto {
  const factory WarehouseLocationListDto({
    @JsonKey(name: 'items') required List<WarehouseDto> items,
    @JsonKey(name: 'total') required int total,
    @JsonKey(name: 'page') required int page,
    @JsonKey(name: 'per_page') required int perPage,
    @JsonKey(name: 'total_pages') required int totalPages,
  }) = _WarehouseLocationListDto;

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

@freezed
class WarehouseCapacityInfo with _$WarehouseCapacityInfo {
  const factory WarehouseCapacityInfo({
    @JsonKey(name: 'total_capacity') int? totalCapacity,
    @JsonKey(name: 'used_capacity') int? usedCapacity,
    @JsonKey(name: 'available_capacity') int? availableCapacity,
  }) = _WarehouseCapacityInfo;

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

@freezed
class WarehouseEquipmentListDto with _$WarehouseEquipmentListDto {
  const factory WarehouseEquipmentListDto({
    @JsonKey(name: 'items') required List<WarehouseEquipmentDto> items,
    @JsonKey(name: 'total') required int total,
  }) = _WarehouseEquipmentListDto;

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

@freezed
class WarehouseEquipmentDto with _$WarehouseEquipmentDto {
  const factory WarehouseEquipmentDto({
    int? id,
    @JsonKey(name: 'equipment_id') int? equipmentId,
    @JsonKey(name: 'warehouse_id') int? warehouseId,
    String? name,
    int? quantity,
  }) = _WarehouseEquipmentDto;

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