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

part 'model_dto.freezed.dart';
part 'model_dto.g.dart';

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

  const factory ModelDto({
    int? id,
    required String name,
    @JsonKey(name: 'vendors_id') required int vendorsId, // 백엔드 snake_case로 수정
    @JsonKey(name: 'is_deleted') @Default(false) bool isDeleted,
    @JsonKey(name: 'registered_at') DateTime? registeredAt,
    @JsonKey(name: 'updated_at') DateTime? updatedAt,
    
    // JOIN 필드 - 백엔드에서 제공
    @JsonKey(name: 'vendor_name') String? vendorName,
    
    // Nested vendor data (optional, populated in GET requests)
    VendorDto? vendor,
  }) = _ModelDto;

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

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

// Request DTO for creating/updating models
@freezed
class ModelRequestDto with _$ModelRequestDto {
  const factory ModelRequestDto({
    required String name,
    @JsonKey(name: 'vendors_id') required int vendorsId, // 백엔드 snake_case로 수정
  }) = _ModelRequestDto;

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

// Update Request DTO
@freezed
class ModelUpdateRequestDto with _$ModelUpdateRequestDto {
  const factory ModelUpdateRequestDto({
    String? name,
    @JsonKey(name: 'vendors_id') int? vendorsId, // 백엔드 snake_case로 수정
  }) = _ModelUpdateRequestDto;

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

// Response wrapper for paginated results
@freezed
class ModelListResponse with _$ModelListResponse {
  const factory ModelListResponse({
    required List<ModelDto> items, // 백엔드 'items' 키와 일치
    @JsonKey(name: 'total') required int totalCount,
    @JsonKey(name: 'page') required int currentPage,
    @JsonKey(name: 'total_pages') required int totalPages,
    @JsonKey(name: 'per_page') int? pageSize, // 백엔드 'per_page' 키와 일치
  }) = _ModelListResponse;

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