require "rails_helper"

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

  render_views

  before :each do
    allow(BrayniacAI::Validation).to receive(:create).and_return(double(:validation, valid: true))
  end

  describe "#create" do
    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, appearance_release: { person_email: "email@email.com" } }
      body = CGI.unescape_html(response.body)
      expect(body).to match /Person first name can't be blank/
      expect(body).to match /Person last name can't be blank/
      expect(body).to match /Photo is not attached/
      expect(body).to match />can't be blank</
    end

    it "responds with success " do
      contract_template = create(:contract_template, project: project)

      post :create, params: { account_id: project.account.to_param, project_id: project, contract_template_id: contract_template, appearance_release: appearance_release_params }

      expect(response).to be_successful
    end

    it "responds with success when guardian photo is attached for minor" do
      contract_template = create(:contract_template, project: project)

      post :create, params: { account_id: project.account.to_param, project_id: project, contract_template_id: contract_template, appearance_release: minor_appearance_release_params }

      expect(response).to be_successful
    end

    it "responds with success when guardian photo is not attached for minor" do
      contract_template = create(:contract_template, project: project)

      post :create, params: { account_id: project.account.to_param, project_id: project, contract_template_id: contract_template, appearance_release: minor_appearance_release_params(false) }

      expect(response).to be_successful
    end

    it "runs headshots 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,
          appearance_release: appearance_release_params
        }
      }.to(
        have_enqueued_job(AddHeadshotCollectionUidToProjectJob)
        .with(project)
      )
    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,
          appearance_release: appearance_release_params
        }
      }.to(
        have_enqueued_job(SetTagsForReleasableJob)
        .with(AppearanceRelease.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,
          appearance_release: appearance_release_params
        }
      }.to(
        have_enqueued_job(AttachContractToReleasableJob)
        .with(AppearanceRelease.last)
      )
    end
  end

  private

  def appearance_release_params
    attributes_for(:appearance_release, :native).except(:signature).merge(signature_param)
  end

  def minor_appearance_release_params(with_guardian_photo = true)
    minor_type = with_guardian_photo ? :minor_with_guardian_photo : :minor
    attributes_for(:appearance_release, minor_type)
        .merge(signature_param, guardian_address_params)
  end

  def guardian_address_params
    {
      guardian_address_street1: "St1",
      guardian_address_street2: "St2",
      guardian_address_city: "City",
      guardian_address_state: "State",
      guardian_address_zip: "ZIP",
      guardian_address_country: "Country"
    }
  end

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

    { signature_base64: data_uri }
  end
end
