class Video < ApplicationRecord
  include AASM

  belongs_to :user

  has_many :video_applies, dependent: :destroy
  has_one :processing_video_apply, -> { where(status: :pending) }, class_name: 'VideoApply'

  aasm(:status) do
    state :pending, initial: true
    state :processing
    state :refused
    state :published

    event :apply_publish do
      transitions from: :pending, to: :processing
    end

    event :refuse do
      transitions from: :processing, to: :refused
    end

    event :publish do
      transitions from: :processing, to: :published, guard: :vod_uploaded?
    end
  end

  aasm(:vod_status, namespace: :vod) do
    state :uploading, initial: true
    state :uploaded

    event :upload_success do
      transitions from: :uploading, to: :uploaded
    end
  end
end