# TODO: Delete! NOT used by anything anymore!
module Admin
  class PagesController < Admin::AdminController
    before_action :set_page,         only: %i[show edit update destroy]
    before_action :set_published_at, only: %i[create update]

    after_action :organize_page, only: %i[create update]

    # /admin/pages
    def index
      @pages = Page.all
      @title = admin_title
    end

    # /admin/pages/1
    def show
      @title = admin_title(@page, [:title])
    end

    # /admin/pages/new
    def new
      @page = Page.new
      @title = admin_title
    end

    # /admin/pages/1/edit
    def edit
      @title = admin_title(@page, %i[id title subtitle])
    end

    # /admin/pages
    def create
      @page = Page.new(page_params)

      if @page.save
        redirect_to [:admin, @page], notice: t('.notice')
      else
        render :new
      end
    end

    # /admin/pages/1
    def update
      if @page.update(page_params)
        redirect_to [:admin, @page], notice: t('.notice')
      else
        render :edit
      end
    end

    # /admin/pages/1
    def destroy
      @page.destroy
      redirect_to %i[admin pages], notice: t('.notice')
    end

    private

    def set_page
      @page =
        if params[:draft_code].present?
          Page.find_by(draft_code: params[:draft_code])
        else
          Page.find(params[:id])
        end
    end

    def organize_page
      tag_assigner = TagAssigner.parse_glob(params[:tags])
      tag_assigner.assign_tags_to!(@page) if tag_assigner.present?
    end

    def page_params
      params.expect page: %i[
        year month day css slug tags draft_code locale
        published_at categories published_at_tz publication_status
      ]
    end
  end
end
