CREATE TYPE "public"."approval_status" AS ENUM('pending', 'approved', 'rejected', 'skipped');--> statement-breakpoint CREATE TYPE "public"."invoice_status" AS ENUM('pending_approval', 'approved', 'scheduled', 'paid', 'rejected', 'void');--> statement-breakpoint CREATE TYPE "public"."payment_method" AS ENUM('check', 'ach');--> statement-breakpoint CREATE TYPE "public"."payment_status" AS ENUM('scheduled', 'payment_submitted', 'issued', 'outstanding', 'cleared');--> statement-breakpoint CREATE TYPE "public"."user_role" AS ENUM('admin', 'ap_processor', 'approver', 'viewer');--> statement-breakpoint CREATE TABLE "activity_log" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "invoice_id" uuid NOT NULL, "actor_user_id" uuid, "message" text NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "approval_policies" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "name" text NOT NULL, "priority" integer DEFAULT 100 NOT NULL, "amount_threshold" numeric(14, 2), "skip_below_amount" numeric(14, 2), "active" boolean DEFAULT true NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "approval_steps" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "invoice_id" uuid NOT NULL, "policy_id" uuid, "step_order" integer DEFAULT 1 NOT NULL, "approver_role" "user_role" DEFAULT 'approver' NOT NULL, "assignee_user_id" uuid, "status" "approval_status" DEFAULT 'pending' NOT NULL, "acted_by_user_id" uuid, "acted_at" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "departments" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "code" text NOT NULL, "name" text NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "documents" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "invoice_id" uuid, "object_key" text NOT NULL, "content_type" text DEFAULT 'application/pdf' NOT NULL, "file_name" text NOT NULL, "uploaded_by_user_id" uuid, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "gl_accounts" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "code" text NOT NULL, "name" text NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "invoice_comments" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "invoice_id" uuid NOT NULL, "author_user_id" uuid, "body" text NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "invoice_lines" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "invoice_id" uuid NOT NULL, "description" text NOT NULL, "amount" numeric(14, 2) NOT NULL, "gl_account_id" uuid, "department_id" uuid, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "invoices" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "vendor_id" uuid NOT NULL, "invoice_number" text NOT NULL, "amount" numeric(14, 2) NOT NULL, "amount_due" numeric(14, 2) NOT NULL, "due_date" text NOT NULL, "pay_date" text, "send_payment_on" text, "status" "invoice_status" DEFAULT 'pending_approval' NOT NULL, "payment_method" "payment_method" DEFAULT 'check' NOT NULL, "memo" text DEFAULT '' NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "pay_runs" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "created_by_user_id" uuid, "status" text DEFAULT 'draft' NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "payments" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "pay_run_id" uuid, "invoice_id" uuid, "vendor_id" uuid, "amount" numeric(14, 2) NOT NULL, "payment_method" "payment_method" DEFAULT 'check' NOT NULL, "check_number" integer, "status" "payment_status" DEFAULT 'scheduled' NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "users" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "cognito_sub" text NOT NULL, "email" text NOT NULL, "name" text NOT NULL, "role" "user_role" DEFAULT 'viewer' NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "vendors" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "name" text NOT NULL, "email" text, "default_payment_method" "payment_method" DEFAULT 'check' NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint ALTER TABLE "activity_log" ADD CONSTRAINT "activity_log_invoice_id_invoices_id_fk" FOREIGN KEY ("invoice_id") REFERENCES "public"."invoices"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "activity_log" ADD CONSTRAINT "activity_log_actor_user_id_users_id_fk" FOREIGN KEY ("actor_user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "approval_steps" ADD CONSTRAINT "approval_steps_invoice_id_invoices_id_fk" FOREIGN KEY ("invoice_id") REFERENCES "public"."invoices"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "approval_steps" ADD CONSTRAINT "approval_steps_policy_id_approval_policies_id_fk" FOREIGN KEY ("policy_id") REFERENCES "public"."approval_policies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "approval_steps" ADD CONSTRAINT "approval_steps_assignee_user_id_users_id_fk" FOREIGN KEY ("assignee_user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "approval_steps" ADD CONSTRAINT "approval_steps_acted_by_user_id_users_id_fk" FOREIGN KEY ("acted_by_user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "documents" ADD CONSTRAINT "documents_invoice_id_invoices_id_fk" FOREIGN KEY ("invoice_id") REFERENCES "public"."invoices"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "documents" ADD CONSTRAINT "documents_uploaded_by_user_id_users_id_fk" FOREIGN KEY ("uploaded_by_user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "invoice_comments" ADD CONSTRAINT "invoice_comments_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_comments" ADD CONSTRAINT "invoice_comments_author_user_id_users_id_fk" FOREIGN KEY ("author_user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "invoice_lines" ADD CONSTRAINT "invoice_lines_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_lines" ADD CONSTRAINT "invoice_lines_gl_account_id_gl_accounts_id_fk" FOREIGN KEY ("gl_account_id") REFERENCES "public"."gl_accounts"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "invoice_lines" ADD CONSTRAINT "invoice_lines_department_id_departments_id_fk" FOREIGN KEY ("department_id") REFERENCES "public"."departments"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "invoices" ADD CONSTRAINT "invoices_vendor_id_vendors_id_fk" FOREIGN KEY ("vendor_id") REFERENCES "public"."vendors"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "pay_runs" ADD CONSTRAINT "pay_runs_created_by_user_id_users_id_fk" FOREIGN KEY ("created_by_user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "payments" ADD CONSTRAINT "payments_pay_run_id_pay_runs_id_fk" FOREIGN KEY ("pay_run_id") REFERENCES "public"."pay_runs"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "payments" ADD CONSTRAINT "payments_invoice_id_invoices_id_fk" FOREIGN KEY ("invoice_id") REFERENCES "public"."invoices"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "payments" ADD CONSTRAINT "payments_vendor_id_vendors_id_fk" FOREIGN KEY ("vendor_id") REFERENCES "public"."vendors"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint CREATE UNIQUE INDEX "invoices_vendor_number_active_uidx" ON "invoices" USING btree ("vendor_id","invoice_number") WHERE "invoices"."status" <> 'void';--> statement-breakpoint CREATE UNIQUE INDEX "users_cognito_sub_uidx" ON "users" USING btree ("cognito_sub");--> statement-breakpoint CREATE UNIQUE INDEX "users_email_uidx" ON "users" USING btree ("email");