CREATE TYPE "public"."client_mode" AS ENUM('read', 'write');--> statement-breakpoint
CREATE TYPE "public"."client_scope" AS ENUM('internal', 'customer', 'public');--> statement-breakpoint
CREATE TABLE "api_client" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"name" text NOT NULL,
	"token_hash" text NOT NULL,
	"mode" "client_mode" DEFAULT 'read' NOT NULL,
	"scope" "client_scope" DEFAULT 'customer' NOT NULL,
	"project_id" uuid,
	"can_publish" boolean DEFAULT false NOT NULL,
	"last_used_at" timestamp with time zone,
	"revoked_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "api_client_token_hash_unique" UNIQUE("token_hash")
);
--> statement-breakpoint
CREATE TABLE "post_read" (
	"post_id" uuid NOT NULL,
	"project_id" uuid NOT NULL,
	"external_user_id" text NOT NULL,
	"read_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "post_read_post_id_external_user_id_pk" PRIMARY KEY("post_id","external_user_id")
);
--> statement-breakpoint
ALTER TABLE "api_client" ADD CONSTRAINT "api_client_project_id_project_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."project"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "post_read" ADD CONSTRAINT "post_read_post_id_post_id_fk" FOREIGN KEY ("post_id") REFERENCES "public"."post"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "post_read" ADD CONSTRAINT "post_read_project_id_project_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."project"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "post_read_project_user" ON "post_read" USING btree ("project_id","external_user_id");