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

class UserModel extends User {
  const UserModel({
    required super.id,
    required super.name,
    required super.email,
    required super.role,
    super.plan,
    super.paymentStatus,
    super.phoneNumber,
    super.message,
    super.code,
  });

  factory UserModel.fromJson(Map<String, dynamic> json) {
    final payment = json['payment'] ?? {};
    return UserModel(
      id: json['userid'] ?? json['id']?.toString() ?? '',
      name: json['name'] ?? '',
      email: json['email'] ?? '',
      role: json['role'] ?? '',
      plan: payment['plan'] ?? json['plan'],
      paymentStatus: payment['status'] ?? json['paymentStatus'],
      phoneNumber: json['phonenumber'] ?? '',
      message: json['message'] ?? '',
      code: json['code'] ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'userid': id,
      'name': name,
      'email': email,
      'role': role,
      'plan': plan,
      'paymentStatus': paymentStatus,
      'phonenumber': phoneNumber,
      'message': message,
      'code': code,
    };
  }

  /// Store user as raw JSON string
  String toRawJson() => jsonEncode(toJson());

  /// Parse user from raw JSON string
  factory UserModel.fromRawJson(String raw) =>
      UserModel.fromJson(jsonDecode(raw));

  @override
  String toString() {
    return 'UserModel(id: $id, name: $name, email: $email, role: $role, phone: $phoneNumber, plan: $plan, status: $paymentStatus, code: $code)';
  }
}
