# frozen_string_literal: true

# TranscriptHighlights Controller
class TranscriptHighlightsController < ApplicationController
  def create
    highlight = TranscriptHighlight.new(start: params[:start], end: params[:end], highlight_color: params[:color],
                                        content: params[:content], master_section_id: params[:master_section_id],
                                        user: current_user, respondent_id: params[:respondent_id],
                                        manual: params[:manual], manual_start_line: params[:manual_start_line],
                                        manual_end_line: params[:manual_end_line])
    if highlight.save!
      render json: { status: "ok",
                     newHighlight: highlight.as_json.merge(highlighted_by: highlight.highlighted_by) }
    else
      render json: { status: "error" }
    end
  end

  def update
    highlight = TranscriptHighlight.find(params[:id])
    highlight.update!(start: params[:start], end: params[:end], highlight_color: params[:color],
                      content: params[:content], master_section_id: params[:master_section_id],
                      user: current_user, respondent_id: params[:respondent_id],
                      manual: params[:manual], manual_start_line: params[:manual_start_line],
                      manual_end_line: params[:manual_end_line])

    render json: { status: "ok",
                   newHighlight: highlight.as_json.merge(highlighted_by: highlight.highlighted_by) }
  end

  def update_text
    highlight = TranscriptHighlight.find(params[:id])

    content = params[:content]
    return render json: { error: "Invalid content format" }, status: :unprocessable_entity if content.blank? || !content.is_a?(Array)

    highlight.update!(content:)

    render json: {
      status: "ok",
      updatedHighlight: highlight.as_json(
        include: :highlight_theme,
        methods: %i[reel_available reel_url_unsigned verbose_to_text]
      )
    }
  rescue ActiveRecord::RecordInvalid => e
    Rails.logger.fatal("TranscriptHighlight content update error - #{e}")
    render json: { error: e.message }, status: :unprocessable_entity
  rescue StandardError => e
    Rails.logger.fatal("TranscriptHighlight content update error - #{e}")
    render json: { error: "Failed to update highlight" }, status: :internal_server_error
  end

  def destroy
    highlight = TranscriptHighlight.find(params[:id])
    highlight.destroy!
    render json: { status: "ok" }
  end

  def assign_theme
    highlight = TranscriptHighlight.find(params[:id])
    highlight.highlight_theme_id = params["themeId"]

    if highlight.save!
      render json: {
        status: "ok",
        updated_highlight: highlight
      }
    else
      render json: { status: "error" }
    end
  end

  def queue_reel_creation
    @project = Project.find(params[:project_id])
    @respondent_category = RespondentCategory.find(params[:respondent_category_id])
    CreateReelJob.perform_later(
      metadata: { transcript_highlight_id: params[:id].to_i }, user_id: current_user.id
    )
    # redirect_to reel_creator_project_respondent_category_path(@project, @respondent_category),
    #             notice: "Reel Creation Queued"
    render json: { status: :ok }
  end
end
