require "rails_helper"

RSpec.describe AppearanceReleasesController, tye: :controller do
  render_views

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

  before do
    sign_in(user)
  end

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

      expect(response).to be_successful
    end

    it "renders content" do
      release = create(:appearance_release, project: project,
                                            person_first_name: "John", person_last_name: "Doe", person_phone: "5551234567", person_email: "jane.doe@test.com")
      create(:note, notable: release, content: "Some notes here")

      get :index, params: { project_id: project }

      expect(response.body).to have_content "John Doe"
      expect(response.body).to have_content "P: 5551234567E: jane.doe@test.com"
      expect(response.body).to have_content "Some notes here"
      expect(response.body).to have_button "Import Release"
      expect(response.body).to have_content "Manage"
    end

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

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

    context "when there are many records" do
      it "paginates the table" do
        create_list(:appearance_release, 20, project: project)

        get :index, params: { project_id: project }

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

    context "for xhr request" do
      it "filters the releases by a query param" do
        appearance_releases = [
          create(:appearance_release, person_first_name: "Adam", person_last_name: "Sandler", project: project),
          create(:appearance_release, person_first_name: "Zoe", person_last_name: "Saldana", project: project),
        ]

        get :index, params: { project_id: project, query: "Zoe" }, xhr: true

        expect(response.body).not_to have_content("Adam")
        expect(response.body).to have_content("Zoe")
      end

      it "filters the releases by a type_filter param" do
        appearance_releases = [
            create(:appearance_release, :without_person_photo, :non_native, person_first_name: "Adam", person_last_name: "Sandler", project: project),
            create(:appearance_release, :non_native, person_first_name: "Zoe", person_last_name: "Saldana", project: project),
        ]

        get :index, params: { project_id: project, type_filter: 'all' }, xhr: true

        expect(response.body).to have_content("Adam")
        expect(response.body).to have_content("Zoe")


        get :index, params: { project_id: project, type_filter: 'complete' }, xhr: true

        expect(response.body).not_to have_content("Adam")
        expect(response.body).to have_content("Zoe")

        get :index, params: { project_id: project, type_filter: 'incomplete' }, xhr: true

        expect(response.body).to have_content("Adam")
        expect(response.body).not_to have_content("Zoe")
      end

      it "filters the releases by a type_filter and query params simultaneously" do
        appearance_releases = [
            create(:appearance_release, :without_person_photo, :non_native, person_first_name: "Adam", person_last_name: "Sandler Incomplete", project: project),
            create(:appearance_release, :non_native, person_first_name: "Adam", person_last_name: "Sandler Complete", project: project),
            create(:appearance_release, :without_person_photo, :non_native, person_first_name: "Zoe", person_last_name: "Saldana Incomplete", project: project),
            create(:appearance_release, :non_native, person_first_name: "Zoe", person_last_name: "Saldana Complete", project: project),
        ]

        get :index, params: { project_id: project, type_filter: 'complete', query: 'Adam' }, xhr: true

        expect(response.body).to have_content("Adam Sandler Complete")
        expect(response.body).not_to have_content("Adam Sandler Incomplete")
        expect(response.body).not_to have_content("Zoe Saldana Incomplete")
        expect(response.body).not_to have_content("Zoe Saldana Complete")

        get :index, params: { project_id: project, type_filter: 'incomplete', query: 'Zoe' }, xhr: true

        expect(response.body).not_to have_content("Adam Sandler Complete")
        expect(response.body).not_to have_content("Adam Sandler Incomplete")
        expect(response.body).to have_content("Zoe Saldana Incomplete")
        expect(response.body).not_to have_content("Zoe Saldana Complete")
      end
    end
  end

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

      expect(response).to be_successful
    end
  end

  describe "#create" do
    it "responds successfully when guardian_photo is not sent" do
      expect {
        post :create, params: { project_id: project, appearance_release: appearance_release_params }
      }.to change(AppearanceRelease, :count).by(1)

      expect(response).to be_redirect
      expect(response).to redirect_to [project, :appearance_releases]
      expect(flash.notice).to be_present
    end

    it "responds successfully when guardian_photo is sent" do
      expect {
        post :create, params: { project_id: project, appearance_release: minor_appearance_release_params }
      }.to change(AppearanceRelease, :count).by(1)

      expect(response).to be_redirect
      expect(response).to redirect_to [project, :appearance_releases]
      expect(flash.notice).to be_present
    end

    it "enqueues headshot collection job" do
      expect {
        post :create, params: { project_id: project, appearance_release: appearance_release_params }
      }.to have_enqueued_job(AddHeadshotCollectionUidToProjectJob).with(project)
    end

    it "enqueues tagging job" do
      expect {
        post :create, params: { project_id: project, appearance_release: appearance_release_params }
      }.to have_enqueued_job(SetTagsForReleasableJob).with(AppearanceRelease.last)
    end

    it "logs analytics" do
      expect {
        post :create, params: { project_id: project, appearance_release: appearance_release_params }
      }.to(
        have_enqueued_job(TrackAnalyticsJob).with(user, account, :track_create_non_native_release, release_type: "AppearanceRelease", user_agent: "Rails Testing", user_ip: "0.0.0.0")
      )
    end

    context "when the record would be invalid" do
      before do
        allow_any_instance_of(AppearanceRelease).to receive(:save).and_return(false)
      end

      it "re-renders the form" do
        expect {
          post :create, params: { project_id: project, appearance_release: appearance_release_params }
        }.not_to change(AppearanceRelease, :count)

        expect(response).to be_successful
        expect(flash.notice).to be_nil
      end

      it "does not enqueue any jobs" do
        expect {
          post :create, params: { project_id: project, appearance_release: appearance_release_params }
        }.not_to have_enqueued_job
      end
    end
  end

  describe "#edit" do
    let(:non_native_appearance_release) { create(:appearance_release, project: project) }
    let(:native_appearance_release) { create(:appearance_release, :native, project: project) }

    it "responds successfully for non-native release" do
      get :edit, params: { project_id: project, id: non_native_appearance_release }

      expect(response).to be_successful
    end

    it "responds with error for native release" do
      expect { get :edit, params: { id: native_appearance_release } }.to raise_exception Pundit::NotAuthorizedError
    end
  end

  describe "#update" do
    let(:appearance_release) { create(:appearance_release, project: project) }

    it "responds successfully" do
      patch :update, params: { project_id: project, id: appearance_release, appearance_release: appearance_release_params }

      expect(response).to redirect_to [project, :appearance_releases]
      expect(flash.notice).to be_present
    end

    it "enqueues headshot collection job" do
      expect {
        patch :update, params: { project_id: project, id: appearance_release, appearance_release: appearance_release_params }
      }.to have_enqueued_job(AddHeadshotCollectionUidToProjectJob).with(project)
    end

    context "when the record would be invalid" do
      before do
        allow_any_instance_of(AppearanceRelease).to receive(:save).and_return(false)
      end

      it "re-renders the form" do
        patch :update, params: { project_id: project, id: appearance_release, appearance_release: appearance_release_params }

        expect(response).to be_successful
        expect(flash.notice).to be_nil
      end

      it "does not enqueue any jobs" do
        expect {
          patch :update, params: { project_id: project, id: appearance_release, appearance_release: appearance_release_params }
        }.not_to have_enqueued_job(AddHeadshotCollectionUidToProjectJob)
      end
    end
  end

  describe "#destroy" do
    let!(:appearance_release) { create(:appearance_release, project: project) }

    it "responds with redirect" do
      delete :destroy, params: { project_id: project, id: appearance_release }

      expect(response).to be_redirect
      expect(response).to redirect_to [project, :appearance_releases]
    end

    it "sets the flash" do
      delete :destroy, params: { project_id: project, id: appearance_release }

      expect(flash.alert).not_to be_nil
    end

    it "destroys the record" do
      expect {
        delete :destroy, params: { project_id: project, id: appearance_release }
      }.to change(AppearanceRelease, :count).by(-1)
    end
  end

  private

  def appearance_release_params
    attributes_for(:appearance_release, :non_native).except(:contract).merge(contract_param, exploitable_rights_params)
  end

  def minor_appearance_release_params
    attributes_for(:appearance_release, :non_native, :minor_with_guardian_photo)
      .except(:contract)
      .merge(contract_param, exploitable_rights_params)
  end

  def exploitable_rights_params
    {
      applicable_medium_id: ApplicableMedium.last.id,
      applicable_medium_text: "applicable_media",
      territory_id: Territory.last.id,
      territory_text: "territory",
      term_id: Term.last.id,
      term_text: "term",
      restriction_id: Restriction.last.id,
      restriction_text: "restrictions",
    }
  end

  def contract_param
    path = Rails.root.join("spec", "fixtures", "files", "contract.pdf")
    contract_file = Rack::Test::UploadedFile.new(path, "application/pdf")

    { contract: contract_file }
  end
end
