-- Migrate from custom app_user/session to Better Auth.
-- Drop tables with FKs to app_user, drop app_user, recreate with better-auth schema.

-- Drop tables that reference app_user
DROP TABLE IF EXISTS client_member;
DROP TABLE IF EXISTS session;
DROP TABLE IF EXISTS audit_log;
DROP TABLE IF EXISTS app_user;

-- Better Auth core tables
CREATE TABLE IF NOT EXISTS "user" (
  "id"             TEXT NOT NULL PRIMARY KEY,
  "name"           TEXT NOT NULL,
  "email"          TEXT NOT NULL UNIQUE,
  "email_verified" INTEGER NOT NULL,
  "image"          TEXT,
  "created_at"     TEXT NOT NULL,
  "updated_at"     TEXT NOT NULL,
  "role"           TEXT NOT NULL DEFAULT 'client',
  "locale"         TEXT NOT NULL DEFAULT 'de'
);

CREATE TABLE IF NOT EXISTS "session" (
  "id"          TEXT NOT NULL PRIMARY KEY,
  "expires_at"  TEXT NOT NULL,
  "token"       TEXT NOT NULL UNIQUE,
  "created_at"  TEXT NOT NULL,
  "updated_at"  TEXT NOT NULL,
  "ip_address"  TEXT,
  "user_agent"  TEXT,
  "user_id"     TEXT NOT NULL REFERENCES "user" ("id") ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS "account" (
  "id"                       TEXT NOT NULL PRIMARY KEY,
  "account_id"               TEXT NOT NULL,
  "provider_id"              TEXT NOT NULL,
  "user_id"                  TEXT NOT NULL REFERENCES "user" ("id") ON DELETE CASCADE,
  "access_token"             TEXT,
  "refresh_token"            TEXT,
  "id_token"                 TEXT,
  "access_token_expires_at"  TEXT,
  "refresh_token_expires_at" TEXT,
  "scope"                    TEXT,
  "password"                 TEXT,
  "created_at"               TEXT NOT NULL,
  "updated_at"               TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS "verification" (
  "id"         TEXT NOT NULL PRIMARY KEY,
  "identifier" TEXT NOT NULL,
  "value"      TEXT NOT NULL,
  "expires_at" TEXT NOT NULL,
  "created_at" TEXT NOT NULL,
  "updated_at" TEXT NOT NULL
);

-- Booking-client cookie sessions (kept separate from better-auth sessions)
CREATE TABLE IF NOT EXISTS booking_session (
  session_id  TEXT PRIMARY KEY,
  user_id     TEXT NOT NULL REFERENCES "user" ("id") ON DELETE CASCADE,
  expires_at  TEXT NOT NULL,
  created_at  TEXT NOT NULL DEFAULT (datetime('now'))
);

-- Recreate client_member referencing user(id)
CREATE TABLE IF NOT EXISTS client_member (
  client_id   TEXT NOT NULL REFERENCES client(client_id),
  user_id     TEXT NOT NULL REFERENCES "user" ("id"),
  role        TEXT NOT NULL DEFAULT 'organiser',
  PRIMARY KEY (client_id, user_id)
);

-- Recreate audit_log referencing user(id)
CREATE TABLE IF NOT EXISTS audit_log (
  audit_id    TEXT PRIMARY KEY,
  venue_id    TEXT NOT NULL REFERENCES venue(venue_id),
  user_id     TEXT REFERENCES "user" ("id"),
  entity      TEXT NOT NULL,
  entity_id   TEXT NOT NULL,
  field       TEXT,
  before_value TEXT,
  after_value TEXT,
  action      TEXT NOT NULL,
  at          TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS session_user_id_idx         ON "session"        ("user_id");
CREATE INDEX IF NOT EXISTS account_user_id_idx         ON "account"        ("user_id");
CREATE INDEX IF NOT EXISTS verification_identifier_idx ON "verification"   ("identifier");
CREATE INDEX IF NOT EXISTS booking_session_user_idx    ON booking_session  (user_id);
CREATE INDEX IF NOT EXISTS booking_session_expires_idx ON booking_session  (expires_at);
CREATE INDEX IF NOT EXISTS audit_log_entity_idx        ON audit_log        (entity, entity_id);
