require "rails_helper"

module ExcelReports
  module VideoReports
    RSpec.describe NatGeoLegalBinderLog, type: :model do
      let(:video) { create(:video) }

      subject { described_class.new(video) }

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

        it "builds the necessary worksheets" do
          allow(NatGeoLegalBinderLogs::LegalBinderChecklistSheet).to receive(:build)
          allow(NatGeoLegalBinderLogs::AppearanceReleaseLogSheet).to receive(:build)
          allow(NatGeoLegalBinderLogs::LocationReleaseLogSheet).to receive(:build)
          allow(NatGeoLegalBinderLogs::AcquiredFootageLogSheet).to receive(:build)
          allow(NatGeoLegalBinderLogs::ThirdPartyContractLogSheet).to receive(:build)
          allow(NatGeoLegalBinderLogs::ProductionPersonnelLogSheet).to receive(:build)

          described_class.new(video).to_xls

          expect(NatGeoLegalBinderLogs::LegalBinderChecklistSheet).to have_received(:build)
          expect(NatGeoLegalBinderLogs::LocationReleaseLogSheet).to have_received(:build)
          expect(NatGeoLegalBinderLogs::AcquiredFootageLogSheet).to have_received(:build)
          expect(NatGeoLegalBinderLogs::ThirdPartyContractLogSheet).to have_received(:build)
          expect(NatGeoLegalBinderLogs::ProductionPersonnelLogSheet).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(3).times
          expect(confirmations).to have_received(:where).exactly(3).times
          expect(confirmations).to have_received(:order).with(timecode_in: :asc).exactly(3).times
        end
      end

      describe "#filename" do
        it "includes video file filename, report title, and format" do
          expect(subject.filename).to eq "video_file-mp4_legal-binder-log.xlsx"
        end
      end
    end
  end
end
