require 'set'


class MaterialsController < ApplicationController
  rescue_from MaterialZipExporter::EmptyExportError, with: :render_zip_empty
  rescue_from MaterialZipExporter::DuplicatePathError, with: :render_zip_duplicate_path
  rescue_from MaterialZipExporter::MissingFileError, with: :render_zip_missing_file

  def index
    page = (params[:page].presence || 1).to_i
    limit = (params[:limit].presence || 20).to_i

    page = 1 if page < 1
    limit = 1 if limit < 1

    offset = (page - 1) * limit

    filters = material_index_filters
    tag_graph = material_index_tag_graph(filters)
    q = Material.includes(:material_export_items,
                          thumbnail_attachment: :blob,
                          file_attachment: :blob,
                          tag: :tag_name)
    q = q.where(tag_id: nil) if filters[:tag_state] == 'untagged'
    q = q.where.not(tag_id: nil) if filters[:tag_state] == 'tagged'
    q = apply_material_tag_filter(q, filters, tag_graph)
    q = q.where('materials.created_at >= ?', filters[:created_from]) if filters[:created_from]
    q = q.where('materials.created_at <= ?', filters[:created_to]) if filters[:created_to]
    q = q.where('materials.updated_at >= ?', filters[:updated_from]) if filters[:updated_from]
    q = q.where('materials.updated_at <= ?', filters[:updated_to]) if filters[:updated_to]

    q = material_index_join_tag_name(q) if material_index_needs_tag_name?(filters)
    q = material_index_join_file_blob(q) if material_index_needs_file_blob?(filters)
    q = apply_material_query(q, filters[:q]) if filters[:q].present?
    q = apply_material_media_kind(q, filters[:media_kind])

    count = q.distinct.count(:id)
    materials =
        q
        .order(Arel.sql(material_index_order_sql(filters)))
        .limit(limit)
        .offset(offset)
        .to_a

    response = { materials: MaterialRepr.list_many(materials, host: request.base_url),
                 count: }
    if filters[:tag_id]
      response[:tag_scope] = material_index_tag_scope(filters, tag_graph)
    end
    if filters[:group_by] == 'parent_tag'
      response[:groups] = material_index_groups(materials, tag_graph)
    end

    render json: response
  end

  def show
    material =
        Material
        .includes(:tag, :material_export_items)
        .with_attached_thumbnail
        .with_attached_file
        .find_by(id: params[:id])
    return head :not_found unless material

    wiki_page_body = material.tag&.tag_name&.wiki_page&.current_revision&.body

    render json: MaterialRepr.base(material, host: request.base_url).merge(wiki_page_body:)
  end

  def create
    return head :unauthorized unless current_user
    return head :forbidden unless current_user.gte_member?

    tag_name_raw = params[:tag].to_s.strip
    file = params[:file]
    file_sha256 = MaterialFileSha256.from_upload(file)
    url = params[:url].to_s.strip.presence
    return render_unprocessable_entity('タグは必須です．', field: :tag) if tag_name_raw.blank?
    if file.blank? && url.blank?
      return render_validation_error fields: { file: ['ファイルまたは URL は必須です．'],
                                               url: ['ファイルまたは URL は必須です．'] }
    end
    block = MaterialImportBlockMatcher.match_for_sha256(file_sha256)
    return render_material_import_block(block) if block

    uploaded_blob = build_uploaded_material_blob!(file, file_sha256)
    material = nil

    begin
      Material.transaction do
        tag = resolve_material_tag!(tag_name_raw)
        material = Material.new(tag:, url:,
                                created_by_user: current_user,
                                updated_by_user: current_user)
        material.file.attach(uploaded_blob) if uploaded_blob
        material.save!
        TagVersioning.record_tag_snapshot!(tag, created_by_user: current_user)
        upsert_export_paths!(material)
        MaterialVersionRecorder.record!(material:, event_type: :create,
                                        created_by_user: current_user)
      end
    rescue StandardError
      uploaded_blob&.purge_later
      raise
    end

    if material
      log_thumbnail_generation(material, MaterialThumbnailGenerator.generate!(material))
      render json: MaterialRepr.base(material, host: request.base_url), status: :created
    else
      render_validation_error material
    end
  end

  def update
    return head :unauthorized unless current_user
    return head :forbidden unless current_user.gte_member?

    material = Material.with_attached_file.find_by(id: params[:id])
    return head :not_found unless material

    tag_name_raw = params[:tag].to_s.strip
    file = params[:file]
    file_sha256 = MaterialFileSha256.from_upload(file)
    url = params.key?(:url) ? params[:url].to_s.strip.presence : material.url
    return render_unprocessable_entity('タグは必須です．', field: :tag) if tag_name_raw.blank?
    if file.blank? && url.blank? && !material.file.attached?
      return render_validation_error fields: { file: ['ファイルまたは URL は必須です．'],
                                               url: ['ファイルまたは URL は必須です．'] }
    end
    block = MaterialImportBlockMatcher.match_for_sha256(file_sha256)
    return render_material_import_block(block) if block

    uploaded_blob = build_uploaded_material_blob!(file, file_sha256)

    begin
      Material.transaction do
        MaterialVersionRecorder.ensure_snapshot!(material, created_by_user: current_user)
        tag = resolve_material_tag!(tag_name_raw)
        material.assign_attributes(tag:, url:, updated_by_user: current_user)
        if uploaded_blob
          material.file.attach(uploaded_blob)
        elsif params.key?(:url) && url.present? && file.blank?
          material.file.detach
        end
        material.save!
        TagVersioning.record_tag_snapshot!(tag, created_by_user: current_user)
        upsert_export_paths!(material)
        MaterialVersionRecorder.record!(material:, event_type: :update,
                                        created_by_user: current_user)
      end
    rescue StandardError
      uploaded_blob&.purge_later
      raise
    end

    log_thumbnail_generation(material, MaterialThumbnailGenerator.generate!(material))

    render json: MaterialRepr.base(material, host: request.base_url)
  end

  def destroy
    return head :unauthorized unless current_user
    return head :forbidden unless current_user.gte_member?

    material = Material.find_by(id: params[:id])
    return head :not_found unless material

    Material.transaction do
      MaterialVersionRecorder.ensure_snapshot!(material, created_by_user: current_user)
      material.discard!
      MaterialVersionRecorder.record!(material:, event_type: :discard,
                                      created_by_user: current_user)
    end
    head :no_content
  end

  def download
    zip = MaterialZipExporter.new(profile: params[:profile],
                                  tag_id: params[:tag_id]).export
    profile = params[:profile].presence || 'legacy_drive'
    send_data zip,
              type: 'application/zip',
              disposition: 'attachment',
              filename: "btrc-materials-#{ profile }.zip"
  end

  private

  def material_index_filters
    tag_state = params[:tag_state].to_s.presence
    tag_state = 'untagged' if bool?(:unclassified)
    tag_state = 'all' unless ['all', 'tagged', 'untagged'].include?(tag_state)

    media_kind = params[:media_kind].to_s.presence
    unless ['all', 'image', 'video', 'audio', 'file_other', 'url_only'].include?(media_kind)
      media_kind = 'all'
    end

    sort = params[:sort].to_s.presence
    unless ['created_at', 'updated_at', 'tag_name', 'media_kind', 'file_byte_size',
            'version_no', 'id'].include?(sort)
      sort = 'created_at'
    end

    direction = params[:direction].to_s.downcase
    direction = 'desc' unless ['asc', 'desc'].include?(direction)

    group_by = params[:group_by].to_s.presence
    group_by = 'none' unless ['none', 'parent_tag'].include?(group_by)

    tag_id = params[:tag_id].to_i
    tag_id = nil if tag_id <= 0

    { q: params[:q].to_s.strip.presence,
      tag_state:,
      media_kind:,
      tag_id:,
      include_descendants: bool?(:include_descendants),
      group_by:,
      created_from: parse_time_param(:created_from),
      created_to: parse_time_param(:created_to),
      updated_from: parse_time_param(:updated_from),
      updated_to: parse_time_param(:updated_to),
      sort:,
      direction: }
  end

  def parse_time_param name
    value = params[name].to_s.strip
    return nil if value.blank?

    Time.zone.parse(value)
  rescue ArgumentError
    nil
  end

  def resolve_material_tag! tag_name_raw
    tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
    tag = tag_name.tag
    tag || Tag.create!(tag_name:, category: :material)
  end

  def material_index_needs_tag_name? filters
    filters[:q].present? || filters[:sort] == 'tag_name'
  end

  def material_index_needs_file_blob? filters
    filters[:q].present? ||
        filters[:media_kind] != 'all' ||
        ['media_kind', 'file_byte_size'].include?(filters[:sort])
  end

  def material_index_join_tag_name q
    q.left_joins(tag: :tag_name)
  end

  def material_index_join_file_blob q
    q.joins(<<~SQL.squish)
      LEFT JOIN active_storage_attachments material_file_attachments
        ON material_file_attachments.record_type = 'Material'
       AND material_file_attachments.record_id = materials.id
       AND material_file_attachments.name = 'file'
      LEFT JOIN active_storage_blobs material_file_blobs
        ON material_file_blobs.id = material_file_attachments.blob_id
    SQL
  end

  def apply_material_query q, term
    like = "%#{ ActiveRecord::Base.sanitize_sql_like(term) }%"
    q.where('tag_names.name LIKE :q OR materials.url LIKE :q OR ' \
            'material_file_blobs.filename LIKE :q',
            q: like)
  end

  def apply_material_media_kind q, media_kind
    case media_kind
    when 'image'
      q.where('material_file_blobs.content_type LIKE ?', 'image/%')
    when 'video'
      q.where('material_file_blobs.content_type LIKE ?', 'video/%')
    when 'audio'
      q.where('material_file_blobs.content_type LIKE ?', 'audio/%')
    when 'file_other'
      q.where('material_file_attachments.id IS NOT NULL')
       .where.not('material_file_blobs.content_type LIKE ?', 'image/%')
       .where.not('material_file_blobs.content_type LIKE ?', 'video/%')
       .where.not('material_file_blobs.content_type LIKE ?', 'audio/%')
    when 'url_only'
      q.where('material_file_attachments.id IS NULL').where.not(url: [nil, ''])
    else
      q
    end
  end

  def apply_material_tag_filter q, filters, tag_graph
    return q unless filters[:tag_id]

    tag_ids =
        if tag_graph
          tag_graph[:scope_tag_ids]
        else
          [filters[:tag_id]]
        end

    q.where(tag_id: tag_ids)
  end

  def material_index_tag_graph filters
    return nil unless filters[:tag_id]

    children_by_parent_id = Hash.new { |h, k| h[k] = [] }
    TagImplication.pluck(:parent_tag_id, :tag_id).each do |parent_id, child_id|
      children_by_parent_id[parent_id] << child_id
    end

    scope_tag_ids =
        if filters[:include_descendants]
          collect_material_descendant_ids(filters[:tag_id], children_by_parent_id)
        else
          Set.new([filters[:tag_id]])
        end
    tags_by_id =
        Tag
        .joins(:tag_name)
        .where(id: scope_tag_ids)
        .pluck('tags.id', 'tag_names.name', 'tags.category', 'tags.deprecated_at')
        .each_with_object({ }) do |(id, name, category, deprecated_at), hash|
          hash[id] = { id:, name:, category:, deprecated: deprecated_at.present? }
        end

    selected_tag = tags_by_id[filters[:tag_id]]
    return nil unless selected_tag

    group_tag_id_by_tag_id = { }
    children_by_parent_id[filters[:tag_id]].each do |child_tag_id|
      assign_material_group_branch(child_tag_id,
                                   children_by_parent_id:,
                                   tags_by_id:,
                                   group_tag_id_by_tag_id:,
                                   current_group_tag_id: nil,
                                   seen: Set.new([filters[:tag_id]]))
    end

    { selected_tag_id: filters[:tag_id],
      selected_tag:,
      scope_tag_ids: scope_tag_ids.to_a,
      tags_by_id:,
      group_tag_id_by_tag_id: }
  end

  def collect_material_descendant_ids selected_tag_id, children_by_parent_id
    ids = Set.new([selected_tag_id])
    stack = [selected_tag_id]

    until stack.empty?
      tag_id = stack.pop
      children_by_parent_id[tag_id].each do |child_tag_id|
        next if ids.include?(child_tag_id)

        ids << child_tag_id
        stack << child_tag_id
      end
    end

    ids
  end

  def assign_material_group_branch tag_id, children_by_parent_id:, tags_by_id:,
                                   group_tag_id_by_tag_id:, current_group_tag_id:, seen:
    return if seen.include?(tag_id)

    tag = tags_by_id[tag_id]
    return unless tag

    seen = seen.dup << tag_id
    next_group_tag_id = current_group_tag_id
    next_group_tag_id = tag_id if next_group_tag_id.nil? && !tag[:deprecated]
    group_tag_id_by_tag_id[tag_id] = next_group_tag_id if next_group_tag_id

    children_by_parent_id[tag_id].each do |child_tag_id|
      assign_material_group_branch(child_tag_id,
                                   children_by_parent_id:,
                                   tags_by_id:,
                                   group_tag_id_by_tag_id:,
                                   current_group_tag_id: next_group_tag_id,
                                   seen:)
    end
  end

  def material_index_groups materials, tag_graph
    return [] unless tag_graph

    groups_by_key = { }

    materials.each do |material|
      next unless material.tag_id

      group_tag_id =
          if material.tag_id == tag_graph[:selected_tag_id]
            tag_graph[:selected_tag_id]
          else
            tag_graph[:group_tag_id_by_tag_id][material.tag_id] || tag_graph[:selected_tag_id]
          end

      group_tag = tag_graph[:tags_by_id][group_tag_id]
      next unless group_tag

      key = "tag:#{ group_tag_id }"
      group =
          groups_by_key[key] ||= { key:,
                                   tag: group_tag_repr(group_tag),
                                   material_ids: [],
                                   count: 0 }
      group[:material_ids] << material.id
      group[:count] += 1
    end

    groups_by_key.values.sort_by { |group| group[:tag][:name] }
  end

  def material_index_tag_scope filters, tag_graph
    return nil unless tag_graph

    {
      tag: group_tag_repr(tag_graph[:selected_tag]),
      include_descendants: filters[:include_descendants]
    }
  end

  def group_tag_repr group_tag
    { id: group_tag[:id],
      name: group_tag[:name],
      category: group_tag[:category] }
  end

  def material_index_order_sql filters
    direction = filters[:direction] == 'asc' ? 'ASC' : 'DESC'
    sort_sql =
        case filters[:sort]
        when 'tag_name'
          'tag_names.name'
        when 'media_kind'
          material_media_kind_sql
        when 'file_byte_size'
          'material_file_blobs.byte_size'
        when 'updated_at'
          'materials.updated_at'
        when 'version_no'
          'materials.version_no'
        when 'id'
          'materials.id'
        else
          'materials.created_at'
        end

    "#{ sort_sql } #{ direction }, materials.id #{ direction }"
  end

  def material_media_kind_sql
    "CASE " \
      "WHEN material_file_attachments.id IS NULL AND materials.url IS NOT NULL THEN 5 " \
      "WHEN material_file_blobs.content_type LIKE 'image/%' THEN 1 " \
      "WHEN material_file_blobs.content_type LIKE 'video/%' THEN 2 " \
      "WHEN material_file_blobs.content_type LIKE 'audio/%' THEN 3 " \
      "WHEN material_file_attachments.id IS NOT NULL THEN 4 " \
      "ELSE 6 END"
  end

  def upsert_export_paths! material
    raw = params[:export_paths]
    return if raw.blank?

    export_paths = raw.respond_to?(:to_unsafe_h) ? raw.to_unsafe_h : raw.to_h
    export_paths.each do |profile, export_path|
      profile = profile.to_s
      export_path = export_path.to_s.strip
      item = material.material_export_items.find_or_initialize_by(profile:)

      if export_path.blank?
        item.destroy! if item.persisted?
        next
      end

      item.export_path = export_path
      item.enabled = true
      item.created_by_user ||= current_user
      item.save!
    end
  end

  def render_zip_empty
    render_unprocessable_entity('ZIP export 対象の素材がありません．')
  end

  def render_zip_duplicate_path error
    render_unprocessable_entity("ZIP export path が重複してゐます: #{ error.message }")
  end

  def render_zip_missing_file error
    missing_files = error.missing_files.map do |missing_file|
      { material_id: missing_file.material_id,
        export_path: missing_file.export_path,
        blob_id: missing_file.blob_id,
        filename: missing_file.filename }
    end

    render json: { type: 'validation_error',
                   message: 'ZIP export に必要な素材ファイルが欠損しています．',
                   errors: { },
                   base_errors: ['ZIP export に必要な素材ファイルが欠損しています．'],
                   missing_files: },
           status: :unprocessable_entity
  end

  def build_uploaded_material_blob! file, file_sha256
    return nil unless file

    file.tempfile.rewind

    blob = ActiveStorage::Blob.create_and_upload!(
        io: file.tempfile,
        filename: file.original_filename,
        content_type: file.content_type,
    )

    MaterialFileSha256.assign_metadata_sha256!(blob, file_sha256)

    blob
  ensure
    file.tempfile.rewind if file&.tempfile
  end

  def render_material_import_block block
    render_validation_error fields: { file: ["抑止された素材です: #{ block.reason }"] }
  end

  def log_thumbnail_generation material, result
    material.reload
    Rails.logger.info(
        "Material thumbnail generation: material_id=#{ material.id } " \
        "result=#{ result } " \
        "thumbnail_attached=#{ material.thumbnail.attached? } " \
        "content_type=#{ material.content_type }")
  end
end
