class MedicalReleasesController < ApplicationController
  include ProjectContext, MedicalReleaseContext

  before_action :set_project, only: [:index]
  before_action :set_medical_release, only: [:destroy]

  include ProjectLayout

  def index
    @medical_releases = filtered_medical_releases.order_by_recent.paginate(page: params[:page])
  end

  def destroy
    @project = @medical_release.project

    if @medical_release.destroy
      redirect_to [@project, :medical_releases], alert: t(".alert")
    end
  end

  private

  def medical_releases
    if @project
      policy_scope(@project.medical_releases)
    else
      policy_scope(MedicalRelease)
    end
  end

  def filtered_medical_releases
    results = medical_releases

    if params[:query].present?
      results = results.search(params[:query])
    end

    results
  end
end
