CREATE TYPE "public"."chronic_condition_status" AS ENUM('active', 'controlled', 'resolved');--> statement-breakpoint CREATE TYPE "public"."health_review_status" AS ENUM('pending', 'reviewed', 'resolved');--> statement-breakpoint CREATE TYPE "public"."vital_record_source" AS ENUM('manual', 'device', 'import');--> statement-breakpoint CREATE TABLE "chronic_conditions" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "organization_id" uuid NOT NULL, "elder_id" uuid NOT NULL, "name" text NOT NULL, "status" "chronic_condition_status" DEFAULT 'active' NOT NULL, "diagnosed_at" timestamp with time zone, "treatment_notes" text DEFAULT '' NOT NULL, "follow_up_notes" 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 "health_anomaly_reviews" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "organization_id" uuid NOT NULL, "elder_id" uuid NOT NULL, "vital_record_id" uuid, "severity" "incident_severity" NOT NULL, "status" "health_review_status" DEFAULT 'pending' NOT NULL, "title" text NOT NULL, "description" text NOT NULL, "reviewed_by_account_id" uuid, "reviewed_at" timestamp with time zone, "resolution_notes" 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 "health_profiles" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "organization_id" uuid NOT NULL, "elder_id" uuid NOT NULL, "allergy_notes" text DEFAULT '' NOT NULL, "medical_history" text DEFAULT '' NOT NULL, "medication_notes" text DEFAULT '' NOT NULL, "care_restrictions" text DEFAULT '' NOT NULL, "emergency_notes" 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 "vital_records" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "organization_id" uuid NOT NULL, "elder_id" uuid NOT NULL, "recorded_at" timestamp with time zone NOT NULL, "source" "vital_record_source" DEFAULT 'manual' NOT NULL, "systolic_bp" integer, "diastolic_bp" integer, "heart_rate" integer, "temperature_tenths" integer, "spo2" integer, "blood_glucose_tenths" integer, "weight_tenths" integer, "notes" text DEFAULT '' NOT NULL, "created_by_account_id" uuid, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint ALTER TABLE "chronic_conditions" ADD CONSTRAINT "chronic_conditions_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "chronic_conditions" ADD CONSTRAINT "chronic_conditions_elder_id_elders_id_fk" FOREIGN KEY ("elder_id") REFERENCES "public"."elders"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "health_anomaly_reviews" ADD CONSTRAINT "health_anomaly_reviews_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "health_anomaly_reviews" ADD CONSTRAINT "health_anomaly_reviews_elder_id_elders_id_fk" FOREIGN KEY ("elder_id") REFERENCES "public"."elders"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "health_anomaly_reviews" ADD CONSTRAINT "health_anomaly_reviews_vital_record_id_vital_records_id_fk" FOREIGN KEY ("vital_record_id") REFERENCES "public"."vital_records"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "health_anomaly_reviews" ADD CONSTRAINT "health_anomaly_reviews_reviewed_by_account_id_accounts_id_fk" FOREIGN KEY ("reviewed_by_account_id") REFERENCES "public"."accounts"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "health_profiles" ADD CONSTRAINT "health_profiles_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "health_profiles" ADD CONSTRAINT "health_profiles_elder_id_elders_id_fk" FOREIGN KEY ("elder_id") REFERENCES "public"."elders"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "vital_records" ADD CONSTRAINT "vital_records_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "vital_records" ADD CONSTRAINT "vital_records_elder_id_elders_id_fk" FOREIGN KEY ("elder_id") REFERENCES "public"."elders"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "vital_records" ADD CONSTRAINT "vital_records_created_by_account_id_accounts_id_fk" FOREIGN KEY ("created_by_account_id") REFERENCES "public"."accounts"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint CREATE INDEX "chronic_conditions_elder_status_lookup" ON "chronic_conditions" USING btree ("organization_id","elder_id","status");--> statement-breakpoint CREATE INDEX "health_anomaly_reviews_queue_lookup" ON "health_anomaly_reviews" USING btree ("organization_id","status","severity");--> statement-breakpoint CREATE UNIQUE INDEX "health_profiles_organization_elder_unique" ON "health_profiles" USING btree ("organization_id","elder_id");--> statement-breakpoint CREATE INDEX "vital_records_recent_lookup" ON "vital_records" USING btree ("organization_id","elder_id","recorded_at");