-- Add up migration script here

CREATE TABLE "clients" (
    "id" uuid NOT NULL,
    "user_id" uuid NOT NULL,
    "name" text NOT NULL,
    "secret" text NOT NULL,
    "redirect_uri" text NOT NULL,
    "created_at" timestamp DEFAULT CURRENT_TIMESTAMP,
    "updated_at" timestamp DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "clients_name_key" UNIQUE ("name"),
    CONSTRAINT "clients_pkey" PRIMARY KEY ("id")
) WITH (oids = false);

CREATE TABLE "authorization_code" (
    "code" text NOT NULL,
    "user_id" uuid NOT NULL,
    "client_id" uuid NOT NULL,
    "code_challenge" text NOT NULL,
    "code_challenge_method" text NOT NULL,
    "created_at" timestamp DEFAULT CURRENT_TIMESTAMP,
    "updated_at" timestamp DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "authorization_code_pkey" PRIMARY KEY ("code")
) WITH (oids = false);

CREATE TABLE "authorized_clients" (
    "user_id" uuid NOT NULL,
    "client_id" uuid NOT NULL,
    "created_at" timestamp DEFAULT CURRENT_TIMESTAMP,
    "updated_at" timestamp DEFAULT CURRENT_TIMESTAMP, -- last accessed
    CONSTRAINT "authorized_clients_pkey" PRIMARY KEY ("user_id", "client_id")
) WITH (oids = false);

CREATE TABLE "refresh_tokens" (
    "token" text NOT NULL,
    "user_id" uuid NOT NULL,
    "client_id" uuid NOT NULL,
    "expires_at" timestamp DEFAULT CURRENT_TIMESTAMP + interval '30' day,
    CONSTRAINT "refresh_tokens_pkey" PRIMARY KEY ("token")
) WITH (oids = false);
