require "rails_helper"

module ExcelReports
  module AudioReports
    module NatGeoOriginalMusicLogs
      RSpec.describe MainSheet do
        let(:workbook) { instance_double(Axlsx::Workbook, :workbook) }

        subject { described_class.new(workbook) }
  
        it_behaves_like "a worksheet"

        describe "#title" do
          it "returns the title of the sheet" do
            expect(subject.title).to eq "Nat Geo Original Music Log"
          end
        end

        describe "#fill_content" do
          it "adds headers and data to given sheet for both types of audio confirmations" do
            sheet = instance_double(Axlsx::Worksheet)
            allow(sheet).to receive(:add_row)
            allow(BigMediaTime).to receive(:time_zone_now).and_return(DateTime.new(2019,10,9))

            video = create(:video)
            header_data = video
            data = [
              AudioConfirmationData.new(
                build(:audio_confirmation,
                      :original,
                      title: "title_1",
                      composer_info: "composer1a, composer1b, affiliation, 50%, $cae:cae123|composer2, affiliation, 50%, $cae:cae456",
                      publisher_info: "publisher1, affiliation, 50%|publisher2, affiliation, 50%",
                      source_file_name: "source_file_name_1",
                     )
              ),
            ]

            described_class.new(workbook, data, header_data).fill_content(sheet)

            expect(sheet).to have_received(:add_row).with([
              "10/09/19", "", "", ""
            ])
            expect(sheet).to have_received(:add_row).with([
              "CUE TITLE",
              "COMPOSER(S), % SPLIT & PRO",
              "COMPOSER CAE/IPI#",
              "PUBLISHER(S), % SPLIT & PRO",
            ])
            expect(sheet).to have_received(:add_row).with([
              "title_1 - source_file_name_1",
              "composer1a, composer1b (affiliation) 50.0%\ncomposer2 (affiliation) 50.0%",
              "cae123\ncae456",
              "publisher1 (affiliation) 50.0%\npublisher2 (affiliation) 50.0%",
            ])
          end
        end

        describe "#format" do
          it "sets column widths and merge cells" do
            sheet = instance_double(Axlsx::Worksheet)
            allow(sheet).to receive(:column_widths)
            allow(sheet).to receive(:merge_cells)

            subject.format(sheet)

            expect(sheet).to have_received(:column_widths)
            expect(sheet).to have_received(:merge_cells).at_least(1).times
          end
        end

        describe "#style" do
          it "sets sheet style" do
            sheet = instance_double(Axlsx::Worksheet, :sheet)
            allow(sheet).to receive(:add_style)

            described_class.new(workbook, ["data1", "data2"]).style(sheet)

            expect(sheet).to have_received(:add_style).at_least(1).times
          end
        end
      end
    end
  end
end
