import { StagingStorageStrategy } from "./staging-storage-strategy.js"
import { PlainTextStagingStrategy } from "./plain-text-staging-strategy.js"
import { GitBranchStagingStrategy } from "./git-branch-staging-strategy.js"

export interface StagingStorageEligibility {
  tier: string
  hasGitRepo: boolean
  owner?: string
  repo?: string
  privacyMode?: boolean
}

const ELIGIBLE_TIERS = ["pro", "enterprise"]

export class StagingStorageStrategyFactory {
  /**
   * Returns the appropriate storage strategy based on user tier, privacy mode, and git repo availability.
   *
   * Git branch storage is only available when ALL conditions are met:
   * - PRO or Enterprise tier users
   * - Privacy mode is enabled (user wants code stored on GitHub only)
   * - Users with a valid git repository (owner and repo provided)
   *
   * When privacy mode is disabled (paused), data is stored as plain text in the database
   * for faster loading, regardless of tier.
   */
  static getStrategy(eligibility: StagingStorageEligibility): StagingStorageStrategy {
    const { tier, hasGitRepo, owner, repo, privacyMode } = eligibility

    const isEligibleTier = ELIGIBLE_TIERS.includes(tier.toLowerCase())
    const hasValidRepo = hasGitRepo && !!owner && !!repo
    const isPrivacyModeEnabled = privacyMode

    // Git branch storage requires: eligible tier + valid repo + privacy mode enabled
    if (isEligibleTier && hasValidRepo && isPrivacyModeEnabled) {
      return new GitBranchStagingStrategy()
    }

    // All other cases (including privacy mode disabled) use plain text storage
    return new PlainTextStagingStrategy()
  }

  /**
   * Determines whether git branch storage is available for the given eligibility.
   */
  static isGitBranchStorageAvailable(eligibility: StagingStorageEligibility): boolean {
    const { tier, hasGitRepo, owner, repo, privacyMode } = eligibility
    const isEligibleTier = ELIGIBLE_TIERS.includes(tier.toLowerCase())
    const hasValidRepo = hasGitRepo && !!owner && !!repo
    const isPrivacyModeEnabled = privacyMode === true
    return isEligibleTier && hasValidRepo && isPrivacyModeEnabled
  }
}
