CREATE TABLE "invoice_email_events" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"invoice_id" uuid NOT NULL,
	"sent_by" uuid NOT NULL,
	"to_address" text NOT NULL,
	"event_type" text NOT NULL,
	"metadata" jsonb,
	"occurred_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "invoice_email_events_event_type_check" CHECK ("invoice_email_events"."event_type" IN ('sent', 'delivered', 'opened', 'clicked', 'bounced', 'failed'))
);
--> statement-breakpoint
ALTER TABLE "contracts" ADD COLUMN "signing_order" text DEFAULT 'parallel' NOT NULL;--> statement-breakpoint
ALTER TABLE "invoice_email_events" ADD CONSTRAINT "invoice_email_events_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "invoice_email_events" ADD CONSTRAINT "invoice_email_events_invoice_id_invoices_id_fk" FOREIGN KEY ("invoice_id") REFERENCES "public"."invoices"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "invoice_email_events" ADD CONSTRAINT "invoice_email_events_sent_by_users_id_fk" FOREIGN KEY ("sent_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "idx_invoice_email_events_invoice" ON "invoice_email_events" USING btree ("invoice_id","occurred_at");--> statement-breakpoint
CREATE INDEX "idx_invoice_email_events_recipient" ON "invoice_email_events" USING btree ("invoice_id","to_address","event_type","occurred_at");