# frozen_string_literal: true

# == Schema Information
#
# Table name: screens
#
#  id                     :integer          not null, primary key
#  collaborator           :string
#  component_graph_data   :jsonb
#  conditions             :string
#  deleted_at             :datetime
#  description            :string
#  name                   :string
#  plain_text_description :text
#  requirements           :string
#  result                 :string
#  created_at             :datetime         not null
#  updated_at             :datetime         not null
#
# Indexes
#
#  index_screens_on_deleted_at  (deleted_at)
#
require 'rails_helper'

RSpec.describe Screen, type: :model do
  let(:collection) { create(:collection) }
  let(:wellplate)  { create(:wellplate) }
  let(:research_plan) { create(:research_plan) }
  let(:screen) { create(:screen) }
  let(:sc2) do
    create(:screen, collections: [collection], wellplates: [wellplate], research_plans: [research_plan])
  end

  describe 'creation' do
    it 'is possible to create a valid screen' do
      expect(screen.valid?).to be(true)
    end
  end

  describe 'after creation' do
    it 'has a CodeLog' do
      expect(screen.code_log.value).to match(/\d{40}/)
      expect(screen.code_log.id).to match(
        /^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
      )
    end

    it 'has proper associations' do
      expect(sc2.wellplates.first).to eq(wellplate)
      expect(sc2.collections.first).to eq(collection)
      expect(sc2.research_plans.first).to eq(research_plan)
    end
  end

  describe 'deletion' do
    before do
      sc2.destroy
    end

    it 'destroys associations properly' do
      expect(sc2.collections_screens).to eq []
      expect(sc2.screens_wellplates).to eq []
    end

    it 'only soft deletes screen and associated wellplates' do
      expect(sc2.deleted_at).not_to be_nil
      expect(sc2.screens_wellplates.only_deleted).not_to be_empty
      expect(sc2.wellplates.only_deleted).to eq []
      expect(sc2.collections_screens.only_deleted).not_to be_empty
      expect(sc2.collections.only_deleted).to eq []
    end
  end
end
