defmodule Web.Router do
  use Web, :router

  import Web.AdminAuth
  import Web.Globals

  alias Web.AdminAuth
  alias Web.Globals

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_live_flash
    plug :put_root_layout, html: {Web.Layouts, :root}
    plug :protect_from_forgery
    plug :put_secure_browser_headers
    plug :assign_globals
  end

  pipeline :supports_admin_action do
    plug :mount_admin
  end

  pipeline :requires_admin do
    plug :mount_admin
    plug :require_admin
  end

  live_session :default, on_mount: [AdminAuth, Globals] do
    scope "/", Web do
      pipe_through :browser
      pipe_through :supports_admin_action

      get "/", PageController, :home

      get "/writing", PostController, :index
      get "/writing/:post_id", PostController, :show

      get "/microblog", StatusController, :index
      get "/microblog/:status_id", StatusController, :show

      live "/sign-in", AdminLoginLive
      post "/admin/session/create", AdminSessionController, :create
      get "/admin/session/destroy", AdminSessionController, :destroy
    end

    scope "/admin", Web do
      pipe_through :browser
      pipe_through :requires_admin

      live "/", AdminLive

      live "/posts/new", PostLive, :new
      live "/posts/:post_id", PostLive, :edit

      live "/statuses/new", StatusLive, :new
      live "/statuses/:status_id", StatusLive, :edit
    end
  end
end