defmodule CentralcloudMy.Router do
  use Phoenix.Router
  import Phoenix.LiveView.Router

  @session_opts [store: :cookie, key: "_centralcloud_my_session", signing_salt: "my_salt_2026"]
  def session_opts, do: @session_opts

  pipeline :api do
    plug :accepts, ["json"]
  end

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

  pipeline :auth do
    plug CentralcloudMy.Plugs.RequireAuth
  end

  # Health check — no auth, no CSRF
  scope "/api", CentralcloudMy do
    pipe_through :api
    get "/health", HealthController, :check
  end

  # Public routes
  scope "/", CentralcloudMy do
    pipe_through :browser

    get "/login", SessionController, :new
    post "/login", SessionController, :create
    delete "/logout", SessionController, :delete
    get "/auth/callback", SessionController, :oidc_callback
  end

  # Authenticated customer routes
  scope "/", CentralcloudMy do
    pipe_through [:browser, :auth]

    live "/", DashboardLive, :index
    live "/dr", ReplicationLive, :index
    live "/billing", BillingLive, :index
    live "/security", SecurityLive, :index
    live "/support", SupportLive, :index
  end
end
