# TODO: nest this controller's routes under a podcast's route
module Admin
  class EpisodesController < Admin::AdminController
    before_action :set_episode,      only: %i[show edit update destroy]
    before_action :set_published_at, only: %i[create update]
    before_action :set_podcasts,     only: %i[new edit]

    def index
      redirect_to %i[admin podcasts]
    end

    def show
      @title = admin_title(@episode, %i[title subtitle])
    end

    def new
      @episode = Episode.for_admin.new
      @title = admin_title
    end

    def edit
      @title = admin_title(@episode, %i[id title subtitle])
    end

    def create
      @episode = Episode.for_admin.new episode_params

      if @episode.save
        redirect_to [:admin, @episode], notice: 'Episode was successfully created.'
      else
        render :new
      end
    end

    def update
      if @episode.update(episode_params)
        redirect_to [:admin, @episode], notice: 'Episode was successfully updated.'
      else
        render :edit
      end
    end

    def destroy
      @episode.destroy
      redirect_to %i[admin podcasts], notice: 'Episode was successfully destroyed.'
    end

    private

    def set_podcasts
      @podcasts = Podcast.all
    end

    def set_episode
      @episode = Episode.for_admin.find params[:id]
      redirect_to %i[admin podcasts] if @episode.blank?
    end

    def episode_params
      params.expect episode: %i[
        podcast_id title subtitle image content locale
        audio_mp3_url audio_mp3_file_size audio_ogg_url
        audio_ogg_file_size show_notes transcript audio_length
        duration audio_type tags published_at published_at_tz
        publication_status draft_code
      ]
    end
  end
end
