class Analytics
  def initialize(user, account, backend = AnalyticsRuby)
    @user = user || Guest.new
    @account = account
    @backend = backend
  end

  def track_user_sign_in(user_agent:, user_ip:)
    if analytics_enabled?
      identify
      track(
        {
          user_id: user.id,
          event: "Sign In User",
          properties: {
            account: account.try(:name),
            account_id: account.try(:id),
            user_agent: user_agent,
            ip: user_ip,
          },
        }
      )
    end
  end

  def track_guest_sign_up(user_agent:, user_ip:)
    if analytics_enabled?
      identify
      track(
        {
          user_id: user.id,
          event: "Guest User Sign Up",
          properties: {
            account: account.try(:name),
            account_id: account.try(:id),
            user_agent: user_agent,
            ip: user_ip,
          },
        }
      )
    end
  end

  def track_create_native_release(release_type:, account:, user_agent:, user_ip:, application: "web")
    if analytics_enabled?
      identify
      track(
        {
          anonymous_id: user.id,
          event: "Release Signed",
          properties: {
            release_type: release_type,
            account: account.try(:name),
            account_id: account.try(:id),
            user_agent: user_agent,
            ip: user_ip,
            application: application
          },
        }
      )
    end
  end

  def track_create_non_native_release(release_type:, user_agent:, user_ip:)
    if analytics_enabled?
      identify
      track(
        {
          user_id: user.id,
          event: "Release Imported",
          properties: {
            release_type: release_type,
            account: account.try(:name),
            account_id: account.try(:id),
            user_agent: user_agent,
            ip: user_ip,
          },
        }
      )
    end
  end

  def track_video_upload(user_agent:, user_ip:)
    if analytics_enabled?
      identify
      track(
        {
          user_id: user.id,
          event: "Video Uploaded",
          properties: {
            account: account.try(:name),
            account_id: account.try(:id),
            user_agent: user_agent,
            ip: user_ip,
          },
        }
      )
    end
  end

  def track_create_project(user_agent:, user_ip:)
    if analytics_enabled?
      identify
      track(
        {
          user_id: user.id,
          event: "Project Created",
          properties: {
            account: account.try(:name),
            account_id: account.try(:id),
            user_agent: user_agent,
            ip: user_ip,
          },
        }
      )
    end
  end

  def track_create_custom_folder(user_agent:, user_ip:)
    if analytics_enabled?
      identify
      track(
        {
          user_id: user.id,
          event: "Custom folder created",
          properties: {
            account: account.try(:name),
            account_id: account.try(:id),
            user_agent: user_agent,
            ip: user_ip,
          },
        }
      )
    end
  end

  def track_create_live_stream(user_agent:, user_ip:)
    if analytics_enabled?
      identify
      track(
        {
          user_id: user.id,
          event: "Live stream created",
          properties: {
            account: account.try(:name),
            account_id: account.try(:id),
            user_agent: user_agent,
            ip: user_ip,
          },
        }
      )
    end
  end
  
  private

  attr_reader :user, :account, :backend

  def analytics_enabled?
    ENV["ENABLE_ANALYTICS"] == "true"
  end

  def identify
    backend.identify(identify_params)
  end

  def identify_params
    if user.anonymous?
      {
        anonymous_id: user.id
      }
    else
      {
        user_id: user.id,
        traits: traits
      }
    end
  end

  def traits
    {
      email: user.email,
    }.reject { |_key, value| value.blank? }
  end

  def track(options)
    backend.track(options)
  end

  class Guest
    attr_reader :id

    def initialize
      @id = SecureRandom.uuid
    end

    def anonymous?
      true
    end
  end
end
