require "rails_helper"

feature "Admin managing accounts" do
  let(:current_user) { create(:user, admin: true, email: "user@test.com") }
  let(:another_user) { create(:user, admin: false, email: "user2@test.com", accounts: current_user.accounts) }
  let(:project) { create(:project, account: current_user.primary_account) }

  scenario "creates a new account" do
    sign_in current_user
    visit admin_signed_in_root_path

    click_link "New Account"
    fill_in "Name", with: "MGM"
    select "ME Suite", from: "Plan"
    click_on "Create Account"

    expect(page).to have_content "The account was created"
    expect(page).to have_content /MGM/
    expect(page).to have_content /Add Account Manager/
  end

  scenario "sees account details" do
    sign_in current_user
    visit admin_signed_in_root_path
    click_button "Manage"
    click_link "Overview"

    expect(page).to have_content "ME Suite"
    expect(page).to have_content "Users 1"
    expect(page).to have_content "Created at less than a minute ago"
  end

  scenario "sees videos for an account in the system" do
    visit_account_overview_page

    expect(page).to have_content "Videos"
    expect(page).to have_content "Name My Video"

    by "clicking on Analysis link can navigate to video analysis page" do
      click_button "Manage"
      click_link "Analysis"

      expect(page.current_path).to eq video_video_analyses_path(I18n.locale, @video_1)
    end
  end

  scenario "re-analyzes the video" do
    visit_account_overview_page

    by "clicking on Re-analyze link can navigate to video analysis page" do
      click_button "Manage"
      click_link "Re-analyze"

      expect(page).to have_content t("video_analyses.create.notice")
      expect(page.current_path).to eq video_video_analyses_path(I18n.locale, @video_1)
    end
  end

  scenario "manages users in an account" do
    another_account = create(:account, name: "New Account")
    user = create(:user, admin: false, email: "user@example.com")
    user.account_auths << AccountAuth.create(user: user, account: current_user.primary_account, role: :account_manager)

    sign_in current_user
    visit admin_signed_in_root_path
    within "#account_#{current_user.primary_account.id}" do
      click_button "Manage"
      click_link "Account Managers"
    end

    expect(page).to have_content "user@example.com"
  end

  scenario "manages an account" do
    sign_in current_user
    visit admin_signed_in_root_path
    click_button "Manage"
    click_link "Edit"

    expect(page).to have_content "Edit Account"

    by "updating an account" do
      fill_in "Name", with: "Another Team"
      click_button "Update Account"
    end

    expect(page).to have_content "Another Team"
  end

  scenario "sees information for each account in the system" do
    mgm = create(:account, name: "MGM", users: build_list(:user, 1, email: "user@mgm.com"), projects: build_list(:project, 1))
    discovery = create(:account, name: "Discovery", users: [build(:user, email: "user1@discovery.com"), build(:user, email: "user2@discovery.com")])

    video_1 = create(:video, project: mgm.projects.first)
    path = Rails.root.join("spec", "fixtures", "files", "video_file_1m.mp4")
    longer_file = Rack::Test::UploadedFile.new(path, "video/mp4")
    video_2 = create(:video, project: mgm.projects.first, file: longer_file)
    video_3 = create(:video, project: mgm.projects.first, file: longer_file, created_at: 1.month.ago)
    video_1.file.blob.analyze
    video_2.file.blob.analyze
    video_3.file.blob.analyze

    sign_in current_user
    visit profile_path
    click_link "Admin"

    expect(page).to have_content /Name Plan # Projects Monthly Video Upload Minutes Total Video Upload Minutes Total Storage Created At/
    expect(page).to have_content /MGM ME Suite 1 1 minutes 2 minutes 6.74 MB/
    expect(page).to have_content /Discovery ME Suite 0 0 minutes 0 minutes 0 Bytes/
  end

  scenario "Uses the search button to filter accounts", js: true do
    sign_in current_user

    create(:account, name: "First account")
    create(:account, name: "Second account")

    visit admin_accounts_path

    expect(page).to have_content("First account")
    expect(page).to have_content("Second account")

    fill_in "query", with: "First"
    click_button "search-button"

    expect(page).to have_content("First account")
    expect(page).not_to have_content("Second_account")
  end

  scenario "Uses search button to filter videos in account overview", js: true do
    sign_in current_user

    account = create(:account, name: "User accound")
    project = create(:project, account: account)
    create(:video, name: "First Video", project: project)
    create(:video, name: "Second Video", project: project)

    visit admin_account_path(account)

    expect(page).to have_content("First Video")
    expect(page).to have_content("Second Video")

    fill_in "query", with: "First"
    click_button "search-button"

    expect(page).to have_content("First Video")
    expect(page).not_to have_content("Second Video")
  end

  def visit_account_overview_page
    @video_1 = create(:video, project: project)
    @video_1.file.blob.analyze

    VideoAnalysis.use_overlay_video = false
    allow(BrayniacAI::EdlParse).to receive(:create).and_return(
      BrayniacAI::EdlParse.new({ results: [], edl_timecode_start: "00:00:00:00" })
    )

    sign_in current_user
    visit admin_signed_in_root_path
    click_button "Manage"
    click_link "Overview"
  end
end
