import 'package:json_annotation/json_annotation.dart';

part 'auth_login_response.g.dart';

@JsonSerializable()
class AuthLoginUser {
  final String id;
  final String username;
  final String firstName;
  final String lastName;
  final String? userType;
  final String? className;

  AuthLoginUser({
    required this.id,
    required this.username,
    required this.firstName,
    required this.lastName,
    required this.userType,
    required this.className,
  });

  factory AuthLoginUser.fromJson(Map<String, dynamic> json) =>
      _$AuthLoginUserFromJson(json);
  Map<String, dynamic> toJson() => _$AuthLoginUserToJson(this);
}

@JsonSerializable()
class AuthLoginResponse {
  final String token;
  final String tokenId;

  @JsonKey(fromJson: _expiresFromJson)
  final DateTime? expiresAt;

  final AuthLoginUser user;

  AuthLoginResponse({
    required this.token,
    required this.tokenId,
    required this.expiresAt,
    required this.user,
  });

  factory AuthLoginResponse.fromJson(Map<String, dynamic> json) =>
      _$AuthLoginResponseFromJson(json);
  Map<String, dynamic> toJson() => _$AuthLoginResponseToJson(this);

  static DateTime? _expiresFromJson(Object? value) {
    if (value == null) return null;
    if (value is String) return DateTime.tryParse(value);
    return null;
  }
}
