CREATE TABLE "sys_users" (
  "id" uuid NOT NULL DEFAULT gen_random_uuid(),
  "account" character varying(100) NOT NULL,
  "password_hash" text NOT NULL,
  "name" character varying(100) NOT NULL,
  "email" character varying(255) NOT NULL,
  "phone" character varying(50) NULL,
  "created_at" timestamptz NOT NULL DEFAULT now(),
  "updated_at" timestamptz NOT NULL DEFAULT now(),
  PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "ux_users_account" ON "sys_users" ("account");
CREATE UNIQUE INDEX "ux_users_email" ON "sys_users" ("email");

CREATE TABLE "user_sessions" (
  "id" uuid NOT NULL DEFAULT gen_random_uuid(),
  "user_id" uuid NOT NULL,
  "jti" character varying(64) NOT NULL,
  "token_hash" text NOT NULL,
  "expires_at" timestamptz NOT NULL,
  "revoked_at" timestamptz NULL,
  "created_at" timestamptz NOT NULL DEFAULT now(),
  "updated_at" timestamptz NOT NULL DEFAULT now(),
  PRIMARY KEY ("id"),
  CONSTRAINT "user_sessions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "sys_users" ("id") ON DELETE CASCADE
);

CREATE UNIQUE INDEX "ux_user_sessions_jti" ON "user_sessions" ("jti");
CREATE INDEX "ix_user_sessions_user_id" ON "user_sessions" ("user_id");
