-- Add up migration script here
CREATE TABLE "oauth2_state_storage" (
    "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
    "csrf_state" text NOT NULL,
    "pkce_code_verifier" text NOT NULL,
    "return_url" text NOT NULL
);

CREATE TABLE "user_sessions" (
    "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
    "user_id" integer NOT NULL,
    "session_token_p1" text NOT NULL,
    "session_token_p2" text NOT NULL,
    "created_at" integer NOT NULL,
    "expires_at" integer NOT NULL
);

CREATE TABLE "users" (
    "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
    "created_at" integer NOT NULL,
    "created_by" integer NOT NULL,
    "updated_at" integer NOT NULL,
    "updated_by" integer NOT NULL,
	"email"	text NOT NULL UNIQUE,
	"name"	text NOT NULL,
	"family_name"	text NOT NULL,
	"given_name"	text NOT NULL
);

CREATE TABLE IF NOT EXISTS roles (
    "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
    "created_at" integer NOT NULL,
    "created_by" integer NOT NULL,
    "updated_at" integer NOT NULL,
    "updated_by" integer NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT
);

CREATE TABLE IF NOT EXISTS user_roles (
    "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
    "created_at" integer NOT NULL,
    "created_by" integer NOT NULL,
    "updated_at" integer NOT NULL,
    "updated_by" integer NOT NULL,
    "user_id" integer NOT NULL,
    "role_id" integer NOT NULL
);

create TABLE IF NOT EXISTS role_permissions (
    "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
    "created_at" integer NOT NULL,
    "created_by" integer NOT NULL,
    "updated_at" integer NOT NULL,
    "updated_by" integer NOT NULL,
    "role_id" integer NOT NULL,
    "item" text NOT NULL
);
