# frozen_string_literal: true

require 'rails_helper'

describe ContractTemplatePreview do
  describe '#build_releasable' do
    it 'fills missing contract template data' do
      ct = ContractTemplate.new
      ct.release_type = :talent
      preview = ContractTemplatePreview.new(ct)
      expect do
        preview.build_releasable
      end.to change { ct.name }
        .and change { ct.body.to_s }
        .and change { ct.guardian_clause.to_s }
    end

    it 'does not overwrite existing contract template data' do
      ct = build(:contract_template)
      preview = ContractTemplatePreview.new(ct)
      expect do
        preview.build_releasable
      end.to not_change { ct.name }
        .and not_change { ct.body.to_s }
        .and not_change { ct.guardian_clause.to_s }
    end

    describe 'fills releasable with dummy data' do
      it 'with guardian details when guardian clause is present' do
        ct = build(:contract_template)
        preview = ContractTemplatePreview.new(ct)
        releasable = preview.build_releasable
        expect(releasable.attributes).to include(expected_dummy_data)
      end

      it 'with guardian details when guardian clause is not present' do
        ct = ContractTemplate.new
        ct.release_type = :appearance
        preview = ContractTemplatePreview.new(ct)
        releasable = preview.build_releasable
        expect(releasable.attributes).to include(expected_dummy_data)
      end
    end
  end

  private

  def expected_dummy_data
    {
        'id' => nil,
        'person_first_name' => 'Dummy',
        'person_last_name' => 'Person',
        'person_address' => 'Street 1, Street 2, City, State 12345, Country',
        'person_phone' => '00 111 222 333 4444',
        'updated_at' => nil,
        'minor' => true,
        'guardian_address' => 'Street 3, Street 4, City-2, State-2 112233, Country-2',
        "guardian_first_name" => nil,
        "guardian_last_name" => nil,
        "guardian_name_old" => nil,
        'guardian_phone' => '00 123 456 7890',
        'person_email' => 'email@email.com',
        'locale' => nil,
        'tagging_status' => 'pending',
        'contract_template_id' => nil,
        'applicable_medium_id' => nil,
        'applicable_medium_text' => '',
        'territory_id' => nil,
        'territory_text' => '',
        'term_id' => nil,
        'term_text' => '',
        'restriction_id' => nil,
        'restriction_text' => '',
        'signed_at' => nil,
        'internal_tag_list' => nil,
        'tag_list' => nil
    }
  end
end
