CREATE TABLE IF NOT EXISTS "auth_refresh_tokens" (
    "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
    "user_id" uuid NOT NULL,
    "tenant_id" bigint,
    "token_hash" text NOT NULL,
    "created_at" timestamp with time zone DEFAULT now() NOT NULL,
    "expires_at" timestamp with time zone NOT NULL,
    "revoked_at" timestamp with time zone,
    CONSTRAINT "auth_refresh_tokens_token_hash_unique" UNIQUE("token_hash"),
    CONSTRAINT "auth_refresh_tokens_user_id_auth_users_id_fk"
        FOREIGN KEY ("user_id") REFERENCES "public"."auth_users"("id")
        ON DELETE cascade ON UPDATE cascade,
    CONSTRAINT "auth_refresh_tokens_tenant_id_tenants_id_fk"
        FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id")
        ON DELETE set null ON UPDATE cascade
);

CREATE INDEX IF NOT EXISTS "auth_refresh_tokens_user_id_idx"
    ON "auth_refresh_tokens" ("user_id");
CREATE INDEX IF NOT EXISTS "auth_refresh_tokens_expires_at_idx"
    ON "auth_refresh_tokens" ("expires_at");
