# frozen_string_literal: true

require 'rails_helper'

RSpec.feature 'User manages contract templates', type: :feature do
  let(:current_user) { create(:user, :manager) }
  let(:project) { create(:project, members: current_user, account: current_user.primary_account) }
  let(:project2) { create(:project, members: current_user, account: current_user.primary_account, name: 'New project') }

  before do
    sign_in(current_user)
  end

  scenario 'creating a new release template' do
    visit new_project_contract_template_path(project)

    fill_in 'Name', with: 'My Release Template'
    select 'Appearance Release', from: 'Release type'
    fill_in_trix body_field, with: 'You agree to this release.'
    fill_hidden guardian_clause_field, with: 'Your minor agrees to this release.'
    select 'All', from: 'Applicable Media'
    select 'Other', from: 'Territory'
    fill_in 'Describe other territory', with: 'North America only'
    select 'In perpetuity', from: 'Term'
    select 'None', from: 'Restriction'
    click_on 'Create Release Template'

    expect(page).to have_content('The release template has been created')
  end

  scenario 'preview new talent release template without guardian clause' do
    visit new_project_contract_template_path(project)
    select 'Talent Release', from: 'Release type'

    click_on 'Preview'
    expect(content_type).to eq('application/pdf')
    expect(content_disposition).to include('inline')
    expect(pdf_body).to have_content('PREVIEW ONLY')
    expect(pdf_body).to have_content('Talent Release')
    expect(pdf_body).to have_content('Body text goes here')
    expect(pdf_body).to have_content('Guardian')
  end

  scenario 'preview new talent release template with guardian clause' do
    visit new_project_contract_template_path(project)
    select 'Talent Release', from: 'Release type'
    fill_hidden guardian_clause_field, with: 'Your minor agrees to this release.'

    click_on 'Preview'
    expect(content_type).to eq('application/pdf')
    expect(content_disposition).to include('inline')
    expect(pdf_body).to have_content('PREVIEW ONLY')
    expect(pdf_body).to have_content('Talent Release')
    expect(pdf_body).to have_content('Body text goes here')
    expect(pdf_body).to have_content('Guardian')
    expect(pdf_body).to have_content('Your minor')
  end

  scenario 'preview new appearance release template without guardian clause' do
    visit new_project_contract_template_path(project)
    select 'Appearance Release', from: 'Release type'

    click_on 'Preview'
    expect(content_type).to eq('application/pdf')
    expect(content_disposition).to include('inline')
    expect(pdf_body).to have_content('PREVIEW ONLY')
    expect(pdf_body).to have_content('Appearance Release')
    expect(pdf_body).to have_content('Body text goes here')
    expect(pdf_body).to have_content('Guardian')
  end

  scenario 'preview new appearance release template with guardian clause' do
    visit new_project_contract_template_path(project)
    select 'Appearance Release', from: 'Release type'
    fill_hidden guardian_clause_field, with: 'Your minor agrees to this release.'

    click_on 'Preview'
    expect(content_type).to eq('application/pdf')
    expect(content_disposition).to include('inline')
    expect(pdf_body).to have_content('PREVIEW ONLY')
    expect(pdf_body).to have_content('Appearance Release')
    expect(pdf_body).to have_content('Body text goes here')
    expect(pdf_body).to have_content('Guardian')
    expect(pdf_body).to have_content('Your minor')
  end

  scenario 'different release type generates different pdf' do
    visit new_project_contract_template_path(project)
    select 'Acquired Media Release', from: 'Release type'
    click_on 'Preview'
    expect(content_type).to eq('application/pdf')
    expect(content_disposition).to include('inline')
    expect(pdf_body).to have_content('PREVIEW ONLY')
    expect(pdf_body).to have_content('Acquired Media Release')
    expect(pdf_body).not_to have_content('Guardian')

    visit new_project_contract_template_path(project)
    select 'Location Release', from: 'Release type'
    click_on 'Preview'
    expect(content_type).to eq('application/pdf')
    expect(content_disposition).to include('inline')
    expect(pdf_body).to have_content('PREVIEW ONLY')
    expect(pdf_body).to have_content('Location Release')
    expect(pdf_body).not_to have_content('Guardian')

    visit new_project_contract_template_path(project)
    select 'Material Release', from: 'Release type'
    click_on 'Preview'
    expect(content_type).to eq('application/pdf')
    expect(content_disposition).to include('inline')
    expect(pdf_body).to have_content('PREVIEW ONLY')
    expect(pdf_body).to have_content('Material Release')
    expect(pdf_body).not_to have_content('Guardian')
  end

  context 'preventing creation of release template with wrong fee value' do
    before do
      visit new_project_contract_template_path(project)

      fill_in 'Name', with: 'My Release Template'
      select 'Appearance Release', from: 'Release type'
      fill_in_trix body_field, with: 'You agree to this release.'
      fill_hidden guardian_clause_field, with: 'Your minor agrees to this release.'
      select 'All', from: 'Applicable Media'
      select 'Other', from: 'Territory'
      fill_in 'Describe other territory', with: 'North America only'
      select 'In perpetuity', from: 'Term'
      select 'None', from: 'Restriction'
    end

    scenario 'Should not allow negative fees' do
      fill_in 'Fee', with: '-200'
      click_on 'Create Release Template'
      expect(page).not_to have_content('The release template has been created')
    end

    scenario 'Should not allow fees with more than 9 digits' do
      fill_in 'Fee', with: '9999999999'
      click_on 'Create Release Template'
      expect(page).not_to have_content('The release template has been created')
    end
  end

  scenario 'contract template preview is shown before printing' do
    create(:appearance_release_contract_template, body: 'Contract legal language', project: project)
    visit project_contract_templates_path(project)
    click_on 'Manage'
    expect(page).to have_content('Print')

    click_link 'Print'

    expect(page).to have_content preview_heading
    expect(page).to have_selector 'embed'
  end

  scenario 'printing blank release template' do
    create(:appearance_release_contract_template, body: 'Contract legal language', project: project)
    visit project_contract_templates_path(project)
    click_on 'Manage'
    expect(page).to have_content('Print')

    click_link 'Print'

    fill_in 'Number of copies', with: '-1'
    click_on t('shared.print')
    expect(page).to have_content(t('contract_templates.blank_contracts.create.number_of_copies_invalid_notice'))

    fill_in 'Number of copies', with: 2
    click_on t('shared.print')

    expect(content_type).to eq('application/pdf')
    expect(content_disposition).to include('inline')
    expect(pdf_body).to have_content('Contract legal language', count: 2)
    expect(pdf_body).to have_content('Appearance Release', count: 2)
    expect(pdf_body).to have_content('Name: _____', count: 4) #2 times for person name and 2 times for guardian name
    expect(pdf_body).to have_content(t 'blank_contracts.pdf.do_not_copy_warning', count: 2)
  end

  scenario 'archiving an existing release template', js: true do
    create(:contract_template, project: project)

    visit project_contract_templates_path(project)

    expect(page).to have_content('Test template')

    click_on 'Manage'
    accept_alert do
      click_on 'Archive'
    end

    expect(page).to have_content('The release template has been archived')
    expect(page).not_to have_content('Test template')
  end

  scenario 'archived contract templates from other projects are not shown when importing contract templates' do
    create(:contract_template, :archived, project: project2, name: 'Archived template')
    create(:contract_template, project: project2, name: 'Active template')
    create(:contract_template, project: project)

    visit project_contract_templates_path(project)
    expect(page).to have_content('Test template')

    click_on import_template_button

    expect(page).not_to have_content('Test template')
    expect(page).not_to have_content('Archived template')
    expect(page).to have_content('Active template')
  end

  context 'When the user is associate' do
    let(:current_user) { create(:user, :associate) }

    it 'does not show management buttons for release templates' do
      visit project_contract_templates_path(project)

      expect(page).not_to have_content('Create New Release Template')
      expect(page).not_to have_content(import_template_button)
      expect(page).not_to have_content('Delete')
    end
  end

  context 'When the user is account manager' do
    let(:current_user) { create(:user, :account_manager) }

    it 'shows archive button for release template' do
      create(:contract_template, project: project)

      visit project_contract_templates_path(project)

      click_on 'Manage'
      expect(page).to have_content('Archive')
    end
  end

  private

  def import_template_button
    t 'contract_templates.index.actions.import'
  end

  def import_selected_templates_button
    t 'release_template_imports.new.actions.import'
  end

  def preview_heading
    t 'blank_contracts.new.preview_heading'
  end

  def fill_in_trix(id, with:)
    find("trix-editor##{id}").click.set(with)
  end

  def body_field
    'contract_template_body'
  end

  def guardian_clause_field
    'contract_template_guardian_clause_trix_input_contract_template'
  end

  def fill_hidden(id, with:)
    find(:xpath, "//input[@id='#{id}']", visible: false).set with
  end
end
