# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Api::TalentReleasesController, type: :controller do
  let(:current_user) { create(:user) }
  let(:project) { create(:project, name: 'first', account: current_user.primary_account) }

  describe '#index' do
    it 'returns a succesful response' do
      create(:talent_release, person_first_name: 'ct1', person_last_name: 'ct12', project_id: project.id)

      sign_in_to_api(current_user)
      get :index, params: { project_id: project.id }

      expect(response).to be_successful
      old_body = response.body

      get :index, params: { project_id: project.id, updated_since: (DateTime.current + 10.days).to_date.to_s }
      expect(response.body).not_to eq(old_body)
    end
  end

  describe '#show' do
    it 'returns a succesful response' do
      tested_release = create(:talent_release, person_first_name: 'ct1', person_last_name: 'ct12', project_id: project.id)

      sign_in_to_api(current_user)
      get :show, params: { id: tested_release.id }

      expect(response).to be_successful
    end

    it 'includes photos' do
          tested_release = create("talent_release_with_photo", person_name: 'Release 1', project: project)

          sign_in_to_api(current_user)
          get :show, params: { id: tested_release.id }

          tested_release.photos.each do |photo|
            data = {
              'id' => photo.id.to_s,
              'type' => 'active_storage_attachment',
              'attributes' => {
                'content_type' => photo.content_type,
                'filename' => photo.filename.to_s,
                'url' => photo_path_for(photo),
                'thumbnail_url' => photo_variant_path_for(photo, '150x150')
              }
            }

            expect(response_body_included_attributes).to include(data)
          end
        end
  end

  describe '#create' do
    before do
      template = create(:contract_template, name: 'ct1', project_id: project.id)
      small_photo = photo_base64
      guardian_photo = guardian_photo_base64
      signature = signature_base64
      @parameters = {
        contract_template_id: template.id,
        talent_release: {
          signature: signature,
          person_phone: '123',
          person_first_name: 'aaa',
          person_last_name: 'bbb',
          minor: true,
          guardian_first_name: 'John',
          guardian_last_name: 'Doe',
          guardian_phone: '9191919191',
          signed_at: '2020-01-01 16:50:26 UTC', 
          photos: [{
            io: small_photo,
            filename: 'filenameee.jpg'
          }]
        }
      }

      @guardian_info = {
          minor: true,
          guardian_name: 'Guardian',
          guardian_phone: '101010'
      }

      @guardian_photo = {
          guardian_photo: {
              io: guardian_photo,
              filename: 'guardianPhoto.jpeg'
          }
      }
      sign_in_to_api(current_user)
    end

    it 'returns 201 - created response' do
      post :create, params: @parameters
      expect(response.status).to eq 201

      expect(TalentRelease.last.signature).to be_attached
      expect(TalentRelease.last.guardian_first_name).to eq('John')
      expect(TalentRelease.last.guardian_last_name).to eq('Doe')
    end

    it 'saves signed on date correctly' do 
      post :create, params: @parameters
      expect(response.status).to eq 201

      expect(TalentRelease.last.signed_on).to eq '01/01/20'
    end

    it 'runs attach contract to background job' do
      expect do
        post :create, params: @parameters
      end.to have_enqueued_job(AttachContractToReleasableJob).with(TalentRelease.last)
    end

    it 'runs add headshot collection uid background job' do
      expect do
        post :create, params: @parameters
      end.to have_enqueued_job(AddHeadshotCollectionUidToProjectJob).with(project)
    end

    it 'runs set tags for background releasable job' do
      expect do
        post :create, params: @parameters
      end.to have_enqueued_job(SetTagsForReleasableJob).with(TalentRelease.last)
    end

    it 'fails if there is no signature' do
      template = create(:contract_template, name: 'ct1', project_id: project.id)
      small_photo = photo_base64
      parameters = {
        contract_template_id: template.id,
        talent_release: {
          person_name: 'aaa bbb'
        }
      }

      parameters[:talent_release][:photos] = [{
        io: small_photo,
        filename: 'filenameee.jpg'
      }]

      sign_in_to_api(current_user)
      expect do
        post :create, params: parameters
      end.to raise_error(ActiveRecord::RecordInvalid)
    end

    it 'returns 201 for minor without guardian photo' do
      params_without_guardian_photo = @parameters
      params_without_guardian_photo[:talent_release].merge!(@guardian_info)

      post :create, params: params_without_guardian_photo
      expect(response.status).to eq 201
      expect(TalentRelease.last.signature).to be_attached
      expect(TalentRelease.last.guardian_photo).not_to be_attached
    end

    it 'returns 201 for minor with guardian photo' do
      params_with_guardian_photo = @parameters
      params_with_guardian_photo[:talent_release].merge!(@guardian_info, @guardian_photo)

      post :create, params: params_with_guardian_photo
      expect(response.status).to eq 201
      talent_release = TalentRelease.last
      photo = talent_release.guardian_photo
      data = {
        'id' => photo.id.to_s,
        'type' => 'active_storage_attachment',
        'attributes' => {
          'content_type' => photo.content_type,
          'filename' => photo.filename.to_s,
          'url' => photo_path_for(photo),
          'thumbnail_url' => photo_variant_path_for(photo, '150x150')
        }
      }
      expect(talent_release.signature).to be_attached
      expect(talent_release.guardian_photo).to be_attached
    end
  end

  private

  def photo_base64
    @photo_base64 ||= Base64Image.from_image(file_fixture('person_photo.png')).data_uri
  end

  def guardian_photo_base64
    @guardian_photo_base64 ||= Base64Image.from_image(file_fixture('pratt.jpg')).data_uri
  end

  def signature_base64
    @signature_base64 ||= Base64Image.from_image(file_fixture('signature.png')).data_uri
  end

  def photo_path_for(attachment)
    Rails.application.routes.url_helpers.rails_blob_url(attachment, host: AppHost.new.domain_with_port)
  end

  def photo_variant_path_for(attachment, _size)
    Rails.application.routes.url_helpers.rails_representation_url(attachment.variant(resize: '150x150'), host: AppHost.new.domain_with_port)
  end

  def response_body_data_attributes
    JSON.parse(response.body).dig('data', 'attributes')
  end

  def response_body_included_attributes
    JSON.parse(response.body).dig('included')
  end
end
