/**
 * Theme cookie helper — dark-light-theme (spec 114).
 *
 * Sets (or clears) the ui_theme cookie used by the SSR flash-prevention
 * inline script in the app shell. The cookie must remain script-readable
 * because the boot script resolves the preference from document.cookie
 * before hydration.
 *
 * Cookie spec:
 *   Name:     ui_theme
 *   Value:    'dark' | 'light' | 'system'
 *   Path:     /
 *   HttpOnly: false
 *   SameSite: Strict
 *   Secure:   true in production (always for .zync.is which is HTTPS-only)
 *   MaxAge:   1 year (365 days) — this is a long-lived preference cookie
 */
import type { Context } from 'hono'
import { setCookie } from 'hono/cookie'

export type UiTheme = 'dark' | 'light' | 'system'

const THEME_COOKIE = 'ui_theme'
const ONE_YEAR_SECONDS = 60 * 60 * 24 * 365

/**
 * Emit Set-Cookie: ui_theme={uiTheme}; Path=/; SameSite=Strict; Secure
 * Consumes any Hono Context (does not require AppEnv — reusable from login + PATCH).
 */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function setThemeCookie(c: Context<any>, uiTheme: UiTheme): void {
  setCookie(c, THEME_COOKIE, uiTheme, {
    path: '/',
    secure: true,
    sameSite: 'Strict',
    maxAge: ONE_YEAR_SECONDS,
  })
}
