import 'dart:convert';
import 'package:autos/domain/entities/turn14.dart';

class Turn14Response {
  final String code;
  final String message;
  final String userId;
  final String accessToken;

  const Turn14Response({
    required this.code,
    required this.message,
    required this.userId,
    required this.accessToken,
  });

  factory Turn14Response.fromJson(Map<String, dynamic> json) {
    return Turn14Response(
      code: json['code'] ?? '',
      message: json['message'] ?? '',
      userId: json['userid'] ?? '',
      accessToken: json['access_token'] ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'code': code,
      'message': message,
      'userid': userId,
      'access_token': accessToken,
    };
  }

  /// Convert model → raw JSON string
  String toRawJson() => jsonEncode(toJson());

  /// Convert raw JSON string → model
  factory Turn14Response.fromRawJson(String raw) =>
      Turn14Response.fromJson(jsonDecode(raw));

  /// Convert Response → Domain Entity
  Turn14Entity toEntity() {
    return Turn14Entity(
      code: code,
      message: message,
      userId: userId,
      accessToken: accessToken,
    );
  }

  @override
  String toString() {
    return 'Turn14Response(code: $code, message: $message, userId: $userId, accessToken: $accessToken)';
  }
}



class Turn14StatusModel {
  final String userId;
  final bool hasCredentials;
  final String? clientId;
  final String? clientSecret;
  final String? accessToken;
  final String? expiresIn;

  Turn14StatusModel({
    required this.userId,
    required this.hasCredentials,
    this.clientId,
    this.clientSecret,
    this.accessToken,
    this.expiresIn,
  });

  factory Turn14StatusModel.fromJson(Map<String, dynamic> json) {
    return Turn14StatusModel(
      userId: json["userid"],
      hasCredentials: json["hasCredentials"] ?? false,
      clientId: json["credentials"]?["turn14clientid"],
      clientSecret: json["credentials"]?["turn14clientsecret"],
      accessToken: json["tokenInfo"]?["access_token"],
      expiresIn: json["tokenInfo"]?["expires_in"],
    );
  }

  Turn14StatusEntity toEntity() {
    return Turn14StatusEntity(
      userId: userId,
      hasCredentials: hasCredentials,
      clientId: clientId,
      clientSecret: clientSecret,
      accessToken: accessToken,
      expiresIn: expiresIn,
    );
  }
}
