/**
 * Water Log — session constants + shared types.
 *
 * This file is **safe to import from client components** — it has
 * no server-only side effects (no `next/headers`, no `cookies()`).
 * It just exports the cookie name strings and the public session
 * result types so both client and server code can refer to them.
 *
 * The actual cookie I/O helpers (`setSessionCookie`,
 * `clearSessionCookie`, `readSessionCookie`) live in
 * `./session-server.ts` because they call `cookies()` from
 * `next/headers`, which is server-only.
 *
 * Two session cookies live in this module's surface area:
 *
 *   - `wl_session`        — DB-backed row in `water_sessions`. Set by
 *                           `verifyWaterPin` on a successful irrigator
 *                           PIN. Server-side expiry is enforced by
 *                           `requireFieldSession`.
 *
 *   - `wl_admin_session`  — cookie-only, no DB row. Set by
 *                           `verifyAdminPin` on a successful admin PIN.
 *                           Cookie expiry (`maxAge`) is the source of
 *                           truth for session lifetime.
 */

export const WL_SESSION_COOKIE = "wl_session";
export const WL_ADMIN_SESSION_COOKIE = "wl_admin_session";

export type SessionCookieName =
  | typeof WL_SESSION_COOKIE
  | typeof WL_ADMIN_SESSION_COOKIE;

/**
 * `FieldSession` is the discriminated union result of `requireFieldSession()`.
 * Use `FieldSessionAuthed` (the `ok: true` branch) when you've already
 * narrowed via the `ok` discriminator. The legacy shape — where
 * `FieldSession` itself was the union — is preserved here for callers
 * that imported it from `actions/water-log/auth.ts`.
 */
export type FieldSessionError = { ok: false; error: string };

export type FieldSession = FieldSessionAuthed | FieldSessionError;

/** Narrowed success-branch type, useful for parameter types after a guard. */
export type FieldSessionAuthed = {
  ok: true;
  userId: string;
  brandId: string;
  role: "irrigator" | "water_admin";
};

/** Back-compat alias for the union. Identical to `FieldSession`. */
export type FieldSessionResult = FieldSession;

/** Admin session (cookie-only bearer token). */
export type AdminSession = {
  sessionId: string;
  brandId: string;
};