require "rails_helper"

RSpec.describe ReportsController, type: :controller do
  render_views

  let(:current_user) { create(:user) }
  let(:project) { create(:project, :discovery_client, account: current_user.primary_account) }

  before do
    sign_in current_user
  end

  describe "#index" do
    it "responds successfully" do
      get :index, params: { project_id: project }

      expect(response).to be_successful
    end

    context "for a Discovery client" do
      let(:project) { create(:project, :discovery_client, account: current_user.primary_account) }

      it "renders content" do
        create(:video, :published, project: project, name: "My Video", number: "001")

        get :index, params: { project_id: project }

        expect(response.body).to have_content "My Video"
        expect(response.body).to have_content "001"
        expect(response.body).to have_content "Production Elements Log"
        expect(response.body).to have_content "GFX Cue List"
        expect(response.body).to have_content "Music Cue Sheet"
        expect(response.body).to have_content "BiG Music Cue Sheet"
        expect(response.body).to have_content "Issues and Concerns Report"
      end
    end

    context "for a Nat Geo client" do
      let(:project) { create(:project, :nat_geo_client, account: current_user.primary_account) }

      it "renders content" do
        create(:video, :published, project: project, name: "My Video", number: "001")

        get :index, params: { project_id: project }

        expect(response.body).to have_content "My Video"
        expect(response.body).to have_content "001"
        expect(response.body).to have_content "Legal Binder Log"
        expect(response.body).to have_content "Text Graphics Log"
        expect(response.body).to have_content "Music Cue Sheet"
        expect(response.body).to have_content "Original Music Log"
        expect(response.body).to have_content "BiG Music Cue Sheet"
        expect(response.body).to have_content "Issues and Concerns Report"
      end
    end

    context "for another client" do
      let(:project) { create(:project, account: current_user.primary_account) }

      it "renders content" do
        create(:video, :published, project: project, name: "My Video", number: "001")

        get :index, params: { project_id: project }

        expect(response.body).to have_content "My Video"
        expect(response.body).to have_content "001"
        expect(response.body).to have_content "Production Elements Log"
        expect(response.body).to have_content "GFX Cue List"
        expect(response.body).to have_content "Music Cue Sheet"
        expect(response.body).to have_content "BiG Music Cue Sheet"
        expect(response.body).to have_content "Issues and Concerns Report"
      end
    end

    context "when there are no reports" do
      it "renders an empty message" do
        get :index, params: { project_id: project }

        expect(response.body).to have_content("Reports will appear here")
      end
    end

    context "when there are many records" do
      it "paginates the table" do
        create_list(:video, 4, :published, project: project)

        get :index, params: { project_id: project }

        expect(response.body).to have_link("2", href: project_reports_path(project, page: 2))
      end
    end

    describe "xhr request" do
      let(:project) { create(:project, :discovery_client, account: current_user.primary_account) }

      before do
        create(:video, :published, project: project, name: "My Video", number: "001")
      end

      context "when search field is empty" do
        it "shows all reports" do
          get :index, params: { project_id: project }, xhr: true

          expect(response.body).to have_content "Production Elements Log"
          expect(response.body).to have_content "GFX Cue List"
          expect(response.body).to have_content "Music Cue Sheet"
          expect(response.body).to have_content "BiG Music Cue Sheet"
          expect(response.body).to have_content "Issues and Concerns Report"
        end
      end

      context "when search field is not empty" do
        it "shows only reports with name matching search query" do
          get :index, params: { project_id: project, query: "Production" }, xhr: true

          expect(response.body).to have_content("Production Elements Log")

          expect(response.body).not_to have_content("GFX Cue List")
          expect(response.body).not_to have_content("Music Cue Sheet")
          expect(response.body).not_to have_content("BiG Music Cue Sheet")
          expect(response.body).not_to have_content("Issues and Concerns Report")
        end
      end
    end
  end
end
