/// Frappe Session Model
///
/// Data model for Frappe API session response.
/// Used for public API authentication to access blog content.
library;

import 'package:json_annotation/json_annotation.dart';

part 'frappe_session_model.g.dart';

/// Frappe Session Data
///
/// Contains session credentials from Frappe API.
@JsonSerializable()
class FrappeSessionData {
  /// Session ID
  final String sid;

  /// CSRF Token
  @JsonKey(name: 'csrf_token')
  final String csrfToken;

  const FrappeSessionData({
    required this.sid,
    required this.csrfToken,
  });

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

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

/// Frappe Session Message Wrapper
@JsonSerializable()
class FrappeSessionMessage {
  /// Session data
  final FrappeSessionData data;

  const FrappeSessionMessage({
    required this.data,
  });

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

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

/// Frappe Session Response
///
/// API response from get_session endpoint.
/// Example:
/// ```json
/// {
///   "message": {
///     "data": {
///       "sid": "edb6059ecf147f268176cd4aff8ca034a75ebb8ff23464f9913c9537",
///       "csrf_token": "d0077178c349f69bc1456401d9a3d90ef0f7b9df3e08cfd26794a53f"
///     }
///   },
///   "home_page": "/app",
///   "full_name": "PublicAPI"
/// }
/// ```
@JsonSerializable()
class FrappeSessionResponse {
  /// Message containing session data
  final FrappeSessionMessage message;

  /// Home page path
  @JsonKey(name: 'home_page')
  final String homePage;

  /// Full name of the API user
  @JsonKey(name: 'full_name')
  final String fullName;

  const FrappeSessionResponse({
    required this.message,
    required this.homePage,
    required this.fullName,
  });

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

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

  /// Get session ID
  String get sid => message.data.sid;

  /// Get CSRF token
  String get csrfToken => message.data.csrfToken;
}
