import Foundation

enum AnalyticsEvent {

    // MARK: - Authentication
    case userSignedIn(method: String)
    case userSignedInApple(isNewUser: Bool)
    case userSignedInGoogle(isNewUser: Bool)
    case userRegistered(method: String)

    // MARK: - Residence
    case residenceCreated(type: String)
    case residenceLimitReached
    case residenceShared(method: String)
    case shareResidencePaywallShown

    // MARK: - Task
    case taskCreated(residenceId: Int32)

    // MARK: - Onboarding (First Task screen)
    // Event names must stay in lockstep with
    // composeApp/.../analytics/Analytics.kt so PostHog funnels join cleanly
    // across iOS and Android.
    case onboardingSuggestionsLoaded(count: Int, profileCompleteness: Double)
    case onboardingSuggestionAccepted(templateId: Int32, relevanceScore: Double)
    case onboardingBrowseTemplateAccepted(templateId: Int32, categoryId: Int32?)
    case onboardingTasksCreated(count: Int)
    case onboardingTaskStepSkipped(reason: String)

    // MARK: - Contractor
    case contractorCreated
    case contractorShared
    case contractorPaywallShown(currentCount: Int)
    case shareContractorPaywallShown

    // MARK: - Documents
    case documentCreated(type: String)
    case documentsPaywallShown(currentCount: Int)

    // MARK: - Settings
    case themeChanged(theme: String)
    case honeycombToggled(enabled: Bool)
    case analyticsToggled(enabled: Bool)

    // MARK: - Errors
    case errorOccurred(domain: String, message: String, screen: String?)

    // MARK: - Payload

    var payload: (String, [String: Any]?) {
        switch self {

        // Authentication
        case .userSignedIn(let method):
            return ("user_signed_in", ["method": method])
        case .userSignedInApple(let isNewUser):
            return ("user_signed_in_apple", ["is_new_user": isNewUser])
        case .userSignedInGoogle(let isNewUser):
            return ("user_signed_in_google", ["is_new_user": isNewUser])
        case .userRegistered(let method):
            return ("user_registered", ["method": method])

        // Residence
        case .residenceCreated(let type):
            return ("residence_created", ["residence_type": type])
        case .residenceLimitReached:
            return ("residence_limit_reached", nil)
        case .residenceShared(let method):
            return ("residence_shared", ["method": method])
        case .shareResidencePaywallShown:
            return ("share_residence_paywall_shown", nil)

        // Task
        case .taskCreated(let residenceId):
            return ("task_created", ["residence_id": residenceId])

        // Onboarding
        case .onboardingSuggestionsLoaded(let count, let completeness):
            return ("onboarding_suggestions_loaded", [
                "count": count,
                "profile_completeness": completeness
            ])
        case .onboardingSuggestionAccepted(let templateId, let relevance):
            return ("onboarding_suggestion_accepted", [
                "template_id": templateId,
                "relevance_score": relevance
            ])
        case .onboardingBrowseTemplateAccepted(let templateId, let categoryId):
            var props: [String: Any] = ["template_id": templateId]
            if let categoryId { props["category_id"] = categoryId }
            return ("onboarding_browse_template_accepted", props)
        case .onboardingTasksCreated(let count):
            return ("onboarding_tasks_created", ["count": count])
        case .onboardingTaskStepSkipped(let reason):
            return ("onboarding_task_step_skipped", ["reason": reason])

        // Contractor
        case .contractorCreated:
            return ("contractor_created", nil)
        case .contractorShared:
            return ("contractor_shared", nil)
        case .contractorPaywallShown(let currentCount):
            return ("contractor_paywall_shown", ["current_count": currentCount])
        case .shareContractorPaywallShown:
            return ("share_contractor_paywall_shown", nil)

        // Documents
        case .documentCreated(let type):
            return ("document_created", ["type": type])
        case .documentsPaywallShown(let currentCount):
            return ("documents_paywall_shown", ["current_count": currentCount])

        // Settings
        case .themeChanged(let theme):
            return ("theme_changed", ["theme": theme])
        case .honeycombToggled(let enabled):
            return ("honeycomb_toggled", ["enabled": enabled])
        case .analyticsToggled(let enabled):
            return ("analytics_toggled", ["enabled": enabled])

        // Errors
        case .errorOccurred(let domain, let message, let screen):
            var props: [String: Any] = ["domain": domain, "message": message]
            if let screen { props["screen"] = screen }
            return ("error_occurred", props)
        }
    }
}
