# frozen_string_literal: true

# Controller for ThemesReport
class ThemesReportsController < ApplicationController
  before_action :set_sections_report
  before_action :set_ancestors

  def report
    @respondents = @themes_report.respondents.includes(:tags).order(:first_name)

    master_sections = MasterSection.where(id: @themes_report.section_ids)
                                   .order(:position)
                                   .pluck(:id)

    @sections = @themes_report.respondents.joins(:transcript_sections)
                              .where(transcript_sections: { master_section_id: master_sections })
                              .select("transcript_sections.name, transcript_sections.master_section_id")
                              .group("transcript_sections.name, transcript_sections.master_section_id")
                              .order(Arel.sql("array_position(ARRAY[#{master_sections.join(',')}], transcript_sections.master_section_id)"))
                              .pluck("transcript_sections.name", "transcript_sections.master_section_id")

    @highlight_themes = @themes_report.respondent_category.highlight_themes
    @respondents_with_highlights = @themes_report.respondents.distinct
                                                 .joins(:transcript_highlights)
                                                 .where.not(transcript_highlights: { master_section_id: nil })
                                                 .where(transcript_highlights: {
                                                          highlight_theme_id: @highlight_themes.select(:id)
                                                        })
                                                 .order(:first_name)

    @respondents_with_highlights_raw = @themes_report.respondents.distinct
                                                     .joins(:transcript_highlights)
                                                     .includes(:tags)
                                                     .where(transcript_highlights: {
                                                              master_section_id: nil,
                                                              highlight_theme_id: @highlight_themes.select(:id)
                                                            })
                                                     .order(:first_name)

    @respondents_with_highlights_unthemed = @themes_report.respondents.distinct
                                                          .joins(:transcript_highlights)
                                                          .includes(:tags)
                                                          .where.not(transcript_highlights: { master_section_id: nil })
                                                          .where(transcript_highlights: { highlight_theme_id: nil })
                                                          .order(:first_name)

    sections_with_unthemed_highlights = master_sections & TranscriptHighlight.where(highlight_theme_id: nil)
                                                                             .where.not(master_section_id: nil)
                                                                             .pluck(:master_section_id)

    sections_with_highlights = master_sections & TranscriptHighlight.where(highlight_theme: @highlight_themes)
                                                                    .where.not(master_section_id: nil)
                                                                    .pluck(:master_section_id)

    @raw_highlight_data = @highlight_themes.order(:position).map do |theme|
      highlights_by_respondent = @respondents_with_highlights_raw.map do |respondent|
        highlights = respondent.transcript_highlights
                               .includes(:highlight_theme)
                               .where(
                                 highlight_theme: theme,
                                 master_section_id: nil
                               )
                               .order(created_at: :asc)

        next if highlights.blank?

        {
          respondent_id: respondent.id,
          highlights:
        }
      end.compact # Remove nil entries

      # Only include theme if it has any highlights
      next unless highlights_by_respondent.any?

      {
        theme:,
        highlights_by_respondent:
      }
    end.compact # Remove nil entries

    @highlight_table_data = MasterSection.where(id: sections_with_highlights).order(:position).map do |section|
      theme_data = @highlight_themes.order(:position).map do |theme|
        highlights_by_respondent = @respondents_with_highlights.map do |respondent|
          highlights = respondent.transcript_highlights
                                 .includes(:highlight_theme, :master_section)
                                 .where(
                                   highlight_theme: theme,
                                   master_section: section
                                 )
                                 .order(created_at: :asc)

          next if highlights.blank?

          {
            respondent_id: respondent.id,
            highlights:
          }
        end.compact # Remove nil entries

        # Only include theme data if there are any highlights
        next unless highlights_by_respondent.any?

        {
          theme:,
          highlights_by_respondent:
        }
      end.compact # Remove nil entries

      # Only include section if it has any themes with highlights
      next unless theme_data.any?

      {
        section:,
        themes_data: theme_data
      }
    end.compact # Remove nil entries

    @unthemed_highlight_table_data = MasterSection.where(id: sections_with_unthemed_highlights)
                                                  .order(:position).map do |section|
      highlights_by_respondent = @respondents_with_highlights_unthemed.map do |respondent|
        highlights = respondent.transcript_highlights
                               .includes(:master_section)
                               .where(
                                 highlight_theme_id: nil,
                                 master_section: section
                               )
                               .order(created_at: :asc)

        next if highlights.blank?

        {
          respondent_id: respondent.id,
          highlights:
        }
      end.compact # Remove nil entries

      # Only include section if it has any unthemed highlights
      next unless highlights_by_respondent.any?

      {
        section:,
        highlights_by_respondent:
      }
    end.compact # Remove nil entries
  end

  def delete_report
    report = ThemesReport.find(params[:id])
    report&.destroy
    flash[:notice] = "Theme Report Successfully Deleted"
    redirect_to theme_reports_project_respondent_category_path(@project, @respondent_category)
  end

  private

  def set_ancestors
    @project = Project.find(params[:project_id])
    @respondent_category = RespondentCategory.find(params[:respondent_category_id])
  end

  # Use callbacks to share common setup or constraints between actions.
  def set_sections_report
    @themes_report = ThemesReport.find(params[:id])
  end
end
