import 'package:pshared/api/responses/login_pending.dart';
import 'package:pshared/api/responses/token.dart';
import 'package:pshared/data/mapper/account/account.dart';
import 'package:pshared/models/account/account.dart';
import 'package:pshared/models/session_identifier.dart';


class PendingLogin {
  final Account account;
  final TokenData pendingToken;
  final String destination;
  final int ttlSeconds;
  final SessionIdentifier session;
  final int? cooldownSeconds;
  final DateTime? cooldownUntil;

  const PendingLogin({
    required this.account,
    required this.pendingToken,
    required this.destination,
    required this.ttlSeconds,
    required this.session,
    this.cooldownSeconds,
    this.cooldownUntil,
  });

  factory PendingLogin.fromResponse(
    PendingLoginResponse response, {
    required SessionIdentifier session,
  }) => PendingLogin(
    account: response.account.account.toDomain(),
    pendingToken: response.pendingToken,
    destination: response.destination,
    ttlSeconds: response.ttlSeconds,
    session: session,
  );

  PendingLogin copyWith({
    Account? account,
    TokenData? pendingToken,
    String? destination,
    int? ttlSeconds,
    SessionIdentifier? session,
    int? cooldownSeconds,
    DateTime? cooldownUntil,
    bool clearCooldown = false,
  }) {
    return PendingLogin(
      account: account ?? this.account,
      pendingToken: pendingToken ?? this.pendingToken,
      destination: destination ?? this.destination,
      ttlSeconds: ttlSeconds ?? this.ttlSeconds,
      session: session ?? this.session,
      cooldownSeconds: clearCooldown ? null : cooldownSeconds ?? this.cooldownSeconds,
      cooldownUntil: clearCooldown ? null : cooldownUntil ?? this.cooldownUntil,
    );
  }

  int get cooldownRemainingSeconds {
    final remaining = cooldownUntil?.difference(DateTime.now()).inSeconds ?? 0;
    return remaining < 0 ? 0 : remaining;
  }
}
