class Admin::DownloadsController < Admin::ApplicationController
  before_action :set_download, only: %i[ show edit update destroy ]

  # GET /admin/downloads
  def index
    @pagy, @downloads = pagy(Download.most_recent_first)
  end

  # GET /admin/downloads/1
  def show
  end

  # GET /admin/downloads/new
  def new
    @download = Download.new
  end

  # GET /admin/downloads/1/edit
  def edit
  end

  # POST /admin/downloads
  def create
    @download = Download.new(download_params)

    respond_to do |format|
      if @download.save
        format.html { redirect_to admin_downloads_url, notice: "Download was successfully created." }
      else
        format.html { render :new, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /admin/downloads/1
  def update
    respond_to do |format|
      if @download.update(download_params)
        format.html { redirect_to edit_admin_download_url(@download), notice: "Download was successfully updated." }
      else
        format.html { render :edit, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /admin/downloads/1
  def destroy
    @download.destroy

    respond_to do |format|
      format.html { redirect_to admin_downloads_url, notice: "Download was successfully destroyed." }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_download
      @download = Download.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def download_params
      params.require(:download).permit(:title, :download_url, :version, :size, :release_note, :platform)
    end
end
