# frozen_string_literal: true

# Respondent Category
class RespondentCategory < ApplicationRecord
  has_many :interview_questions, dependent: :destroy
  has_many :respondents, dependent: :destroy
  has_many :quantifiable_questions, dependent: :destroy
  has_many :interview_insights, through: :respondents
  has_many :transcript_sections, through: :respondents
  has_many :section_summaries, as: :groupable, dependent: :destroy
  has_many :analysis_reports, dependent: :destroy
  has_many :insights_reports, dependent: :destroy
  has_many :sections_reports, dependent: :destroy
  has_many :themes_reports, dependent: :destroy
  has_many :master_sections, dependent: :destroy
  has_many :highlight_themes, dependent: :destroy
  has_many :reels, dependent: :destroy
  has_many :manual_summaries, dependent: :destroy
  has_many :research_documents, dependent: :destroy
  belongs_to :project

  validates :name, length: { minimum: 3, maximum: 50 }, presence: true

  def self.reset_all_assistants
    categories = RespondentCategory.where.not(index_name: nil)
    categories.update!(index_name: nil)
    categories.each do |category|
      AssistantConversation.where(raggable: category).update!(outdated: true)
    end
  end

  def ready_for_assistant?
    respondents.count.positive? && respondents.select do |x|
      x.docx_transcript.nil? && x.whisper_transcript.nil?
    end.count.zero?
  end

  def respondents_with_missing_transcripts
    respondents.select do |x|
      x.docx_transcript.nil? && x.whisper_transcript.nil?
    end
  end

  def sibling_categories
    project.respondent_categories.where.not(id:)
  end

  def sibling_categories_with_questions
    RespondentCategory.where(id: sibling_categories.joins(:interview_questions).distinct.select(:id))
  end

  def can_import_questions_from_others?
    !InterviewQuestion.where(respondent_category_id: sibling_categories.select(:id)).take(1).nil?
  end

  def import_questions(path)
    require "csv"
    rows = CSV.read(path)
    rows.each do |row|
      interview_questions.create(question: row[0])
    end
  end

  def total_tags_assigned
    Respondent.joins(:tags)
              .where(respondent_category_id: id)
              .distinct
              .count("tags.id")
  end

  def has_homogenous_language_respondents
    languages = respondents.map do |r|
      r.respondent_type == "transcript" ? r.manual_language : r.language
    end.compact.uniq

    languages.one? && respondents.where(translate: true).count.zero?
  end

  def no_sections_exist?
    respondents.joins(:transcript_sections).count.zero?
  end

  def all_data_report_generated?
    sections_exist = respondents.joins(:transcript_sections).exists?
    queries_exist = interview_questions.exists?

    return false unless sections_exist || queries_exist

    (!sections_exist || all_data_section_report_generated) &&
      (!queries_exist || all_data_insight_report_generated)
  end

  def generating?
    SystemTask.where(
      task_type: %w[SectionReportSummary InsightGeneration InsightsReportSummary],
      running: true,
      status: %w[queued running]
    ).where(
      "metadata @> ?", { respondent_category_id: id, is_all_data: true }.to_json
    ).exists?
  end
end
