class PreviewController < ApplicationController
  before_action :require_member!

  def title
    return render_bad_request('URL は必須です．') if params[:url].blank?

    render json: { title: Preview::ThumbnailFetcher.title(params[:url]) }
  rescue Preview::UrlSafety::UnsafeUrl => e
    render_bad_request(e.message)
  rescue Preview::HttpFetcher::FetchTimeout => e
    render_preview_error(e.message, :gateway_timeout)
  rescue Preview::HttpFetcher::ResponseTooLarge => e
    render_preview_error(e.message, :payload_too_large)
  rescue Preview::HttpFetcher::FetchFailed => e
    render_preview_error(e.message, :bad_gateway)
  end

  def thumbnail
    return render_bad_request('URL は必須です．') if params[:url].blank?

    attachment =
        Post.resized_thumbnail_attachment(
            StringIO.new(Preview::ThumbnailFetcher.fetch(params[:url])))
    send_data attachment[:io].read,
              type: attachment[:content_type],
              disposition: 'inline'
  rescue Preview::UrlSafety::UnsafeUrl => e
    render_bad_request(e.message)
  rescue Preview::HttpFetcher::FetchTimeout => e
    render_preview_error(e.message, :gateway_timeout)
  rescue Preview::HttpFetcher::ResponseTooLarge => e
    render_preview_error(e.message, :payload_too_large)
  rescue Preview::HttpFetcher::FetchFailed => e
    render_preview_error(e.message, :bad_gateway)
  rescue Preview::ThumbnailFetcher::GenerationFailed, MiniMagick::Error => e
    render_unprocessable_entity(e.message)
  end

  private

  def require_member!
    return head :unauthorized unless current_user
    return if current_user.gte_member?

    head :forbidden
  end

  def render_preview_error(message, status)
    render json: { type: status.to_s,
                   message:,
                   errors: { },
                   base_errors: [message] },
           status:
  end
end
