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') int? equipmentHistoryId, // Optional in backend
    @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('V') String maintenanceType, // V: 방문, R: 원격
    @JsonKey(name: 'is_deleted') @Default(false) bool isDeleted,
    @JsonKey(name: 'registered_at') required DateTime registeredAt,
    @JsonKey(name: 'updated_at') DateTime? updatedAt,
    
    // 백엔드 추가 필드들 (계산된 값)
    @JsonKey(name: 'equipment_serial') String? equipmentSerial,
    @JsonKey(name: 'equipment_model') String? equipmentModel,
    @JsonKey(name: 'company_id') int? companyId,
    @JsonKey(name: 'company_name') String? companyName,
    @JsonKey(name: 'days_remaining') int? daysRemaining,
    @JsonKey(name: 'is_expired') @Default(false) bool isExpired,
    
    // 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') int? equipmentHistoryId, // Optional in backend
    @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('V') String maintenanceType, // V: 방문, R: 원격
  }) = _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);
}

@freezed
class MaintenanceQueryDto with _$MaintenanceQueryDto {
  const factory MaintenanceQueryDto({
    @JsonKey(name: 'equipment_id') int? equipmentId,
    @JsonKey(name: 'maintenance_type') String? maintenanceType,
    @JsonKey(name: 'is_expired') bool? isExpired,
    @JsonKey(name: 'expiring_days') int? expiringDays,
    @JsonKey(name: 'page') @Default(1) int page,
    @JsonKey(name: 'per_page') @Default(10) int perPage,
    @JsonKey(name: 'include_deleted') @Default(false) bool includeDeleted,
  }) = _MaintenanceQueryDto;

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

// Maintenance Type 헬퍼 (V/R 시스템)
class MaintenanceType {
  static const String visit = 'V';     // 방문 유지보수
  static const String remote = 'R';    // 원격 유지보수
  
  static String getDisplayName(String type) {
    switch (type) {
      case visit:
        return '방문';
      case remote:
        return '원격';
      default:
        return type;
    }
  }
  
  static List<String> get allTypes => [visit, remote];
  
  static List<Map<String, String>> get typeOptions => [
    {'value': visit, 'label': '방문'},
    {'value': remote, 'label': '원격'},
  ];
}