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

part 'rent_dto.freezed.dart';
part 'rent_dto.g.dart';

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

  const factory RentDto({
    int? id,
    @JsonKey(name: 'started_at') required DateTime startedAt,
    @JsonKey(name: 'ended_at') required DateTime endedAt,
    @JsonKey(name: 'equipment_history_id') int? equipmentHistoryId,
    
    // JOIN fields from backend (계산된 필드들)
    @JsonKey(name: 'equipment_serial') String? equipmentSerial,
    @JsonKey(name: 'equipment_model') String? equipmentModel,
    @JsonKey(name: 'company_name') String? companyName,
    @JsonKey(name: 'days_remaining') int? daysRemaining,
    @JsonKey(name: 'is_active') bool? isActive,
    @JsonKey(name: 'total_days') int? totalDays,
    
    // Related entities (optional, populated in GET requests)
    EquipmentHistoryDto? equipmentHistory,
  }) = _RentDto;

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

@freezed
class RentRequestDto with _$RentRequestDto {
  const factory RentRequestDto({
    @JsonKey(name: 'started_at') required DateTime startedAt,
    @JsonKey(name: 'ended_at') required DateTime endedAt,
    @JsonKey(name: 'equipment_history_id') required int equipmentHistoryId,
  }) = _RentRequestDto;

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

@freezed
class RentUpdateRequestDto with _$RentUpdateRequestDto {
  const factory RentUpdateRequestDto({
    @JsonKey(name: 'started_at') DateTime? startedAt,
    @JsonKey(name: 'ended_at') DateTime? endedAt,
    @JsonKey(name: 'equipment_history_id') int? equipmentHistoryId,
  }) = _RentUpdateRequestDto;

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

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

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