-- Better Auth schema (identity + sessions for "Sign in with Grok").
--
-- Generated by the Better Auth CLI for its Postgres adapter — DO NOT EDIT by
-- hand. `@/lib/auth/server` runs Better Auth against these tables when
-- DATABASE_URL is set. The columns are camelCase and MUST stay double-quoted so
-- Postgres preserves the case Better Auth queries by.
--
-- Migrations in this folder are the single source of truth for your schema. They
-- apply to Neon during the Vercel build (`npm run build`) and to the local
-- PGLite fallback automatically on startup, so dev matches production. Applied
-- files are recorded by name in `_migrations` and NEVER run again.
--
-- Put YOUR app's schema in NEW ordered files (0002_*.sql, 0003_*.sql, …), never
-- in this one. For app tables, prefer snake_case and give per-user tables a
-- `user_id TEXT NOT NULL` column (TEXT, not UUID — the preview dev user id is
-- the string 'dev-user'), then scope every query to the authenticated user
-- server-side (see the `neon` + `auth` skills and src/lib/auth/verify.server.ts).

create table if not exists "user" (
  "id" text not null primary key,
  "name" text not null,
  "email" text not null unique,
  "emailVerified" boolean not null,
  "image" text,
  "createdAt" timestamptz default CURRENT_TIMESTAMP not null,
  "updatedAt" timestamptz default CURRENT_TIMESTAMP not null
);

create table if not exists "session" (
  "id" text not null primary key,
  "expiresAt" timestamptz not null,
  "token" text not null unique,
  "createdAt" timestamptz default CURRENT_TIMESTAMP not null,
  "updatedAt" timestamptz not null,
  "ipAddress" text,
  "userAgent" text,
  "userId" text not null references "user" ("id") on delete cascade
);

create table if not exists "account" (
  "id" text not null primary key,
  "accountId" text not null,
  "providerId" text not null,
  "userId" text not null references "user" ("id") on delete cascade,
  "accessToken" text,
  "refreshToken" text,
  "idToken" text,
  "accessTokenExpiresAt" timestamptz,
  "refreshTokenExpiresAt" timestamptz,
  "scope" text,
  "password" text,
  "createdAt" timestamptz default CURRENT_TIMESTAMP not null,
  "updatedAt" timestamptz not null
);

create table if not exists "verification" (
  "id" text not null primary key,
  "identifier" text not null,
  "value" text not null,
  "expiresAt" timestamptz not null,
  "createdAt" timestamptz default CURRENT_TIMESTAMP not null,
  "updatedAt" timestamptz default CURRENT_TIMESTAMP not null
);

create index if not exists "session_userId_idx" on "session" ("userId");
create index if not exists "account_userId_idx" on "account" ("userId");
create index if not exists "verification_identifier_idx" on "verification" ("identifier");
