CREATE TYPE "public"."audit_event_type" AS ENUM('document_prepared', 'email_sent', 'link_opened', 'document_viewed', 'signature_submitted', 'pdf_hash_computed');--> statement-breakpoint
CREATE TABLE "audit_events" (
	"id" text PRIMARY KEY NOT NULL,
	"document_id" text NOT NULL,
	"event_type" "audit_event_type" NOT NULL,
	"ip_address" text,
	"user_agent" text,
	"metadata" jsonb,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "signing_tokens" (
	"jti" text PRIMARY KEY NOT NULL,
	"document_id" text NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"expires_at" timestamp NOT NULL,
	"used_at" timestamp
);
--> statement-breakpoint
ALTER TABLE "documents" ADD COLUMN "signed_file_path" text;--> statement-breakpoint
ALTER TABLE "documents" ADD COLUMN "pdf_hash" text;--> statement-breakpoint
ALTER TABLE "documents" ADD COLUMN "signed_at" timestamp;--> statement-breakpoint
ALTER TABLE "audit_events" ADD CONSTRAINT "audit_events_document_id_documents_id_fk" FOREIGN KEY ("document_id") REFERENCES "public"."documents"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "signing_tokens" ADD CONSTRAINT "signing_tokens_document_id_documents_id_fk" FOREIGN KEY ("document_id") REFERENCES "public"."documents"("id") ON DELETE cascade ON UPDATE no action;