# frozen_string_literal: true

#
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

class Thumbnail < ActiveRecord::Base
  belongs_to :attachment, foreign_key: "parent_id", inverse_of: :thumbnails

  # the ":keep_profile => true" part is in here so that we tell mini_magic to not try to pass the command line option -strip.
  # this is because on the servers we are actually using graphics_magic not image_magic's mogrify and graphics_magick doesn't
  # support -strip. you'd get something like:
  # MiniMagick::Error (Command ("mogrify -strip -resize \"200x50\" \"/tmp/mini_magick23816-1\"") failed: {:status_code=>1, :output=>"mogrify: Unrecognized option (-strip).\n"}):#012
  has_attachment(
    content_type: :image,
    storage: (Attachment.local_storage? ? :file_system : :s3),
    path_prefix: Attachment.file_store_config["path_prefix"],
    s3_access: "private",
    keep_profile: true,
    thumbnail_max_image_size_pixels: Setting.get("thumbnail_max_image_size_pixels", 100_000_000).to_i
  )

  before_save :set_namespace

  set_policy do
    given { |user, session| attachment.grants_right?(user, session, :read) }
    can :read

    given { |user, session| attachment.grants_right?(user, session, :download) }
    can :download

    given { |user| attachment.grants_right?(user, :read) }
    can :read

    given { |user| attachment.grants_right?(user, :download) }
    can :download

    given { |user, session| attachment.grants_right?(user, session, :read_as_admin) }
    can :read_as_admin
  end

  def set_namespace
    self.namespace = attachment.namespace
  end

  def local_storage_path(user: nil, ttl: nil, location: nil)
    path = "#{HostUrl.context_host(attachment.context)}/images/thumbnails/show/#{id}"
    if attachment.root_account.feature_enabled?(:file_association_access)
      path = "#{path}?location=#{location}" if location
    else
      path = "#{path}/#{uuid}"
    end
    path
  end

  delegate :bucket, to: :attachment

  def cached_s3_url(options: {})
    opts = options.merge(expires_in: 144.hours)
    @cached_s3_url = authenticated_s3_url(opts)
  end

  before_save :assign_uuid
  def assign_uuid
    self.uuid ||= CanvasSlug.generate_securish_uuid
  end
  protected :assign_uuid
end
