require "rails_helper"

module ExcelReports
  module AudioReports
    module NatGeoMusicCueSheets
      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 Music Cue Sheet"
          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)

            video = create(:video)
            header_data = video
            data = [
              AudioConfirmationData.new(
                build(:audio_confirmation,
                      :library,
                      timecode_in: "timecode_in_1",
                      timecode_out: "timecode_out_1",
                      title: "title_1",
                      catalog: "catalog_1",
                      duration: "00:02:30",
                      music_type: "music_type_1",
                      music_category: "music_category_1",
                      composer_info: "composer1, affiliation, 50%|composer2, affiliation, 50%",
                      publisher_info: "publisher1, affiliation, 50%|publisher2, affiliation, 50%",
                      source_file_name: "source_file_name_1",
                     )
              ),
              AudioConfirmationData.new(
                build(:audio_confirmation,
                      :original,
                      timecode_in: "timecode_in_2",
                      timecode_out: "timecode_out_2",
                      title: "title_2",
                      catalog: "catalog_2",
                      duration: "00:03:50",
                      music_type: "music_type_2",
                      music_category: "music_category_2",
                      composer_info: "composer1, affiliation, 50%|composer2, affiliation, 50%",
                      publisher_info: "publisher1, affiliation, 50%|publisher2, affiliation, 50%",
                      source_file_name: "source_file_name_2",
                     )
              )
            ]

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

            expect(sheet).to have_received(:add_row).with([
              "No.",
              "Title",
              "In",
              "Out",
              "Composer",
              "Publisher",
              "Record Label / Library",
              "Music Origin",
              "Use",
              "Duration",
            ])
            expect(sheet).to have_received(:add_row).with([
              1,
              "title_1 - source_file_name_1",
              "timecode_in_1",
              "timecode_out_1",
              "composer1 (affiliation) 50.0%\ncomposer2 (affiliation) 50.0%",
              "publisher1 (affiliation) 50.0%\npublisher2 (affiliation) 50.0%",
              "catalog_1",
              "Production Library (Non-affiliated)",
              "music_type_1 music_category_1",
              "00:02:30",
            ])
            expect(sheet).to have_received(:add_row).with([
              2,
              "title_2 - source_file_name_2",
              "timecode_in_2",
              "timecode_out_2",
              "composer1 (affiliation) 50.0%\ncomposer2 (affiliation) 50.0%",
              "publisher1 (affiliation) 50.0%\npublisher2 (affiliation) 50.0%",
              "catalog_2",
              "Commissioned",
              "music_type_2 music_category_2",
              "00:03:50",
            ])
            expect(sheet).to have_received(:add_row).with([
              "", "", "", "", "", "", "", "", "Total Music Duration:", "00:06:20"
            ])
          end
        end

        describe "#format" do
          it "sets column widths and merges 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
