defmodule CentralcloudMy.SessionController do
  use Phoenix.Controller, formats: [:html]
  import Plug.Conn

  def new(conn, _params) do
    conn
    |> put_resp_content_type("text/html")
    |> send_resp(200, login_page(conn))
  end

  def create(conn, %{"username" => _username, "password" => _password}) do
    # TODO: validate against DR portal customer_users or Authentik
    conn
    |> put_flash(:error, "Invalid credentials")
    |> redirect(to: "/login")
  end

  def delete(conn, _params) do
    conn
    |> clear_session()
    |> redirect(to: "/login")
  end

  def oidc_callback(conn, params) do
    # TODO: exchange code with Authentik, set session
    _ = params
    conn
    |> put_flash(:info, "SSO login coming soon")
    |> redirect(to: "/login")
  end

  defp login_page(conn) do
    csrf = Phoenix.Controller.get_csrf_token()
    flash_error = Phoenix.Flash.get(conn.assigns[:flash] || %{}, :error)

    """
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="utf-8"/>
      <meta name="viewport" content="width=device-width, initial-scale=1"/>
      <meta name="csrf-token" content="#{csrf}"/>
      <title>Sign in — CentralCloud</title>
      <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
               background: #0f172a; color: #e2e8f0; display: flex;
               align-items: center; justify-content: center; min-height: 100vh; }
        .card { background: #1e293b; border: 1px solid #334155; border-radius: 12px;
                padding: 2.5rem; width: 100%; max-width: 380px; }
        h1 { font-size: 1.4rem; font-weight: 700; color: #38bdf8; margin-bottom: 0.25rem; }
        p.sub { color: #94a3b8; font-size: 0.85rem; margin-bottom: 2rem; }
        label { display: block; font-size: 0.8rem; color: #94a3b8; margin-bottom: 0.3rem; }
        input { width: 100%; padding: 0.6rem 0.8rem; background: #0f172a; border: 1px solid #475569;
                border-radius: 6px; color: #e2e8f0; font-size: 0.9rem; margin-bottom: 1rem; }
        button { width: 100%; padding: 0.7rem; background: #0ea5e9; border: none;
                 border-radius: 6px; color: #fff; font-weight: 600; cursor: pointer; font-size: 0.95rem; }
        button:hover { background: #38bdf8; }
        .divider { text-align: center; color: #475569; font-size: 0.8rem; margin: 1rem 0; }
        .sso { width: 100%; padding: 0.7rem; background: #1e293b; border: 1px solid #475569;
               border-radius: 6px; color: #e2e8f0; font-size: 0.9rem; cursor: pointer; text-align: center;
               text-decoration: none; display: block; margin-top: 0.5rem; }
        .sso:hover { border-color: #38bdf8; }
        .error { background: #7f1d1d; border: 1px solid #dc2626; border-radius: 6px;
                 padding: 0.6rem 0.8rem; font-size: 0.85rem; margin-bottom: 1rem; }
      </style>
    </head>
    <body>
      <div class="card">
        <h1>CentralCloud</h1>
        <p class="sub">Sign in to your account</p>
        #{if flash_error, do: "<div class='error'>#{flash_error}</div>", else: ""}
        <form method="post" action="/login">
          <input type="hidden" name="_csrf_token" value="#{csrf}"/>
          <label>Username</label>
          <input type="text" name="username" autocomplete="username" required/>
          <label>Password</label>
          <input type="password" name="password" autocomplete="current-password" required/>
          <button type="submit">Sign in</button>
        </form>
        <div class="divider">or</div>
        <a class="sso" href="/auth/callback">🔐 Sign in with CentralCloud SSO</a>
      </div>
    </body>
    </html>
    """
  end
end
