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

/// ✅ STORE MODEL
class StoreModel {
  final String name;
  final String description;
  final String url;
  final String urlPath;
  final String? lastOpenedTime;
  final String? lastOpenedTimeRaw;
  final String? logoUrl;

  const StoreModel({
    required this.name,
    required this.description,
    required this.url,
    required this.urlPath,
    this.lastOpenedTime,
    this.lastOpenedTimeRaw,
    this.logoUrl,
  });

  factory StoreModel.fromJson(Map<String, dynamic> json) {
    return StoreModel(
      name: json['name'] ?? '',
      description: json['description'] ?? '',
      url: json['url'] ?? '',
      urlPath: json['urlPath'] ?? '',
      lastOpenedTime: json['lastOpenedTime'],
      lastOpenedTimeRaw: json['lastOpenedTimeRaw'],
      logoUrl: json['logo']?['url'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'description': description,
      'url': url,
      'urlPath': urlPath,
      'lastOpenedTime': lastOpenedTime,
      'lastOpenedTimeRaw': lastOpenedTimeRaw,
      'logo': {
        'url': logoUrl,
      },
    };
  }
}

/// ✅ PAYMENT MODEL
class PaymentModel {
  final int id;
  final String email;
  final String amount;
  final String plan;
  final String status;
  final String? stripeSessionId;
  final String? stripePaymentIntentId;
  final String? subscriptionId;
  final String? startDate;
  final String? endDate;

  const PaymentModel({
    required this.id,
    required this.email,
    required this.amount,
    required this.plan,
    required this.status,
    this.stripeSessionId,
    this.stripePaymentIntentId,
    this.subscriptionId,
    this.startDate,
    this.endDate,
  });

  factory PaymentModel.fromJson(Map<String, dynamic> json) {
    return PaymentModel(
      id: json['id'] ?? 0,
      email: json['email'] ?? '',
      amount: json['amount'] ?? '',
      plan: json['plan'] ?? '',
      status: json['status'] ?? '',
      stripeSessionId: json['stripeSessionId'],
      stripePaymentIntentId: json['stripePaymentIntentId'],
      subscriptionId: json['subscriptionId'],
      startDate: json['startDate'],
      endDate: json['endDate'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'email': email,
      'amount': amount,
      'plan': plan,
      'status': status,
      'stripeSessionId': stripeSessionId,
      'stripePaymentIntentId': stripePaymentIntentId,
      'subscriptionId': subscriptionId,
      'startDate': startDate,
      'endDate': endDate,
    };
  }
}

/// ✅ MAIN USER MODEL
class UserModel extends User {
  final StoreModel? store;
  final PaymentModel? payment;

  const UserModel({
    required super.id,
    required super.name,
    required super.email,
    required super.role,
    super.plan,
    super.paymentStatus,
    super.phoneNumber,
    super.message,
    super.code,
    this.store,
    this.payment,
  });

  /// ✅ FROM API JSON
  factory UserModel.fromJson(Map<String, dynamic> json) {
    final paymentJson = json['payment'];
    final storeJson = json['store'];

    final payment =
        paymentJson != null ? PaymentModel.fromJson(paymentJson) : null;

    final store =
        storeJson != null ? StoreModel.fromJson(storeJson) : null;

    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'] ?? '',
      store: store,
      payment: payment,
    );
  }

  /// ✅ FROM ENTITY (LOCAL CONVERSION)
  factory UserModel.fromEntity(User user) {
    return UserModel(
      id: user.id,
      name: user.name,
      email: user.email,
      role: user.role,
      plan: user.plan,
      paymentStatus: user.paymentStatus,
      phoneNumber: user.phoneNumber,
      message: user.message,
      code: user.code,
    );
  }

  /// ✅ TO JSON (FOR LOCAL STORAGE)
  Map<String, dynamic> toJson() {
    return {
      'userid': id,
      'name': name,
      'email': email,
      'role': role,
      'plan': plan,
      'paymentStatus': paymentStatus,
      'phonenumber': phoneNumber,
      'message': message,
      'code': code,
      'store': store?.toJson(),
      'payment': payment?.toJson(),
    };
  }

  /// ✅ STORE USER AS RAW JSON STRING
  String toRawJson() => jsonEncode(toJson());

  /// ✅ RESTORE 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, plan: $plan, status: $paymentStatus)';
  }
}
