require "rails_helper"

RSpec.describe Analytics do
  let(:analytics_backend) { class_double("AnalyticsRuby", :analytics_backend) }
  let(:user) { create(:user, email: "bob@example.com", accounts: [build(:account, name: "Courter Pint Brewery")]) }
  let(:account) { user.primary_account }

  before :each do
    allow(analytics_backend).to receive(:identify)
    allow(analytics_backend).to receive(:track)
  end

  describe "#track_user_sign_in" do
    it "identifies user_id, email" do
      expect(analytics_backend).to receive(:identify).with(
        {
          user_id: user.id,
          traits: {
            email: "bob@example.com",
          },
        }
      )

      described_class.new(user, account, analytics_backend).track_user_sign_in(user_agent: "Mozilla Firefox", user_ip: "0.0.0.0")
    end

    it "tracks event with user_id and 'Sign In User', account, account_id, user_agent, ip" do
      expect(analytics_backend).to receive(:track).with(
        {
          user_id: user.id,
          event: "Sign In User",
          properties: {
            account: "Courter Pint Brewery",
            account_id: user.primary_account.id,
            user_agent: "Mozilla Firefox",
            ip: "0.0.0.0",
          },
        }
      )

      described_class.new(user, account, analytics_backend).track_user_sign_in(user_agent: "Mozilla Firefox", user_ip: "0.0.0.0")
    end
  end

  describe "#track_guest_sign_up" do
    it "identifies user_id, email" do
      expect(analytics_backend).to receive(:identify).with(
        {
          user_id: user.id,
          traits: {
            email: "bob@example.com",
          },
        }
      )

      described_class.new(user, account, analytics_backend).track_guest_sign_up(user_agent: "Mozilla Firefox", user_ip: "0.0.0.0")
    end

    it "tracks event with user_id and 'Guest User Sign Up', account, account_id, user_agent, ip" do
      expect(analytics_backend).to receive(:track).with(
        {
          user_id: user.id,
          event: "Guest User Sign Up",
          properties: {
            account: "Courter Pint Brewery",
            account_id: user.primary_account.id,
            user_agent: "Mozilla Firefox",
            ip: "0.0.0.0",
          },
        }
      )

      described_class.new(user, account, analytics_backend).track_guest_sign_up(user_agent: "Mozilla Firefox", user_ip: "0.0.0.0")
    end
  end

  describe "#track_create_native_release" do
    it "identifies anonymous_id" do
      allow(SecureRandom).to receive(:uuid).and_return("securerandom")

      expect(analytics_backend).to receive(:identify).with(
        {
          anonymous_id: "securerandom",
        }
      )

      described_class.new(nil, nil, analytics_backend).track_create_native_release(release_type: MaterialRelease.to_s, account: build(:account), user_agent: "Mozilla Firefox", user_ip: "0.0.0.0")
    end

    it "tracks event with 'Release Signed', release type, account, account_id, user_agent, ip" do
      allow(SecureRandom).to receive(:uuid).and_return("securerandom")
      account = create(:account, name: "Courter Pint Brewery")

      expect(analytics_backend).to receive(:track).with(
        {
          anonymous_id: "securerandom",
          event: "Release Signed",
          properties: {
            release_type: MaterialRelease.to_s,
            account: "Courter Pint Brewery",
            account_id: account.id,
            application: "web",
            user_agent: "Mozilla Firefox",
            ip: "0.0.0.0",
          },
        }
      )

      described_class.new(nil, nil, analytics_backend).track_create_native_release(release_type: MaterialRelease.to_s, account: account, user_agent: "Mozilla Firefox", user_ip: "0.0.0.0")
    end
  end

  describe "#track_create_non_native_release" do
    it "identifies user_id, email" do
      expect(analytics_backend).to receive(:identify).with(
        {
          user_id: user.id,
          traits: {
            email: "bob@example.com",
          },
        }
      )

      described_class.new(user, account, analytics_backend).track_create_non_native_release(release_type: MaterialRelease.to_s, user_agent: "Mozilla Firefox", user_ip: "0.0.0.0")
    end

    it "tracks event with user_id and 'Release Imported', release type, account, account_id, user_agent, ip" do
      expect(analytics_backend).to receive(:track).with(
        {
          user_id: user.id,
          event: "Release Imported",
          properties: {
            release_type: MaterialRelease.to_s,
            account: "Courter Pint Brewery",
            account_id: user.primary_account.id,
            user_agent: "Mozilla Firefox",
            ip: "0.0.0.0",
          },
        }
      )

      described_class.new(user, account, analytics_backend).track_create_non_native_release(release_type: MaterialRelease.to_s, user_agent: "Mozilla Firefox", user_ip: "0.0.0.0")
    end
  end

  describe "#track_video_upload" do
    it "identifies user_id, email" do
      expect(analytics_backend).to receive(:identify).with(
        {
          user_id: user.id,
          traits: {
            email: "bob@example.com",
          },
        }
      )

      described_class.new(user, account, analytics_backend).track_video_upload(user_agent: "Mozilla Firefox", user_ip: "0.0.0.0")
    end

    it "tracks event with user_id and 'Video Uploaded', account, account_id, user_agent, ip" do
      expect(analytics_backend).to receive(:track).with(
        {
          user_id: user.id,
          event: "Video Uploaded",
          properties: {
            account: "Courter Pint Brewery",
            account_id: user.primary_account.id,
            user_agent: "Mozilla Firefox",
            ip: "0.0.0.0",
          },
        }
      )

      described_class.new(user, account, analytics_backend).track_video_upload(user_agent: "Mozilla Firefox", user_ip: "0.0.0.0")
    end
  end

  describe "#track_create_project" do
    it "identifies user_id, email" do
      expect(analytics_backend).to receive(:identify).with(
        {
          user_id: user.id,
          traits: {
            email: "bob@example.com",
          },
        }
      )

      described_class.new(user, account, analytics_backend).track_create_project(user_agent: "Mozilla Firefox", user_ip: "0.0.0.0")
    end

    it "tracks event with user_id and 'Project Created', account, account_id, user_agent, ip" do
      expect(analytics_backend).to receive(:track).with(
        {
          user_id: user.id,
          event: "Project Created",
          properties: {
            account: "Courter Pint Brewery",
            account_id: user.primary_account.id,
            user_agent: "Mozilla Firefox",
            ip: "0.0.0.0",
          },
        }
      )

      described_class.new(user, account, analytics_backend).track_create_project(user_agent: "Mozilla Firefox", user_ip: "0.0.0.0")
    end
  end
end
