class Video < ApplicationRecord
  include PgSearch

  belongs_to :project, touch: true
  has_many :acquired_media_releases, through: :project
  has_many :appearance_releases, through: :project
  has_many :location_releases, through: :project
  has_many :material_releases, through: :project
  has_many :talent_releases, through: :project
  has_many :video_release_confirmations, dependent: :destroy
  has_many :confirmed_acquired_media_releases, through: :video_release_confirmations, source: :releasable, source_type: "AcquiredMediaRelease"
  has_many :confirmed_talent_releases, through: :video_release_confirmations, source: :releasable, source_type: "TalentRelease"
  has_many :confirmed_appearance_releases, through: :video_release_confirmations, source: :releasable, source_type: "AppearanceRelease"
  has_many :confirmed_location_releases, through: :video_release_confirmations, source: :releasable, source_type: "LocationRelease"
  has_many :confirmed_music_releases, through: :video_release_confirmations, source: :releasable, source_type: "MusicRelease"
  has_many :confirmed_material_releases, through: :video_release_confirmations, source: :releasable, source_type: "MaterialRelease"
  has_many :bookmarks, dependent: :destroy
  has_many :unreleased_appearances, dependent: :destroy
  has_many :graphics_elements, dependent: :destroy
  has_many :audio_confirmations, dependent: :destroy

  has_one_attached :file
  has_one_attached :edl_file
  has_one_attached :graphics_only_edl_file
  has_one_attached :audio_only_edl_file

  enum analysis_status: { not_started: 0, pending: 1, success: 2, failure: 3 }, _prefix: "analysis"
  enum audio_analysis_status: { not_started: 0, pending: 1, success: 2, failure: 3 }, _prefix: "audio_analysis"
  enum video_editing_system: { avid: 0, adobe_premiere: 1 }

  delegate :filename, to: :file, allow_nil: true

  validates :file, attached: true, content_type: ["video/mp4", "video/quicktime"]
  validates :edl_file, attached: true, content_type: ["application/octet-stream"]
  validates :graphics_only_edl_file, content_type: ["application/octet-stream"]
  validates :audio_only_edl_file, attached: true, content_type: ["application/octet-stream"], if: -> { adobe_premiere? }

  pg_search_scope :search, {
    against: [:name, :number],
    using: {
      tsearch: {any_word: true, prefix: true},
      trigram: {},
      dmetaphone: {any_word: true}
    }
  }

  scope :video_analysis_started_before, ->(datetime) { where("analysis_started_at < ?", datetime) }
  scope :audio_analysis_started_before, -> (datetime) { where("audio_analysis_started_at < ?", datetime) }
  scope :reports_published, -> { where.not(report_published_at: nil) }
  scope :published_at_desc_order, -> { order("report_published_at desc") }

  def has_confirmed_release?(releasable)
    public_send("confirmed_#{releasable.model_name.plural}").ids.include?(releasable.id)
  end

  def report_published?
    report_published_at.present?
  end

  def report_unpublished?
    !report_published?
  end

  def publish_report!(timestamp = Time.zone.now)
    update!(report_published_at: timestamp)
  end

  def unpublish_report!
    update!(report_published_at: nil)
  end
end
