package com.tt.honeyDue.models

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class SubscriptionStatus(
    val tier: String = "free",
    @SerialName("is_active") val isActive: Boolean = false,
    @SerialName("subscribed_at") val subscribedAt: String? = null,
    @SerialName("expires_at") val expiresAt: String? = null,
    @SerialName("auto_renew") val autoRenew: Boolean = true,
    val usage: UsageStats,
    val limits: Map<String, TierLimits>,  // {"free": {...}, "pro": {...}}
    @SerialName("limitations_enabled") val limitationsEnabled: Boolean = false,  // Master toggle
    @SerialName("trial_start") val trialStart: String? = null,
    @SerialName("trial_end") val trialEnd: String? = null,
    @SerialName("trial_active") val trialActive: Boolean = false,
    @SerialName("subscription_source") val subscriptionSource: String? = null,
)

@Serializable
data class UsageStats(
    @SerialName("properties_count") val propertiesCount: Int,
    @SerialName("tasks_count") val tasksCount: Int,
    @SerialName("contractors_count") val contractorsCount: Int,
    @SerialName("documents_count") val documentsCount: Int
)

@Serializable
data class TierLimits(
    val properties: Int? = null,  // null = unlimited
    val tasks: Int? = null,
    val contractors: Int? = null,
    val documents: Int? = null
)

@Serializable
data class UpgradeTriggerData(
    val title: String,
    val message: String,
    @SerialName("promo_html") val promoHtml: String? = null,
    @SerialName("button_text") val buttonText: String
)

@Serializable
data class FeatureBenefit(
    @SerialName("feature_name") val featureName: String,
    @SerialName("free_tier_text") val freeTierText: String,
    @SerialName("pro_tier_text") val proTierText: String
)

@Serializable
data class Promotion(
    @SerialName("promotion_id") val promotionId: String,
    val title: String,
    val message: String,
    val link: String? = null
)

@Serializable
data class ReceiptVerificationRequest(
    @SerialName("receipt_data") val receiptData: String
)

@Serializable
data class PurchaseVerificationRequest(
    @SerialName("purchase_token") val purchaseToken: String,
    @SerialName("product_id") val productId: String
)

/**
 * Nested subscription info returned by backend purchase/restore endpoints.
 */
@Serializable
data class VerificationSubscriptionInfo(
    val tier: String = "",
    @SerialName("subscribed_at") val subscribedAt: String? = null,
    @SerialName("expires_at") val expiresAt: String? = null,
    @SerialName("auto_renew") val autoRenew: Boolean = true,
    @SerialName("cancelled_at") val cancelledAt: String? = null,
    val platform: String = "",
    @SerialName("is_active") val isActive: Boolean = false,
    @SerialName("is_pro") val isPro: Boolean = false
)

/**
 * Response from backend purchase/restore endpoints.
 * Backend returns: { "message": "...", "subscription": { "tier": "pro", ... } }
 */
@Serializable
data class VerificationResponse(
    val message: String = "",
    val subscription: VerificationSubscriptionInfo? = null
) {
    /** Backward-compatible: success when subscription is present */
    val success: Boolean get() = subscription != null

    /** Backward-compatible: tier extracted from nested subscription */
    val tier: String? get() = subscription?.tier
}
