require "rails_helper"

describe Public::LocationReleasesController do
  let(:user)    { create(:user) }
  let(:project) { create(:project, account: user.primary_account) }

  render_views

  describe "#create" do
    it "allows photos param" do
      contract_template = create(:contract_template, project: project)

      post :create, params: { account_id: user.primary_account.to_param, project_id: project, contract_template_id: contract_template, location_release: location_release_params_with_photos }

      expect(response).to be_successful
      expect(LocationRelease.last.photos.attached?).to eq true
    end

    it "logs analytics" do
      contract_template = create(:contract_template, project: project)

      expect {
        post :create, params: { account_id: project.account.to_param, project_id: project, contract_template_id: contract_template, location_release: location_release_params }
      }.to(
        have_enqueued_job(TrackAnalyticsJob)
        .with(nil, nil, :track_create_native_release, release_type: "LocationRelease", account: project.account, user_agent: "Rails Testing", user_ip: "0.0.0.0")
      )
    end

    it "displays validation errors" do
      contract_template = create(:contract_template, project: project)
      sign_in(user)

      post :create, params: { account_id: user.primary_account.to_param, project_id: project, contract_template_id: contract_template, location_release: { person_address_city: "Albuquerque", filming_started_on: '02/02/02', filming_ended_on: '01/01/02' } }

      body = CGI.unescape_html(response.body)
      expect(body).to match /Name can't be blank/
      expect(body).to match />can't be blank</
      # expect(body).to match /must be after the filming started on date/
    end

    it "renders a success page" do
      contract_template = create(:contract_template, project: project)

      post :create, params: { account_id: user.primary_account.to_param, project_id: project, contract_template_id: contract_template, location_release: location_release_params, filming_started_on: '01/02/20', filming_ended_on: '03/18/20'  }

      expect(response).to be_successful
    end

    it "runs set tags for releasable job" do
      contract_template = create(:contract_template, project: project)

      expect {
        post :create, params: { account_id: project.account.to_param, project_id: project, contract_template_id: contract_template, location_release: location_release_params }
      }.to(
        have_enqueued_job(SetTagsForReleasableJob)
        .with(LocationRelease.last)
      )
    end

    it "runs attach contract to releasable job" do
      contract_template = create(:contract_template, project: project)

      expect {
        post :create, params: { account_id: project.account.to_param, project_id: project, contract_template_id: contract_template, location_release: location_release_params }
      }.to(
        have_enqueued_job(AttachContractToReleasableJob)
        .with(LocationRelease.last)
      )
    end
  end

  private

  def location_release_params
    attributes_for(:location_release, :native).except(:signature).merge(signature_param).merge(person_address_params)
  end

  def location_release_params_with_photos
    attributes_for(:location_release, :native, :with_photo).except(:signature).merge(signature_param)
  end

  def person_address_params
    {
      person_address_street1: "123 Broadway",
      person_address_city: "New York",
      person_address_state: "NY",
      person_address_zip: "10001",
      person_address_country: "United States"
    }
  end

  def signature_param
    file = file_fixture("signature.png")
    data_uri = Base64Image.from_image(file).data_uri

    { signature_base64: data_uri }
  end
end
