import 'package:p_tits_pas/models/am_registration_data.dart';
import 'package:p_tits_pas/models/reprise_dossier.dart';
import 'package:p_tits_pas/models/user_registration_data.dart';
import 'package:p_tits_pas/services/api/api_config.dart';
import 'package:p_tits_pas/utils/reprise_mapper.dart';

/// Contexte reprise après refus (token e-mail ou identify). Ticket #112.
class RepriseSession {
  RepriseSession._();

  static String? _token;
  static String? _role;
  static String? _photoUrl;
  static String? _photoUrlForApi;

  static bool get isActive =>
      _token != null && _token!.trim().isNotEmpty;

  static String? get token => _token;

  static bool get isParent => _role == 'parent';

  static bool get isAm => _role == 'assistante_maternelle';

  /// URL absolue pour l'affichage.
  static String? get photoUrl => _photoUrl;

  /// Chemin relatif API (`/uploads/…`) pour PATCH sans re-upload.
  static String? get photoUrlForApi => _photoUrlForApi;

  static void start({
    required String token,
    required RepriseDossier dossier,
  }) {
    _token = token.trim();
    _role = dossier.role;
    final raw = dossier.photoUrl?.trim();
    _photoUrlForApi = raw != null && raw.isNotEmpty ? raw : null;
    _photoUrl = _photoUrlForApi != null
        ? ApiConfig.absoluteMediaUrl(_photoUrlForApi)
        : null;
  }

  static void clear() {
    _token = null;
    _role = null;
    _photoUrl = null;
    _photoUrlForApi = null;
  }

  static void applyToParent(UserRegistrationData data, RepriseDossier dossier) {
    RepriseMapper.applyParentDossier(data, dossier);
  }

  static void applyToAm(AmRegistrationData data, RepriseDossier dossier) {
    RepriseMapper.applyAmDossier(data, dossier);
  }
}
