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

part 'maintenance_dto.freezed.dart';
part 'maintenance_dto.g.dart';

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

  const factory MaintenanceDto({
    @JsonKey(name: 'Id') int? id,
    @JsonKey(name: 'equipment_history_Id') required int equipmentHistoryId,
    @JsonKey(name: 'started_at') required DateTime startedAt,
    @JsonKey(name: 'ended_at') required DateTime endedAt,
    @JsonKey(name: 'period_month') @Default(1) int periodMonth,
    @JsonKey(name: 'maintenance_type') @Default('O') String maintenanceType,
    @JsonKey(name: 'is_deleted') @Default(false) bool isDeleted,
    @JsonKey(name: 'registered_at') required DateTime registeredAt,
    @JsonKey(name: 'updated_at') DateTime? updatedAt,
    
    // Related entities (optional, populated in GET requests)
    EquipmentHistoryDto? equipmentHistory,
  }) = _MaintenanceDto;

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

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

@freezed
class MaintenanceRequestDto with _$MaintenanceRequestDto {
  const factory MaintenanceRequestDto({
    @JsonKey(name: 'equipment_history_Id') required int equipmentHistoryId,
    @JsonKey(name: 'started_at') required DateTime startedAt,
    @JsonKey(name: 'ended_at') required DateTime endedAt,
    @JsonKey(name: 'period_month') @Default(1) int periodMonth,
    @JsonKey(name: 'maintenance_type') @Default('O') String maintenanceType,
  }) = _MaintenanceRequestDto;

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

@freezed
class MaintenanceUpdateRequestDto with _$MaintenanceUpdateRequestDto {
  const factory MaintenanceUpdateRequestDto({
    @JsonKey(name: 'started_at') DateTime? startedAt,
    @JsonKey(name: 'ended_at') DateTime? endedAt,
    @JsonKey(name: 'period_month') int? periodMonth,
    @JsonKey(name: 'maintenance_type') String? maintenanceType,
  }) = _MaintenanceUpdateRequestDto;

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

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

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

// Maintenance Type 헬퍼
class MaintenanceType {
  static const String onsite = 'O';
  static const String remote = 'R';
  
  static String getDisplayName(String type) {
    switch (type) {
      case onsite:
        return '방문';
      case remote:
        return '원격';
      default:
        return type;
    }
  }
  
  static List<String> get allTypes => [onsite, remote];
}