CREATE TABLE "funder_grants" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"funder_id" uuid NOT NULL,
	"recipient_name" text NOT NULL,
	"recipient_city" text,
	"recipient_state" text,
	"amount" integer,
	"purpose" text,
	"tax_year" integer NOT NULL,
	"created_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "funders" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"ein" text NOT NULL,
	"name" text NOT NULL,
	"city" text,
	"state" text,
	"ntee_code" text,
	"total_assets" bigint,
	"application_info" jsonb,
	"latest_tax_year" integer,
	"latest_object_id" text,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
ALTER TABLE "grants" ADD COLUMN "funder_ein" text;--> statement-breakpoint
ALTER TABLE "funder_grants" ADD CONSTRAINT "funder_grants_funder_id_funders_id_fk" FOREIGN KEY ("funder_id") REFERENCES "public"."funders"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "idx_funder_grants_funder" ON "funder_grants" USING btree ("funder_id");--> statement-breakpoint
CREATE INDEX "idx_funder_grants_funder_year" ON "funder_grants" USING btree ("funder_id","tax_year");--> statement-breakpoint
CREATE INDEX "idx_funder_grants_recipient_state" ON "funder_grants" USING btree ("recipient_state");--> statement-breakpoint
CREATE UNIQUE INDEX "idx_funders_ein" ON "funders" USING btree ("ein");--> statement-breakpoint
CREATE INDEX "idx_funders_state" ON "funders" USING btree ("state");--> statement-breakpoint
CREATE INDEX "idx_grants_funder_ein" ON "grants" USING btree ("funder_ein");