import { sql } from "drizzle-orm";
import type { AppDatabase } from "./client.js";

// Idempotent schema creation. Runs on every boot; safe to run repeatedly.
// Kept as plain SQL so it works identically on node-postgres and PGlite,
// without a separate migration toolchain for this single-instance app.
const STATEMENTS = [
  `CREATE TABLE IF NOT EXISTS users (
    id uuid PRIMARY KEY,
    email text NOT NULL UNIQUE,
    username text UNIQUE,
    password_hash text NOT NULL,
    display_name text NOT NULL,
    avatar_url text,
    created_at bigint NOT NULL,
    updated_at bigint NOT NULL
  )`,
  `CREATE TABLE IF NOT EXISTS refresh_tokens (
    id uuid PRIMARY KEY,
    user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    token_hash text NOT NULL,
    expires_at bigint NOT NULL,
    revoked_at bigint,
    created_at bigint NOT NULL
  )`,
  `CREATE INDEX IF NOT EXISTS refresh_tokens_hash_idx ON refresh_tokens (token_hash)`,
  `CREATE TABLE IF NOT EXISTS locations (
    id uuid PRIMARY KEY,
    owner_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    name text NOT NULL,
    address text,
    created_at bigint NOT NULL,
    updated_at bigint NOT NULL,
    deleted_at bigint
  )`,
  `CREATE INDEX IF NOT EXISTS locations_owner_updated_idx ON locations (owner_id, updated_at)`,
  `CREATE TABLE IF NOT EXISTS players (
    id uuid PRIMARY KEY,
    owner_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    name text NOT NULL,
    avatar text,
    created_at bigint NOT NULL,
    updated_at bigint NOT NULL,
    deleted_at bigint
  )`,
  `CREATE INDEX IF NOT EXISTS players_owner_updated_idx ON players (owner_id, updated_at)`,
  `CREATE TABLE IF NOT EXISTS sessions (
    id uuid PRIMARY KEY,
    owner_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    game_id text NOT NULL,
    date_start bigint NOT NULL,
    date_end bigint,
    players jsonb NOT NULL,
    rounds jsonb NOT NULL,
    status text NOT NULL,
    options jsonb NOT NULL,
    winner_ids jsonb,
    location_id uuid,
    created_at bigint NOT NULL,
    updated_at bigint NOT NULL,
    deleted_at bigint
  )`,
  `CREATE INDEX IF NOT EXISTS sessions_owner_updated_idx ON sessions (owner_id, updated_at)`,
  `CREATE TABLE IF NOT EXISTS friendships (
    id uuid PRIMARY KEY,
    requester_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    addressee_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    status text NOT NULL,
    created_at bigint NOT NULL,
    updated_at bigint NOT NULL
  )`,
  `CREATE INDEX IF NOT EXISTS friendships_requester_idx ON friendships (requester_id)`,
  `CREATE INDEX IF NOT EXISTS friendships_addressee_idx ON friendships (addressee_id)`,
  `CREATE UNIQUE INDEX IF NOT EXISTS friendships_pair_uidx ON friendships (
    LEAST(requester_id, addressee_id), GREATEST(requester_id, addressee_id)
  )`,
];

export async function ensureSchema(db: AppDatabase) {
  for (const statement of STATEMENTS) {
    await db.execute(sql.raw(statement));
  }
}
