import 'package:json_annotation/json_annotation.dart';

part 'classification.g.dart';

@JsonSerializable()
class ApplicationClassification {
  final int id;
  @JsonKey(name: 'application_name')
  final String applicationName;
  @JsonKey(name: 'category_id')
  final String categoryId;
  @JsonKey(name: 'created_at', fromJson: _dateTimeFromJson, toJson: _dateTimeToJson)
  final DateTime createdAt;
  @JsonKey(name: 'updated_at', fromJson: _dateTimeFromJson, toJson: _dateTimeToJson)
  final DateTime updatedAt;

  ApplicationClassification({
    required this.id,
    required this.applicationName,
    required this.categoryId,
    required this.createdAt,
    required this.updatedAt,
  });

  factory ApplicationClassification.fromJson(Map<String, dynamic> json) =>
      _$ApplicationClassificationFromJson(json);

  Map<String, dynamic> toJson() => _$ApplicationClassificationToJson(this);

  static DateTime _dateTimeFromJson(dynamic timestamp) {
    if (timestamp is int) {
      return DateTime.fromMillisecondsSinceEpoch(timestamp);
    } else if (timestamp is String) {
      return DateTime.parse(timestamp);
    }
    throw ArgumentError('Invalid timestamp format: $timestamp');
  }

  static int _dateTimeToJson(DateTime timestamp) {
    return timestamp.millisecondsSinceEpoch;
  }
}

@JsonSerializable()
class UnclassifiedApplication {
  final int id;
  @JsonKey(name: 'application_name')
  final String applicationName;
  @JsonKey(name: 'first_seen', fromJson: _dateTimeFromJson, toJson: _dateTimeToJson)
  final DateTime firstSeen;
  @JsonKey(name: 'last_seen', fromJson: _dateTimeFromJson, toJson: _dateTimeToJson)
  final DateTime lastSeen;
  @JsonKey(name: 'occurrence_count')
  final int occurrenceCount;

  UnclassifiedApplication({
    required this.id,
    required this.applicationName,
    required this.firstSeen,
    required this.lastSeen,
    required this.occurrenceCount,
  });

  factory UnclassifiedApplication.fromJson(Map<String, dynamic> json) =>
      _$UnclassifiedApplicationFromJson(json);

  Map<String, dynamic> toJson() => _$UnclassifiedApplicationToJson(this);

  static DateTime _dateTimeFromJson(dynamic timestamp) {
    if (timestamp is int) {
      return DateTime.fromMillisecondsSinceEpoch(timestamp);
    } else if (timestamp is String) {
      return DateTime.parse(timestamp);
    }
    throw ArgumentError('Invalid timestamp format: $timestamp');
  }

  static int _dateTimeToJson(DateTime timestamp) {
    return timestamp.millisecondsSinceEpoch;
  }
}

@JsonSerializable()
class ClassificationRequest {
  @JsonKey(name: 'application_name')
  final String applicationName;
  @JsonKey(name: 'category_id')
  final String categoryId;

  ClassificationRequest({
    required this.applicationName,
    required this.categoryId,
  });

  factory ClassificationRequest.fromJson(Map<String, dynamic> json) =>
      _$ClassificationRequestFromJson(json);

  Map<String, dynamic> toJson() => _$ClassificationRequestToJson(this);
}
