require "rails_helper"

module ExcelReports
  module VideoReports
    RSpec.describe DiscoveryProductionElementsLog do
      let(:video) { create(:video) }

      describe "#to_xls" do
        it "generates an excel report" do
          expect(described_class.new(video).to_xls).not_to be_nil
        end

        it "builds the necessary worksheets" do
          allow(DiscoveryProductionElementsLogs::MediaRightsCertificationSheet).to receive(:build)
          allow(DiscoveryProductionElementsLogs::AcquiredFootageAndStillsSheet).to receive(:build)
          allow(DiscoveryProductionElementsLogs::MusicSheet).to receive(:build)
          allow(DiscoveryProductionElementsLogs::TalentSheet).to receive(:build)
          allow(DiscoveryProductionElementsLogs::AppearanceSheet).to receive(:build)
          allow(DiscoveryProductionElementsLogs::LocationSheet).to receive(:build)
          allow(DiscoveryProductionElementsLogs::NameProductLogoSheet).to receive(:build)
          allow(DiscoveryProductionElementsLogs::ProductIntegrationSheet).to receive(:build)

          described_class.new(video).to_xls

          expect(DiscoveryProductionElementsLogs::MediaRightsCertificationSheet).to have_received(:build)
          expect(DiscoveryProductionElementsLogs::AcquiredFootageAndStillsSheet).to have_received(:build)
          expect(DiscoveryProductionElementsLogs::MusicSheet).to have_received(:build)
          expect(DiscoveryProductionElementsLogs::TalentSheet).to have_received(:build)
          expect(DiscoveryProductionElementsLogs::AppearanceSheet).to have_received(:build)
          expect(DiscoveryProductionElementsLogs::LocationSheet).to have_received(:build)
          expect(DiscoveryProductionElementsLogs::NameProductLogoSheet).to have_received(:build)
          expect(DiscoveryProductionElementsLogs::ProductIntegrationSheet).to have_received(:build)
        end

        it "sorts the worksheet data by timecode in" do
          confirmations = double(:video_release_confirmations)
          allow(video).to receive(:video_release_confirmations).and_return(confirmations)
          allow(confirmations).to receive(:where).and_return(confirmations)
          allow(confirmations).to receive(:order).and_return([])

          described_class.new(video).to_xls

          expect(video).to have_received(:video_release_confirmations).exactly(6).times
          expect(confirmations).to have_received(:where).exactly(6).times
          expect(confirmations).to have_received(:order).with(timecode_in: :asc).exactly(6).times
        end
      end

      describe "#filename" do
        it "returns video file filename concatenated with production-elements-log and given format" do
          expect(described_class.new(video).filename("csv")).to eq "video_file-mp4_production-elements-log.csv"
        end
      end
    end
  end
end
