CREATE TYPE "public"."asca_contribution_status_enum" AS ENUM('PENDING', 'PAID', 'LATE', 'REFUNDED');--> statement-breakpoint
CREATE TYPE "public"."asca_member_status_enum" AS ENUM('ACTIVE', 'DEFAULTED', 'WITHDRAWN');--> statement-breakpoint
CREATE TYPE "public"."asca_status_enum" AS ENUM('DRAFT', 'FUNDING', 'READY', 'PENDING_INVESTMENT', 'INVESTED', 'MATURED', 'CLOSED', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."asca_type_enum" AS ENUM('GENERAL', 'INVESTMENT');--> statement-breakpoint
CREATE TYPE "public"."circle_status_enum" AS ENUM('FORMING', 'UPCOMING', 'ACTIVE', 'COMPLETED', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."commitment_status_enum" AS ENUM('PENDING', 'CONFIRMED', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."contribution_frequency_enum" AS ENUM('WEEKLY', 'BIWEEKLY', 'MONTHLY');--> statement-breakpoint
CREATE TYPE "public"."contribution_status_enum" AS ENUM('PAID', 'PENDING', 'MISSED', 'WAIVED');--> statement-breakpoint
CREATE TYPE "public"."event_type_enum" AS ENUM('VIRTUAL', 'IN_PERSON', 'HYBRID');--> statement-breakpoint
CREATE TYPE "public"."helping_hand_status_enum" AS ENUM('DRAFT', 'SUBMITTED', 'UNDER_REVIEW', 'APPROVED', 'REJECTED', 'FUNDED', 'REPAYING', 'CLOSED');--> statement-breakpoint
CREATE TYPE "public"."intake_status_enum" AS ENUM('PENDING', 'IN_REVIEW', 'QUOTED', 'ACCEPTED', 'COMPLETED', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."investment_category_enum" AS ENUM('REAL_ESTATE', 'AGRICULTURE', 'SMALL_BUSINESS', 'COMMUNITY_DEVELOPMENT', 'BONDS', 'OTHER');--> statement-breakpoint
CREATE TYPE "public"."investment_intent_reservation_status_enum" AS ENUM('RESERVED', 'FINALIZED', 'RELEASED');--> statement-breakpoint
CREATE TYPE "public"."investment_interest_status_enum" AS ENUM('PENDING', 'CONFIRMED', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."investment_opportunity_status_enum" AS ENUM('DRAFT', 'OPEN', 'FUNDED', 'CLOSED');--> statement-breakpoint
CREATE TYPE "public"."investor_category_enum" AS ENUM('WORKER_OWNER', 'COMMUNITY_SPONSOR', 'IMPACT_INVESTOR');--> statement-breakpoint
CREATE TYPE "public"."invoice_status_enum" AS ENUM('DRAFT', 'SENT', 'PAID', 'OVERDUE', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."lending_loan_type_enum" AS ENUM('COMMUNITY_LENDING', 'P2P_LENDING');--> statement-breakpoint
CREATE TYPE "public"."lending_min_appreciation_type_enum" AS ENUM('NONE', 'FIXED', 'PCT_OF_PRINCIPAL', 'TERM_TIERED');--> statement-breakpoint
CREATE TYPE "public"."lending_pool_return_model_enum" AS ENUM('FIXED_SIMPLE');--> statement-breakpoint
CREATE TYPE "public"."lending_pool_status_enum" AS ENUM('DRAFT', 'OPEN', 'PAUSED', 'CLOSED');--> statement-breakpoint
CREATE TYPE "public"."lending_repayment_frequency_enum" AS ENUM('WEEKLY', 'BIWEEKLY', 'MONTHLY');--> statement-breakpoint
CREATE TYPE "public"."loan_application_status_enum" AS ENUM('DRAFT', 'SUBMITTED', 'WAITLISTED', 'UNDER_REVIEW', 'APPROVED', 'APPROVED_RESERVED', 'REJECTED', 'CANCELLED', 'FUNDED', 'REPAYING', 'CLOSED');--> statement-breakpoint
CREATE TYPE "public"."membership_plan_tier_enum" AS ENUM('STARTER', 'PROFESSIONAL', 'ELITE', 'ENTREPRENEUR', 'YOUTH', 'COMMUNITY', 'EDUCATION', 'GRASSROOT', 'WORKER', 'IMPACT', 'SPONSOR');--> statement-breakpoint
CREATE TYPE "public"."membership_status_enum" AS ENUM('PENDING', 'ACTIVE', 'SUSPENDED', 'EXPIRED', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."membership_type_enum" AS ENUM('INDIVIDUAL', 'ORGANIZATION', 'INVESTOR');--> statement-breakpoint
CREATE TYPE "public"."notification_category_enum" AS ENUM('SYSTEM', 'MEMBERSHIP', 'SAVINGS', 'LENDING', 'INVESTMENT', 'ACADEMY', 'PAYMENT', 'COMMUNITY');--> statement-breakpoint
CREATE TYPE "public"."notification_channel_enum" AS ENUM('PUSH', 'EMAIL');--> statement-breakpoint
CREATE TYPE "public"."notification_delivery_status_enum" AS ENUM('SENT', 'FAILED', 'SKIPPED');--> statement-breakpoint
CREATE TYPE "public"."notification_severity_enum" AS ENUM('INFO', 'SUCCESS', 'WARNING', 'ERROR');--> statement-breakpoint
CREATE TYPE "public"."onboarding_activity_status_enum" AS ENUM('NOT_STARTED', 'IN_PROGRESS', 'COMPLETED');--> statement-breakpoint
CREATE TYPE "public"."onboarding_activity_type_enum" AS ENUM('VIDEO', 'FILE', 'RICH_TEXT', 'EXTERNAL_LINK', 'QUIZ', 'CHECKLIST', 'SURVEY');--> statement-breakpoint
CREATE TYPE "public"."onboarding_program_duration_unit_enum" AS ENUM('WEEK', 'DAY');--> statement-breakpoint
CREATE TYPE "public"."onboarding_program_status_enum" AS ENUM('DRAFT', 'PUBLISHED', 'ARCHIVED');--> statement-breakpoint
CREATE TYPE "public"."org_member_role_enum" AS ENUM('OWNER', 'ADMIN', 'MEMBER');--> statement-breakpoint
CREATE TYPE "public"."organization_invitation_status_enum" AS ENUM('PENDING', 'ACCEPTED', 'CANCELLED', 'EXPIRED');--> statement-breakpoint
CREATE TYPE "public"."organization_member_obligation_kind_enum" AS ENUM('TREASURY', 'ORG_SUSU');--> statement-breakpoint
CREATE TYPE "public"."organization_member_obligation_status_enum" AS ENUM('PENDING', 'PAID', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."organization_member_status_enum" AS ENUM('PENDING', 'ACTIVE');--> statement-breakpoint
CREATE TYPE "public"."organization_operational_status_enum" AS ENUM('PENDING', 'ACTIVE', 'SUSPENDED', 'ARCHIVED');--> statement-breakpoint
CREATE TYPE "public"."organization_type_enum" AS ENUM('SCHOOL', 'GRASSROOTS_ROSCA', 'NFP_ALUMNI_FAITH');--> statement-breakpoint
CREATE TYPE "public"."payment_method_enum" AS ENUM('STRIPE', 'EFT', 'INTERAC', 'WIRE', 'WALLET');--> statement-breakpoint
CREATE TYPE "public"."payment_provider_enum" AS ENUM('STRIPE', 'INTERAC', 'MANUAL');--> statement-breakpoint
CREATE TYPE "public"."payment_status_enum" AS ENUM('PENDING', 'COMPLETED', 'FAILED', 'REFUNDED');--> statement-breakpoint
CREATE TYPE "public"."payout_status_enum" AS ENUM('PENDING', 'PAID', 'SKIPPED');--> statement-breakpoint
CREATE TYPE "public"."push_platform_enum" AS ENUM('EXPO');--> statement-breakpoint
CREATE TYPE "public"."repayment_status_enum" AS ENUM('PENDING', 'PAID', 'LATE', 'WAIVED');--> statement-breakpoint
CREATE TYPE "public"."resource_file_type_enum" AS ENUM('PDF', 'VIDEO', 'DOCUMENT', 'SPREADSHEET', 'IMAGE', 'LINK', 'OTHER');--> statement-breakpoint
CREATE TYPE "public"."resource_kind_enum" AS ENUM('FILE', 'ARTICLE');--> statement-breakpoint
CREATE TYPE "public"."risk_level_enum" AS ENUM('LOW', 'MEDIUM', 'HIGH');--> statement-breakpoint
CREATE TYPE "public"."rsvp_status_enum" AS ENUM('GOING', 'WAITLISTED', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."user_role_enum" AS ENUM('MEMBER', 'ADMIN', 'SUPER_ADMIN');--> statement-breakpoint
CREATE TYPE "public"."user_segment_enum" AS ENUM('PROFESSIONAL', 'ENTREPRENEUR', 'YOUTH', 'SCHOOL', 'INSTITUTION');--> statement-breakpoint
CREATE TYPE "public"."wallet_owner_type_enum" AS ENUM('USER', 'ORGANIZATION', 'SUSU', 'INVESTMENT', 'CIRCLE', 'LENDING_POOL', 'LENDING_MASTER', 'PLATFORM');--> statement-breakpoint
CREATE TYPE "public"."wallet_payment_intent_status_enum" AS ENUM('CREATED', 'PENDING', 'SUCCEEDED', 'FAILED', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."wallet_payment_intent_type_enum" AS ENUM('TOP_UP', 'CASH_OUT', 'REFUND');--> statement-breakpoint
CREATE TYPE "public"."wallet_transaction_type_enum" AS ENUM('TOP_UP', 'CASH_OUT', 'REFUND', 'CONTRIBUTION', 'PAYOUT', 'ADJUSTMENT', 'INVESTMENT_PURCHASE', 'INVESTMENT_PRINCIPAL_PAYOUT', 'INVESTMENT_RETURN_PAYOUT', 'LENDING_PRINCIPAL_PAYOUT', 'LENDING_RETURN_PAYOUT', 'LOAN_DISBURSEMENT', 'LOAN_REPAYMENT', 'HELPING_HAND_DISBURSEMENT', 'HELPING_HAND_REPAYMENT');--> statement-breakpoint
CREATE TYPE "public"."webhook_event_status_enum" AS ENUM('PENDING', 'PROCESSED', 'FAILED');--> statement-breakpoint
CREATE TABLE "academy_categories" (
	"id" serial PRIMARY KEY NOT NULL,
	"name" text NOT NULL,
	"slug" text NOT NULL,
	"sort_order" integer DEFAULT 0 NOT NULL,
	CONSTRAINT "academy_categories_name_unique" UNIQUE("name"),
	CONSTRAINT "academy_categories_slug_unique" UNIQUE("slug")
);
--> statement-breakpoint
CREATE TABLE "academy_courses" (
	"id" serial PRIMARY KEY NOT NULL,
	"title" text NOT NULL,
	"slug" text NOT NULL,
	"description" text NOT NULL,
	"thumbnail_url" text,
	"category_id" integer NOT NULL,
	"instructor" text NOT NULL,
	"duration" text NOT NULL,
	"level" text NOT NULL,
	"rating" numeric(3, 1),
	"enrolled_count" integer DEFAULT 0 NOT NULL,
	"external_url" text NOT NULL,
	"tags" json DEFAULT '[]'::json NOT NULL,
	"is_published" boolean DEFAULT true NOT NULL,
	"sort_order" integer DEFAULT 0 NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "academy_courses_slug_unique" UNIQUE("slug")
);
--> statement-breakpoint
CREATE TABLE "announcements" (
	"id" serial PRIMARY KEY NOT NULL,
	"title" text NOT NULL,
	"body" text NOT NULL,
	"category" text NOT NULL,
	"pinned" boolean DEFAULT false NOT NULL,
	"published_by" integer,
	"published_at" timestamp DEFAULT now() NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "asca_contributions" (
	"id" serial PRIMARY KEY NOT NULL,
	"asca_id" integer NOT NULL,
	"member_id" integer NOT NULL,
	"amount" numeric(12, 2) NOT NULL,
	"status" "asca_contribution_status_enum" DEFAULT 'PENDING' NOT NULL,
	"due_date" timestamp,
	"paid_at" timestamp,
	"transaction_id" integer,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "asca_members" (
	"id" serial PRIMARY KEY NOT NULL,
	"asca_id" integer NOT NULL,
	"user_id" integer NOT NULL,
	"target_contribution" numeric(12, 2) NOT NULL,
	"contributed_amount" numeric(12, 2) DEFAULT '0' NOT NULL,
	"status" "asca_member_status_enum" DEFAULT 'ACTIVE' NOT NULL,
	"joined_at" timestamp DEFAULT now() NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "ascas" (
	"id" serial PRIMARY KEY NOT NULL,
	"name" text NOT NULL,
	"description" text,
	"type" "asca_type_enum" DEFAULT 'GENERAL' NOT NULL,
	"status" "asca_status_enum" DEFAULT 'DRAFT' NOT NULL,
	"investment_id" integer,
	"escrow_wallet_id" integer,
	"target_amount" numeric(12, 2),
	"current_amount" numeric(12, 2) DEFAULT '0' NOT NULL,
	"constitution" json DEFAULT '{}'::json NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "bank_setup" (
	"id" serial PRIMARY KEY NOT NULL,
	"eft_account_name" text NOT NULL,
	"eft_institution_number" text NOT NULL,
	"eft_transit_number" text NOT NULL,
	"eft_account_number" text NOT NULL,
	"interac_recipient_name" text NOT NULL,
	"interac_email" text NOT NULL,
	"cashout_min_amount" numeric(12, 2) NOT NULL,
	"cashout_fee_pct" numeric(6, 3) NOT NULL,
	"cashout_fee_fixed" numeric(12, 2) NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "circle_memberships" (
	"id" serial PRIMARY KEY NOT NULL,
	"circle_id" integer NOT NULL,
	"user_id" integer NOT NULL,
	"position" integer NOT NULL,
	"member_auto_deduct_enabled" boolean DEFAULT false NOT NULL,
	"joined_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "circle_payouts" (
	"id" serial PRIMARY KEY NOT NULL,
	"circle_id" integer NOT NULL,
	"user_id" integer NOT NULL,
	"position" integer NOT NULL,
	"scheduled_date" timestamp NOT NULL,
	"paid_date" timestamp,
	"amount" numeric(12, 2) NOT NULL,
	"status" "payout_status_enum" DEFAULT 'PENDING' NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "community_events" (
	"id" serial PRIMARY KEY NOT NULL,
	"title" text NOT NULL,
	"description" text NOT NULL,
	"date" timestamp NOT NULL,
	"start_time" text NOT NULL,
	"end_time" text,
	"location" text NOT NULL,
	"type" "event_type_enum" DEFAULT 'VIRTUAL' NOT NULL,
	"max_attendees" integer,
	"organizer_id" integer,
	"cover_image_url" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "course_interests" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer NOT NULL,
	"course_id" integer NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "credit_score_factors" (
	"id" serial PRIMARY KEY NOT NULL,
	"credit_score_id" integer NOT NULL,
	"factor_name" text NOT NULL,
	"score" integer NOT NULL,
	"weight" text NOT NULL
);
--> statement-breakpoint
CREATE TABLE "discussion_replies" (
	"id" serial PRIMARY KEY NOT NULL,
	"discussion_id" integer NOT NULL,
	"author_id" integer NOT NULL,
	"body" text NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "discussions" (
	"id" serial PRIMARY KEY NOT NULL,
	"author_id" integer NOT NULL,
	"title" text NOT NULL,
	"body" text NOT NULL,
	"category" text NOT NULL,
	"pinned" boolean DEFAULT false NOT NULL,
	"view_count" integer DEFAULT 0 NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "event_rsvps" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer NOT NULL,
	"event_id" integer NOT NULL,
	"status" "rsvp_status_enum" DEFAULT 'GOING' NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "helping_hand_applications" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer NOT NULL,
	"requested_amount" numeric(10, 2) NOT NULL,
	"purpose" text NOT NULL,
	"term" text NOT NULL,
	"repayment_schedule" text NOT NULL,
	"notes" text,
	"status" "helping_hand_status_enum" DEFAULT 'DRAFT' NOT NULL,
	"reviewed_by_admin_id" integer,
	"reviewed_at" timestamp,
	"review_note" text,
	"qualifying_circle_id" integer,
	"qualifying_payout_id" integer,
	"qualifying_payout_amount" numeric(12, 2),
	"qualifying_multiplier" numeric(6, 2),
	"approved_amount" numeric(10, 2),
	"disbursed_amount" numeric(10, 2),
	"disbursement_transaction_id" integer,
	"disbursed_at" timestamp,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "helping_hand_repayments" (
	"id" serial PRIMARY KEY NOT NULL,
	"helping_hand_application_id" integer NOT NULL,
	"due_date" timestamp NOT NULL,
	"amount" numeric(10, 2) NOT NULL,
	"paid_amount" numeric(10, 2),
	"paid_at" timestamp,
	"status" "repayment_status_enum" DEFAULT 'PENDING' NOT NULL,
	"method" "payment_method_enum",
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "investment_intent_reservations" (
	"id" serial PRIMARY KEY NOT NULL,
	"interest_id" integer NOT NULL,
	"wallet_id" integer NOT NULL,
	"amount" numeric(10, 2) NOT NULL,
	"status" "investment_intent_reservation_status_enum" DEFAULT 'RESERVED' NOT NULL,
	"finalized_transaction_id" integer,
	"reserved_at" timestamp DEFAULT now() NOT NULL,
	"finalized_at" timestamp,
	"released_at" timestamp,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "investment_interests" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer NOT NULL,
	"opportunity_id" integer NOT NULL,
	"amount" numeric(10, 2) NOT NULL,
	"notes" text,
	"status" "investment_interest_status_enum" DEFAULT 'PENDING' NOT NULL,
	"confirmed_at" timestamp,
	"cancelled_at" timestamp,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "investment_opportunities" (
	"id" serial PRIMARY KEY NOT NULL,
	"title" text NOT NULL,
	"slug" text NOT NULL,
	"description" text NOT NULL,
	"category" "investment_category_enum" NOT NULL,
	"expected_return_pct" integer NOT NULL,
	"min_investment" numeric(10, 2) NOT NULL,
	"target_amount" numeric(12, 2) NOT NULL,
	"raised_amount" numeric(12, 2) DEFAULT '0' NOT NULL,
	"deadline" timestamp NOT NULL,
	"risk_level" "risk_level_enum" DEFAULT 'MEDIUM' NOT NULL,
	"status" "investment_opportunity_status_enum" DEFAULT 'DRAFT' NOT NULL,
	"featured" boolean DEFAULT false NOT NULL,
	"eligible_investor_tiers" json DEFAULT '[]'::json NOT NULL,
	"video_url" text,
	"academy_link" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "investment_opportunities_slug_unique" UNIQUE("slug")
);
--> statement-breakpoint
CREATE TABLE "investment_transactions" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer NOT NULL,
	"opportunity_id" integer NOT NULL,
	"amount" numeric(10, 2) NOT NULL,
	"type" text NOT NULL,
	"description" text,
	"processed_at" timestamp DEFAULT now() NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "invoices" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer NOT NULL,
	"subscription_id" integer,
	"invoice_number" text NOT NULL,
	"amount" numeric(12, 2) NOT NULL,
	"currency" text DEFAULT 'CAD' NOT NULL,
	"status" "invoice_status_enum" DEFAULT 'DRAFT' NOT NULL,
	"due_date" timestamp NOT NULL,
	"paid_at" timestamp,
	"pdf_url" text,
	"line_items" json DEFAULT '[]'::json NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "invoices_invoice_number_unique" UNIQUE("invoice_number")
);
--> statement-breakpoint
CREATE TABLE "lending_commitments" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer NOT NULL,
	"opportunity_id" integer NOT NULL,
	"amount" numeric(10, 2) NOT NULL,
	"status" "commitment_status_enum" DEFAULT 'PENDING' NOT NULL,
	"confirmed_at" timestamp,
	"status_changed_at" timestamp,
	"status_changed_by_admin_id" integer,
	"review_note" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "lending_opportunities" (
	"id" serial PRIMARY KEY NOT NULL,
	"legacy_pool_id" integer,
	"template_id" integer,
	"application_id" integer,
	"loan_type" "lending_loan_type_enum" DEFAULT 'P2P_LENDING' NOT NULL,
	"title" text NOT NULL,
	"description" text NOT NULL,
	"status" "lending_pool_status_enum" DEFAULT 'DRAFT' NOT NULL,
	"target_amount" numeric(12, 2) NOT NULL,
	"committed_amount" numeric(12, 2) DEFAULT '0' NOT NULL,
	"min_commitment" numeric(10, 2) NOT NULL,
	"borrower_appreciation_amount" numeric(10, 2) DEFAULT '0.00' NOT NULL,
	"borrower_term_months" integer DEFAULT 12 NOT NULL,
	"repayment_frequency" "lending_repayment_frequency_enum" DEFAULT 'MONTHLY' NOT NULL,
	"processing_fee_pct" numeric(5, 2),
	"wwn_margin_pct" numeric(5, 2) DEFAULT '0.00' NOT NULL,
	"reserve_contribution_pct" numeric(5, 2) DEFAULT '0.00' NOT NULL,
	"platform_appreciation_share_pct" numeric(5, 2) DEFAULT '0.00' NOT NULL,
	"min_appreciation_type" "lending_min_appreciation_type_enum" DEFAULT 'NONE' NOT NULL,
	"min_appreciation_value" numeric(10, 2) DEFAULT '0.00' NOT NULL,
	"min_appreciation_tiers" json,
	"expected_return_pct" numeric(5, 2) DEFAULT '8.00' NOT NULL,
	"term_months" integer DEFAULT 12 NOT NULL,
	"return_model" "lending_pool_return_model_enum" DEFAULT 'FIXED_SIMPLE' NOT NULL,
	"expected_return" text NOT NULL,
	"term" text NOT NULL,
	"min_borrower_plan_tier" "membership_plan_tier_enum" DEFAULT 'STARTER' NOT NULL,
	"borrower_type" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "lending_opportunities_legacy_pool_id_unique" UNIQUE("legacy_pool_id"),
	CONSTRAINT "lending_opportunities_application_id_unique" UNIQUE("application_id")
);
--> statement-breakpoint
CREATE TABLE "lending_opportunity_return_allocations" (
	"id" serial PRIMARY KEY NOT NULL,
	"return_event_id" integer NOT NULL,
	"commitment_id" integer NOT NULL,
	"opportunity_id" integer NOT NULL,
	"user_id" integer NOT NULL,
	"commitment_amount_snapshot" numeric(10, 2) DEFAULT '0' NOT NULL,
	"ownership_pct_snapshot" numeric(8, 6) DEFAULT '0' NOT NULL,
	"allocated_return_amount" numeric(10, 2) DEFAULT '0' NOT NULL,
	"occurred_at" timestamp DEFAULT now() NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "lending_opportunity_return_events" (
	"id" serial PRIMARY KEY NOT NULL,
	"legacy_return_event_id" integer,
	"opportunity_id" integer NOT NULL,
	"repayment_id" integer NOT NULL,
	"loan_application_id" integer NOT NULL,
	"wallet_transaction_id" integer,
	"gross_amount" numeric(10, 2) DEFAULT '0' NOT NULL,
	"principal_amount" numeric(10, 2) DEFAULT '0' NOT NULL,
	"return_amount" numeric(10, 2) DEFAULT '0' NOT NULL,
	"allocated_return_amount" numeric(10, 2) DEFAULT '0' NOT NULL,
	"occurred_at" timestamp DEFAULT now() NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "lending_opportunity_return_events_legacy_return_event_id_unique" UNIQUE("legacy_return_event_id")
);
--> statement-breakpoint
CREATE TABLE "lending_templates" (
	"id" serial PRIMARY KEY NOT NULL,
	"legacy_pool_id" integer,
	"source_opportunity_id" integer,
	"loan_type" "lending_loan_type_enum" DEFAULT 'P2P_LENDING' NOT NULL,
	"title" text NOT NULL,
	"description" text NOT NULL,
	"status" "lending_pool_status_enum" DEFAULT 'OPEN' NOT NULL,
	"is_snapshot" boolean DEFAULT false NOT NULL,
	"min_commitment" numeric(10, 2) NOT NULL,
	"min_request_amount" numeric(10, 2) DEFAULT '500.00' NOT NULL,
	"max_request_amount" numeric(12, 2) DEFAULT '25000.00' NOT NULL,
	"borrower_appreciation_amount" numeric(10, 2) DEFAULT '0.00' NOT NULL,
	"borrower_term_months" integer DEFAULT 12 NOT NULL,
	"repayment_frequency" "lending_repayment_frequency_enum" DEFAULT 'MONTHLY' NOT NULL,
	"processing_fee_pct" numeric(5, 2),
	"wwn_margin_pct" numeric(5, 2) DEFAULT '0.00' NOT NULL,
	"reserve_contribution_pct" numeric(5, 2) DEFAULT '0.00' NOT NULL,
	"platform_appreciation_share_pct" numeric(5, 2) DEFAULT '0.00' NOT NULL,
	"min_appreciation_type" "lending_min_appreciation_type_enum" DEFAULT 'NONE' NOT NULL,
	"min_appreciation_value" numeric(10, 2) DEFAULT '0.00' NOT NULL,
	"min_appreciation_tiers" json,
	"economics_updated_at" timestamp,
	"economics_updated_by_admin_id" integer,
	"economics_update_reason" text,
	"expected_return_pct" numeric(5, 2) DEFAULT '8.00' NOT NULL,
	"term_months" integer DEFAULT 12 NOT NULL,
	"return_model" "lending_pool_return_model_enum" DEFAULT 'FIXED_SIMPLE' NOT NULL,
	"expected_return" text NOT NULL,
	"term" text NOT NULL,
	"min_borrower_plan_tier" "membership_plan_tier_enum" DEFAULT 'STARTER' NOT NULL,
	"borrower_type" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "lending_templates_legacy_pool_id_unique" UNIQUE("legacy_pool_id"),
	CONSTRAINT "lending_templates_source_opportunity_id_unique" UNIQUE("source_opportunity_id")
);
--> statement-breakpoint
CREATE TABLE "loan_applications" (
	"id" serial PRIMARY KEY NOT NULL,
	"borrower_id" integer NOT NULL,
	"template_id" integer,
	"opportunity_id" integer,
	"loan_type" "lending_loan_type_enum" DEFAULT 'P2P_LENDING' NOT NULL,
	"requested_amount" numeric(10, 2) NOT NULL,
	"purpose" text NOT NULL,
	"term" text NOT NULL,
	"repayment_schedule" text NOT NULL,
	"term_months" integer DEFAULT 12 NOT NULL,
	"borrower_appreciation_amount" numeric(10, 2) DEFAULT '0.00' NOT NULL,
	"repayment_frequency" "lending_repayment_frequency_enum" DEFAULT 'MONTHLY' NOT NULL,
	"processing_fee_pct" numeric(5, 2),
	"wwn_margin_pct" numeric(5, 2) DEFAULT '0.00' NOT NULL,
	"reserve_contribution_pct" numeric(5, 2) DEFAULT '0.00' NOT NULL,
	"platform_appreciation_share_pct" numeric(5, 2) DEFAULT '0.00' NOT NULL,
	"pricing_locked_at" timestamp,
	"total_repayable_amount" numeric(10, 2) DEFAULT '0' NOT NULL,
	"estimated_installment_amount" numeric(10, 2) DEFAULT '0' NOT NULL,
	"funder_opportunity_title" text,
	"notes" text,
	"status" "loan_application_status_enum" DEFAULT 'DRAFT' NOT NULL,
	"pool_assigned_at" timestamp,
	"pool_assigned_by_admin_id" integer,
	"pool_reassigned_at" timestamp,
	"pool_reassigned_by_admin_id" integer,
	"pool_reassignment_note" text,
	"reviewed_by_admin_id" integer,
	"reviewed_at" timestamp,
	"review_note" text,
	"approved_amount" numeric(10, 2),
	"reserved_at" timestamp,
	"reserved_by_admin_id" integer,
	"disbursed_at" timestamp,
	"disbursed_amount" numeric(10, 2),
	"disbursement_transaction_id" integer,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "loan_repayments" (
	"id" serial PRIMARY KEY NOT NULL,
	"loan_application_id" integer NOT NULL,
	"due_date" timestamp NOT NULL,
	"amount" numeric(10, 2) NOT NULL,
	"paid_amount" numeric(10, 2),
	"principal_paid_amount" numeric(10, 2) DEFAULT '0' NOT NULL,
	"return_paid_amount" numeric(10, 2) DEFAULT '0' NOT NULL,
	"fee_paid_amount" numeric(10, 2) DEFAULT '0' NOT NULL,
	"credited_to_pool_amount" numeric(10, 2) DEFAULT '0' NOT NULL,
	"paid_at" timestamp,
	"status" "repayment_status_enum" DEFAULT 'PENDING' NOT NULL,
	"method" "payment_method_enum",
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "member_spotlights" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer NOT NULL,
	"achievement" text NOT NULL,
	"featured" boolean DEFAULT false NOT NULL,
	"approved_at" timestamp,
	"created_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "member_spotlights_user_id_unique" UNIQUE("user_id")
);
--> statement-breakpoint
CREATE TABLE "membership_plans" (
	"id" serial PRIMARY KEY NOT NULL,
	"membership_type" "membership_type_enum" NOT NULL,
	"organization_type" "organization_type_enum",
	"tier" "membership_plan_tier_enum" NOT NULL,
	"name" text NOT NULL,
	"description" text,
	"price_monthly" numeric(10, 2) NOT NULL,
	"price_annual" numeric(10, 2) NOT NULL,
	"currency" text DEFAULT 'CAD' NOT NULL,
	"features" json DEFAULT '[]'::json NOT NULL,
	"entitlements" json DEFAULT '{}'::json NOT NULL,
	"is_active" boolean DEFAULT true NOT NULL,
	"sort_order" integer DEFAULT 0 NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "membership_subscriptions" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer NOT NULL,
	"plan_id" integer NOT NULL,
	"status" "membership_status_enum" DEFAULT 'PENDING' NOT NULL,
	"start_date" timestamp,
	"end_date" timestamp,
	"renews_at" timestamp,
	"cancelled_at" timestamp,
	"stripe_customer_id" text,
	"stripe_subscription_id" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "notification_deliveries" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer NOT NULL,
	"channel" "notification_channel_enum" NOT NULL,
	"category" "notification_category_enum" NOT NULL,
	"status" "notification_delivery_status_enum" NOT NULL,
	"idempotency_key" text NOT NULL,
	"notification_id" integer,
	"provider" text,
	"error_message" text,
	"metadata" json DEFAULT '{}'::json NOT NULL,
	"occurred_at" timestamp DEFAULT now() NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "notification_deliveries_idempotency_key_unique" UNIQUE("idempotency_key")
);
--> statement-breakpoint
CREATE TABLE "notifications" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer NOT NULL,
	"category" "notification_category_enum" DEFAULT 'SYSTEM' NOT NULL,
	"severity" "notification_severity_enum" DEFAULT 'INFO' NOT NULL,
	"title" text NOT NULL,
	"body" text NOT NULL,
	"read" boolean DEFAULT false NOT NULL,
	"read_at" timestamp,
	"action_label" text,
	"action_href" text,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "onboarding_activity_progress" (
	"id" serial PRIMARY KEY NOT NULL,
	"enrollment_id" integer NOT NULL,
	"activity_id" integer NOT NULL,
	"status" "onboarding_activity_status_enum" DEFAULT 'NOT_STARTED' NOT NULL,
	"started_at" timestamp,
	"completed_at" timestamp,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "onboarding_enrollments" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer NOT NULL,
	"program_id" integer NOT NULL,
	"enrolled_at" timestamp DEFAULT now() NOT NULL,
	"completed_at" timestamp,
	"last_activity_at" timestamp,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "onboarding_program_weeks" (
	"id" serial PRIMARY KEY NOT NULL,
	"program_id" integer NOT NULL,
	"week_number" integer NOT NULL,
	"title" text NOT NULL,
	"description" text,
	"sort_order" integer DEFAULT 0 NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "onboarding_programs" (
	"id" serial PRIMARY KEY NOT NULL,
	"title" text NOT NULL,
	"description" text,
	"target_membership_type" "membership_type_enum" DEFAULT 'INDIVIDUAL' NOT NULL,
	"number_of_weeks" integer NOT NULL,
	"duration_unit" "onboarding_program_duration_unit_enum" DEFAULT 'WEEK' NOT NULL,
	"start_date" timestamp,
	"end_date" timestamp,
	"status" "onboarding_program_status_enum" DEFAULT 'DRAFT' NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "onboarding_slides" (
	"id" serial PRIMARY KEY NOT NULL,
	"title" text NOT NULL,
	"subtitle" text,
	"body" text,
	"illustration_icon" text DEFAULT 'diamond-stone' NOT NULL,
	"illustration_url" text,
	"accent_color" text DEFAULT '#f27a4a' NOT NULL,
	"sort_order" integer DEFAULT 0 NOT NULL,
	"is_active" boolean DEFAULT true NOT NULL,
	"created_at" timestamp DEFAULT now(),
	"updated_at" timestamp DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "onboarding_week_activities" (
	"id" serial PRIMARY KEY NOT NULL,
	"week_id" integer NOT NULL,
	"title" text NOT NULL,
	"description" text,
	"duration_minutes" integer,
	"type" "onboarding_activity_type_enum" NOT NULL,
	"content" json,
	"sort_order" integer DEFAULT 0 NOT NULL,
	"is_required" boolean DEFAULT true NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "organization_bank_settings" (
	"id" serial PRIMARY KEY NOT NULL,
	"organization_id" integer NOT NULL,
	"eft_account_name" text NOT NULL,
	"eft_institution_number" text NOT NULL,
	"eft_transit_number" text NOT NULL,
	"eft_account_number" text NOT NULL,
	"interac_recipient_name" text NOT NULL,
	"interac_email" text NOT NULL,
	"cashout_min_amount" numeric(12, 2) NOT NULL,
	"cashout_fee_pct" numeric(6, 3) NOT NULL,
	"cashout_fee_fixed" numeric(12, 2) NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "organization_bank_settings_organization_id_unique" UNIQUE("organization_id")
);
--> statement-breakpoint
CREATE TABLE "organization_intakes" (
	"id" serial PRIMARY KEY NOT NULL,
	"organization_id" integer NOT NULL,
	"program_interests" text,
	"annual_budget" text,
	"staff_count" text,
	"goals" text,
	"additional_notes" text,
	"status" "intake_status_enum" DEFAULT 'PENDING' NOT NULL,
	"quote_amount" numeric(12, 2),
	"quote_note" text,
	"quote_link" text,
	"reviewed_by_admin_id" integer,
	"reviewed_at" timestamp,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "organization_invitations" (
	"id" serial PRIMARY KEY NOT NULL,
	"organization_id" integer NOT NULL,
	"email" text NOT NULL,
	"first_name" text,
	"last_name" text,
	"role" "org_member_role_enum" DEFAULT 'MEMBER' NOT NULL,
	"invited_by_user_id" integer,
	"token_hash" text NOT NULL,
	"status" "organization_invitation_status_enum" DEFAULT 'PENDING' NOT NULL,
	"expires_at" timestamp NOT NULL,
	"accepted_at" timestamp,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "organization_invitations_token_hash_unique" UNIQUE("token_hash")
);
--> statement-breakpoint
CREATE TABLE "organization_member_obligations" (
	"id" serial PRIMARY KEY NOT NULL,
	"organization_id" integer NOT NULL,
	"member_user_id" integer NOT NULL,
	"assigned_by_user_id" integer,
	"kind" "organization_member_obligation_kind_enum" DEFAULT 'TREASURY' NOT NULL,
	"title" text NOT NULL,
	"description" text,
	"amount" numeric(12, 2) NOT NULL,
	"currency" text DEFAULT 'CAD' NOT NULL,
	"status" "organization_member_obligation_status_enum" DEFAULT 'PENDING' NOT NULL,
	"due_date" timestamp,
	"paid_at" timestamp,
	"wallet_transaction_id" integer,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "organization_members" (
	"id" serial PRIMARY KEY NOT NULL,
	"organization_id" integer NOT NULL,
	"user_id" integer NOT NULL,
	"role" "org_member_role_enum" DEFAULT 'MEMBER' NOT NULL,
	"status" "organization_member_status_enum" DEFAULT 'ACTIVE' NOT NULL,
	"joined_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "organization_subscriptions" (
	"id" serial PRIMARY KEY NOT NULL,
	"organization_id" integer NOT NULL,
	"plan_id" integer NOT NULL,
	"status" "membership_status_enum" DEFAULT 'PENDING' NOT NULL,
	"start_date" timestamp,
	"end_date" timestamp,
	"renews_at" timestamp,
	"cancelled_at" timestamp,
	"stripe_customer_id" text,
	"stripe_subscription_id" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "organizations" (
	"id" serial PRIMARY KEY NOT NULL,
	"name" text NOT NULL,
	"type" text NOT NULL,
	"organization_type" "organization_type_enum",
	"operational_status" "organization_operational_status_enum" DEFAULT 'PENDING' NOT NULL,
	"description" text,
	"website" text,
	"contact_name" text,
	"contact_title" text,
	"business_type" text,
	"years_in_business" text,
	"annual_revenue" text,
	"province" text,
	"city" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "password_reset_tokens" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer NOT NULL,
	"token_hash" text NOT NULL,
	"expires_at" timestamp NOT NULL,
	"attempts" integer DEFAULT 0 NOT NULL,
	"is_exchange_token" boolean DEFAULT false NOT NULL,
	"used" boolean DEFAULT false NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "payment_webhook_events" (
	"id" serial PRIMARY KEY NOT NULL,
	"provider" "payment_provider_enum" NOT NULL,
	"event_id" text NOT NULL,
	"event_type" text NOT NULL,
	"payload" json NOT NULL,
	"status" "webhook_event_status_enum" DEFAULT 'PENDING' NOT NULL,
	"error" text,
	"attempts" integer DEFAULT 0 NOT NULL,
	"received_at" timestamp DEFAULT now() NOT NULL,
	"processed_at" timestamp
);
--> statement-breakpoint
CREATE TABLE "payments" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer NOT NULL,
	"amount" numeric(12, 2) NOT NULL,
	"currency" text DEFAULT 'CAD' NOT NULL,
	"method" "payment_method_enum" NOT NULL,
	"status" "payment_status_enum" DEFAULT 'PENDING' NOT NULL,
	"reference" text,
	"description" text,
	"metadata" json,
	"invoice_id" integer,
	"processed_at" timestamp,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "resource_categories" (
	"id" serial PRIMARY KEY NOT NULL,
	"name" text NOT NULL,
	"slug" text NOT NULL,
	"sort_order" integer DEFAULT 0 NOT NULL,
	CONSTRAINT "resource_categories_name_unique" UNIQUE("name"),
	CONSTRAINT "resource_categories_slug_unique" UNIQUE("slug")
);
--> statement-breakpoint
CREATE TABLE "resources" (
	"id" serial PRIMARY KEY NOT NULL,
	"title" text NOT NULL,
	"slug" text NOT NULL,
	"description" text,
	"resource_kind" "resource_kind_enum" DEFAULT 'FILE' NOT NULL,
	"category_id" integer NOT NULL,
	"file_url" text,
	"file_type" "resource_file_type_enum" DEFAULT 'PDF' NOT NULL,
	"file_size" integer,
	"cover_image_url" text,
	"author_name" text,
	"content_html" text,
	"show_to_individuals" boolean DEFAULT false NOT NULL,
	"show_to_investors" boolean DEFAULT true NOT NULL,
	"show_to_organizations" boolean DEFAULT true NOT NULL,
	"uploaded_by_id" integer,
	"is_published" boolean DEFAULT false NOT NULL,
	"published_at" timestamp,
	"downloads" integer DEFAULT 0 NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "resources_slug_unique" UNIQUE("slug")
);
--> statement-breakpoint
CREATE TABLE "savings_circles" (
	"id" serial PRIMARY KEY NOT NULL,
	"name" text NOT NULL,
	"description" text,
	"contribution_amount" numeric(10, 2) NOT NULL,
	"frequency" "contribution_frequency_enum" DEFAULT 'MONTHLY' NOT NULL,
	"helping_hand_multiplier" numeric(6, 2),
	"org_auto_deduct_enabled" boolean DEFAULT false NOT NULL,
	"is_free_to_join" boolean DEFAULT false NOT NULL,
	"max_members" integer NOT NULL,
	"forming_timeout_days" integer DEFAULT 14 NOT NULL,
	"status" "circle_status_enum" DEFAULT 'UPCOMING' NOT NULL,
	"start_date" timestamp,
	"end_date" timestamp,
	"organization_id" integer,
	"created_by_admin_id" integer,
	"created_by_organization_member_user_id" integer,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "savings_contributions" (
	"id" serial PRIMARY KEY NOT NULL,
	"circle_id" integer NOT NULL,
	"user_id" integer NOT NULL,
	"due_date" timestamp NOT NULL,
	"paid_at" timestamp,
	"amount" numeric(10, 2) NOT NULL,
	"status" "contribution_status_enum" DEFAULT 'PENDING' NOT NULL,
	"method" "payment_method_enum",
	"receipt_ref" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "user_notification_preferences" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer NOT NULL,
	"channel" "notification_channel_enum" NOT NULL,
	"category" "notification_category_enum" NOT NULL,
	"enabled" boolean NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "user_push_tokens" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer NOT NULL,
	"platform" "push_platform_enum" DEFAULT 'EXPO' NOT NULL,
	"token" text NOT NULL,
	"device_name" text,
	"device_id" text,
	"is_enabled" boolean DEFAULT true NOT NULL,
	"last_seen_at" timestamp DEFAULT now() NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "user_push_tokens_token_unique" UNIQUE("token")
);
--> statement-breakpoint
CREATE TABLE "users" (
	"id" serial PRIMARY KEY NOT NULL,
	"email" text NOT NULL,
	"username" text,
	"phone" text,
	"password_hash" text NOT NULL,
	"first_name" text NOT NULL,
	"last_name" text,
	"segment" "user_segment_enum",
	"membership_type" "membership_type_enum",
	"membership_status" "membership_status_enum" DEFAULT 'PENDING' NOT NULL,
	"role" text DEFAULT 'member',
	"user_role" "user_role_enum" DEFAULT 'MEMBER' NOT NULL,
	"is_email_verified" boolean DEFAULT false NOT NULL,
	"bio" text,
	"avatar_url" text,
	"city" text,
	"province" text,
	"age_range" text,
	"occupation" text,
	"business_name" text,
	"business_type" text,
	"years_in_business" text,
	"annual_revenue" text,
	"how_heard" text,
	"goals" text,
	"investor_category" "investor_category_enum",
	"organization_id" integer,
	"onboarding_completed_at" timestamp,
	"last_login_at" timestamp,
	"auth_token_version" integer DEFAULT 0 NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "users_email_unique" UNIQUE("email"),
	CONSTRAINT "users_username_unique" UNIQUE("username")
);
--> statement-breakpoint
CREATE TABLE "verification_tokens" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer NOT NULL,
	"code_hash" text NOT NULL,
	"expires_at" timestamp NOT NULL,
	"attempts" integer DEFAULT 0 NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "wallet_payment_intents" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer NOT NULL,
	"wallet_id" integer NOT NULL,
	"provider" "payment_provider_enum" DEFAULT 'MANUAL' NOT NULL,
	"type" "wallet_payment_intent_type_enum" NOT NULL,
	"amount" numeric(12, 2) NOT NULL,
	"currency" text DEFAULT 'CAD' NOT NULL,
	"status" "wallet_payment_intent_status_enum" DEFAULT 'CREATED' NOT NULL,
	"provider_reference" text,
	"metadata" json DEFAULT '{}'::json NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "wallet_transactions" (
	"id" serial PRIMARY KEY NOT NULL,
	"type" "wallet_transaction_type_enum" NOT NULL,
	"idempotency_key" text,
	"from_wallet_id" integer,
	"to_wallet_id" integer,
	"amount" numeric(12, 2) NOT NULL,
	"metadata" json DEFAULT '{}'::json NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "wallets" (
	"id" serial PRIMARY KEY NOT NULL,
	"owner_type" "wallet_owner_type_enum" NOT NULL,
	"owner_id" integer NOT NULL,
	"currency" text DEFAULT 'CAD' NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "wwn_credit_scores" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer NOT NULL,
	"score" integer NOT NULL,
	"computed_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "wwn_credit_scores_user_id_unique" UNIQUE("user_id")
);
--> statement-breakpoint
ALTER TABLE "academy_courses" ADD CONSTRAINT "academy_courses_category_id_academy_categories_id_fk" FOREIGN KEY ("category_id") REFERENCES "public"."academy_categories"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "asca_contributions" ADD CONSTRAINT "asca_contributions_asca_id_ascas_id_fk" FOREIGN KEY ("asca_id") REFERENCES "public"."ascas"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "asca_contributions" ADD CONSTRAINT "asca_contributions_member_id_asca_members_id_fk" FOREIGN KEY ("member_id") REFERENCES "public"."asca_members"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "asca_contributions" ADD CONSTRAINT "asca_contributions_transaction_id_wallet_transactions_id_fk" FOREIGN KEY ("transaction_id") REFERENCES "public"."wallet_transactions"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "asca_members" ADD CONSTRAINT "asca_members_asca_id_ascas_id_fk" FOREIGN KEY ("asca_id") REFERENCES "public"."ascas"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "asca_members" ADD CONSTRAINT "asca_members_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "ascas" ADD CONSTRAINT "ascas_investment_id_investment_opportunities_id_fk" FOREIGN KEY ("investment_id") REFERENCES "public"."investment_opportunities"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "ascas" ADD CONSTRAINT "ascas_escrow_wallet_id_wallets_id_fk" FOREIGN KEY ("escrow_wallet_id") REFERENCES "public"."wallets"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "circle_memberships" ADD CONSTRAINT "circle_memberships_circle_id_savings_circles_id_fk" FOREIGN KEY ("circle_id") REFERENCES "public"."savings_circles"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "circle_memberships" ADD CONSTRAINT "circle_memberships_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "circle_payouts" ADD CONSTRAINT "circle_payouts_circle_id_savings_circles_id_fk" FOREIGN KEY ("circle_id") REFERENCES "public"."savings_circles"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "course_interests" ADD CONSTRAINT "course_interests_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "course_interests" ADD CONSTRAINT "course_interests_course_id_academy_courses_id_fk" FOREIGN KEY ("course_id") REFERENCES "public"."academy_courses"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "credit_score_factors" ADD CONSTRAINT "credit_score_factors_credit_score_id_wwn_credit_scores_id_fk" FOREIGN KEY ("credit_score_id") REFERENCES "public"."wwn_credit_scores"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "discussion_replies" ADD CONSTRAINT "discussion_replies_discussion_id_discussions_id_fk" FOREIGN KEY ("discussion_id") REFERENCES "public"."discussions"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "discussion_replies" ADD CONSTRAINT "discussion_replies_author_id_users_id_fk" FOREIGN KEY ("author_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "discussions" ADD CONSTRAINT "discussions_author_id_users_id_fk" FOREIGN KEY ("author_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "event_rsvps" ADD CONSTRAINT "event_rsvps_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "event_rsvps" ADD CONSTRAINT "event_rsvps_event_id_community_events_id_fk" FOREIGN KEY ("event_id") REFERENCES "public"."community_events"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "helping_hand_applications" ADD CONSTRAINT "helping_hand_applications_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "helping_hand_applications" ADD CONSTRAINT "helping_hand_applications_qualifying_circle_id_savings_circles_id_fk" FOREIGN KEY ("qualifying_circle_id") REFERENCES "public"."savings_circles"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "helping_hand_applications" ADD CONSTRAINT "helping_hand_applications_qualifying_payout_id_circle_payouts_id_fk" FOREIGN KEY ("qualifying_payout_id") REFERENCES "public"."circle_payouts"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "helping_hand_applications" ADD CONSTRAINT "helping_hand_applications_disbursement_transaction_id_wallet_transactions_id_fk" FOREIGN KEY ("disbursement_transaction_id") REFERENCES "public"."wallet_transactions"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "helping_hand_repayments" ADD CONSTRAINT "helping_hand_repayments_helping_hand_application_id_helping_hand_applications_id_fk" FOREIGN KEY ("helping_hand_application_id") REFERENCES "public"."helping_hand_applications"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "investment_intent_reservations" ADD CONSTRAINT "investment_intent_reservations_interest_id_investment_interests_id_fk" FOREIGN KEY ("interest_id") REFERENCES "public"."investment_interests"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "investment_intent_reservations" ADD CONSTRAINT "investment_intent_reservations_wallet_id_wallets_id_fk" FOREIGN KEY ("wallet_id") REFERENCES "public"."wallets"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "investment_intent_reservations" ADD CONSTRAINT "investment_intent_reservations_finalized_transaction_id_wallet_transactions_id_fk" FOREIGN KEY ("finalized_transaction_id") REFERENCES "public"."wallet_transactions"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "investment_interests" ADD CONSTRAINT "investment_interests_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "investment_interests" ADD CONSTRAINT "investment_interests_opportunity_id_investment_opportunities_id_fk" FOREIGN KEY ("opportunity_id") REFERENCES "public"."investment_opportunities"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "investment_transactions" ADD CONSTRAINT "investment_transactions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "investment_transactions" ADD CONSTRAINT "investment_transactions_opportunity_id_investment_opportunities_id_fk" FOREIGN KEY ("opportunity_id") REFERENCES "public"."investment_opportunities"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "invoices" ADD CONSTRAINT "invoices_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "invoices" ADD CONSTRAINT "invoices_subscription_id_membership_subscriptions_id_fk" FOREIGN KEY ("subscription_id") REFERENCES "public"."membership_subscriptions"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lending_commitments" ADD CONSTRAINT "lending_commitments_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lending_commitments" ADD CONSTRAINT "lending_commitments_opportunity_id_lending_opportunities_id_fk" FOREIGN KEY ("opportunity_id") REFERENCES "public"."lending_opportunities"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lending_commitments" ADD CONSTRAINT "lending_commitments_status_changed_by_admin_id_users_id_fk" FOREIGN KEY ("status_changed_by_admin_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lending_opportunities" ADD CONSTRAINT "lending_opportunities_template_id_lending_templates_id_fk" FOREIGN KEY ("template_id") REFERENCES "public"."lending_templates"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lending_opportunity_return_allocations" ADD CONSTRAINT "lending_opportunity_return_allocations_return_event_id_lending_opportunity_return_events_id_fk" FOREIGN KEY ("return_event_id") REFERENCES "public"."lending_opportunity_return_events"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lending_opportunity_return_allocations" ADD CONSTRAINT "lending_opportunity_return_allocations_commitment_id_lending_commitments_id_fk" FOREIGN KEY ("commitment_id") REFERENCES "public"."lending_commitments"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lending_opportunity_return_allocations" ADD CONSTRAINT "lending_opportunity_return_allocations_opportunity_id_lending_opportunities_id_fk" FOREIGN KEY ("opportunity_id") REFERENCES "public"."lending_opportunities"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lending_opportunity_return_allocations" ADD CONSTRAINT "lending_opportunity_return_allocations_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lending_opportunity_return_events" ADD CONSTRAINT "lending_opportunity_return_events_opportunity_id_lending_opportunities_id_fk" FOREIGN KEY ("opportunity_id") REFERENCES "public"."lending_opportunities"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lending_opportunity_return_events" ADD CONSTRAINT "lending_opportunity_return_events_repayment_id_loan_repayments_id_fk" FOREIGN KEY ("repayment_id") REFERENCES "public"."loan_repayments"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lending_opportunity_return_events" ADD CONSTRAINT "lending_opportunity_return_events_loan_application_id_loan_applications_id_fk" FOREIGN KEY ("loan_application_id") REFERENCES "public"."loan_applications"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lending_opportunity_return_events" ADD CONSTRAINT "lending_opportunity_return_events_wallet_transaction_id_wallet_transactions_id_fk" FOREIGN KEY ("wallet_transaction_id") REFERENCES "public"."wallet_transactions"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lending_templates" ADD CONSTRAINT "lending_templates_economics_updated_by_admin_id_users_id_fk" FOREIGN KEY ("economics_updated_by_admin_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "loan_applications" ADD CONSTRAINT "loan_applications_borrower_id_users_id_fk" FOREIGN KEY ("borrower_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "loan_applications" ADD CONSTRAINT "loan_applications_template_id_lending_templates_id_fk" FOREIGN KEY ("template_id") REFERENCES "public"."lending_templates"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "loan_applications" ADD CONSTRAINT "loan_applications_pool_assigned_by_admin_id_users_id_fk" FOREIGN KEY ("pool_assigned_by_admin_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "loan_applications" ADD CONSTRAINT "loan_applications_pool_reassigned_by_admin_id_users_id_fk" FOREIGN KEY ("pool_reassigned_by_admin_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "loan_applications" ADD CONSTRAINT "loan_applications_reserved_by_admin_id_users_id_fk" FOREIGN KEY ("reserved_by_admin_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "loan_applications" ADD CONSTRAINT "loan_applications_disbursement_transaction_id_wallet_transactions_id_fk" FOREIGN KEY ("disbursement_transaction_id") REFERENCES "public"."wallet_transactions"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "loan_repayments" ADD CONSTRAINT "loan_repayments_loan_application_id_loan_applications_id_fk" FOREIGN KEY ("loan_application_id") REFERENCES "public"."loan_applications"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "member_spotlights" ADD CONSTRAINT "member_spotlights_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "membership_subscriptions" ADD CONSTRAINT "membership_subscriptions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "membership_subscriptions" ADD CONSTRAINT "membership_subscriptions_plan_id_membership_plans_id_fk" FOREIGN KEY ("plan_id") REFERENCES "public"."membership_plans"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "notification_deliveries" ADD CONSTRAINT "notification_deliveries_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "notification_deliveries" ADD CONSTRAINT "notification_deliveries_notification_id_notifications_id_fk" FOREIGN KEY ("notification_id") REFERENCES "public"."notifications"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "notifications" ADD CONSTRAINT "notifications_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "onboarding_activity_progress" ADD CONSTRAINT "onboarding_activity_progress_enrollment_id_onboarding_enrollments_id_fk" FOREIGN KEY ("enrollment_id") REFERENCES "public"."onboarding_enrollments"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "onboarding_activity_progress" ADD CONSTRAINT "onboarding_activity_progress_activity_id_onboarding_week_activities_id_fk" FOREIGN KEY ("activity_id") REFERENCES "public"."onboarding_week_activities"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "onboarding_enrollments" ADD CONSTRAINT "onboarding_enrollments_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "onboarding_enrollments" ADD CONSTRAINT "onboarding_enrollments_program_id_onboarding_programs_id_fk" FOREIGN KEY ("program_id") REFERENCES "public"."onboarding_programs"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "onboarding_program_weeks" ADD CONSTRAINT "onboarding_program_weeks_program_id_onboarding_programs_id_fk" FOREIGN KEY ("program_id") REFERENCES "public"."onboarding_programs"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "onboarding_week_activities" ADD CONSTRAINT "onboarding_week_activities_week_id_onboarding_program_weeks_id_fk" FOREIGN KEY ("week_id") REFERENCES "public"."onboarding_program_weeks"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "organization_bank_settings" ADD CONSTRAINT "organization_bank_settings_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "organization_intakes" ADD CONSTRAINT "organization_intakes_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "organization_invitations" ADD CONSTRAINT "organization_invitations_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "organization_invitations" ADD CONSTRAINT "organization_invitations_invited_by_user_id_users_id_fk" FOREIGN KEY ("invited_by_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "organization_member_obligations" ADD CONSTRAINT "organization_member_obligations_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "organization_member_obligations" ADD CONSTRAINT "organization_member_obligations_member_user_id_users_id_fk" FOREIGN KEY ("member_user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "organization_member_obligations" ADD CONSTRAINT "organization_member_obligations_assigned_by_user_id_users_id_fk" FOREIGN KEY ("assigned_by_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "organization_members" ADD CONSTRAINT "organization_members_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "organization_members" ADD CONSTRAINT "organization_members_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "organization_subscriptions" ADD CONSTRAINT "organization_subscriptions_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "organization_subscriptions" ADD CONSTRAINT "organization_subscriptions_plan_id_membership_plans_id_fk" FOREIGN KEY ("plan_id") REFERENCES "public"."membership_plans"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "password_reset_tokens" ADD CONSTRAINT "password_reset_tokens_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "payments" ADD CONSTRAINT "payments_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("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 "resources" ADD CONSTRAINT "resources_category_id_resource_categories_id_fk" FOREIGN KEY ("category_id") REFERENCES "public"."resource_categories"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "savings_circles" ADD CONSTRAINT "savings_circles_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "savings_circles" ADD CONSTRAINT "savings_circles_created_by_organization_member_user_id_users_id_fk" FOREIGN KEY ("created_by_organization_member_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "savings_contributions" ADD CONSTRAINT "savings_contributions_circle_id_savings_circles_id_fk" FOREIGN KEY ("circle_id") REFERENCES "public"."savings_circles"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "savings_contributions" ADD CONSTRAINT "savings_contributions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "user_notification_preferences" ADD CONSTRAINT "user_notification_preferences_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "user_push_tokens" ADD CONSTRAINT "user_push_tokens_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "users" ADD CONSTRAINT "users_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "verification_tokens" ADD CONSTRAINT "verification_tokens_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "wallet_payment_intents" ADD CONSTRAINT "wallet_payment_intents_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "wallet_payment_intents" ADD CONSTRAINT "wallet_payment_intents_wallet_id_wallets_id_fk" FOREIGN KEY ("wallet_id") REFERENCES "public"."wallets"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "wallet_transactions" ADD CONSTRAINT "wallet_transactions_from_wallet_id_wallets_id_fk" FOREIGN KEY ("from_wallet_id") REFERENCES "public"."wallets"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "wallet_transactions" ADD CONSTRAINT "wallet_transactions_to_wallet_id_wallets_id_fk" FOREIGN KEY ("to_wallet_id") REFERENCES "public"."wallets"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "wwn_credit_scores" ADD CONSTRAINT "wwn_credit_scores_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "asca_contributions_member_idx" ON "asca_contributions" USING btree ("member_id");--> statement-breakpoint
CREATE INDEX "asca_contributions_asca_idx" ON "asca_contributions" USING btree ("asca_id");--> statement-breakpoint
CREATE INDEX "asca_contributions_status_idx" ON "asca_contributions" USING btree ("status");--> statement-breakpoint
CREATE UNIQUE INDEX "asca_members_asca_user_idx" ON "asca_members" USING btree ("asca_id","user_id");--> statement-breakpoint
CREATE INDEX "asca_members_user_idx" ON "asca_members" USING btree ("user_id");--> statement-breakpoint
CREATE INDEX "ascas_status_idx" ON "ascas" USING btree ("status");--> statement-breakpoint
CREATE INDEX "ascas_type_idx" ON "ascas" USING btree ("type");--> statement-breakpoint
CREATE INDEX "ascas_investment_idx" ON "ascas" USING btree ("investment_id");--> statement-breakpoint
CREATE UNIQUE INDEX "circle_memberships_circle_user_uidx" ON "circle_memberships" USING btree ("circle_id","user_id");--> statement-breakpoint
CREATE UNIQUE INDEX "circle_memberships_circle_pos_uidx" ON "circle_memberships" USING btree ("circle_id","position");--> statement-breakpoint
CREATE INDEX "circle_memberships_user_idx" ON "circle_memberships" USING btree ("user_id");--> statement-breakpoint
CREATE UNIQUE INDEX "circle_payouts_circle_user_pos_uidx" ON "circle_payouts" USING btree ("circle_id","user_id","position");--> statement-breakpoint
CREATE INDEX "circle_payouts_circle_status_sched_idx" ON "circle_payouts" USING btree ("circle_id","status","scheduled_date");--> statement-breakpoint
CREATE UNIQUE INDEX "course_interests_user_course_uidx" ON "course_interests" USING btree ("user_id","course_id");--> statement-breakpoint
CREATE UNIQUE INDEX "event_rsvps_user_event_uidx" ON "event_rsvps" USING btree ("user_id","event_id");--> statement-breakpoint
CREATE INDEX "hh_apps_user_created_idx" ON "helping_hand_applications" USING btree ("user_id","created_at");--> statement-breakpoint
CREATE INDEX "hh_apps_status_created_idx" ON "helping_hand_applications" USING btree ("status","created_at");--> statement-breakpoint
CREATE INDEX "hh_repayments_app_due_idx" ON "helping_hand_repayments" USING btree ("helping_hand_application_id","due_date");--> statement-breakpoint
CREATE INDEX "hh_repayments_status_due_idx" ON "helping_hand_repayments" USING btree ("status","due_date");--> statement-breakpoint
CREATE UNIQUE INDEX "investment_intent_reservations_interest_uidx" ON "investment_intent_reservations" USING btree ("interest_id");--> statement-breakpoint
CREATE INDEX "invest_reservations_wallet_status_idx" ON "investment_intent_reservations" USING btree ("wallet_id","status");--> statement-breakpoint
CREATE INDEX "invest_reservations_status_created_idx" ON "investment_intent_reservations" USING btree ("status","created_at");--> statement-breakpoint
CREATE UNIQUE INDEX "investment_interests_user_opp_uidx" ON "investment_interests" USING btree ("user_id","opportunity_id");--> statement-breakpoint
CREATE INDEX "invest_interests_opp_status_idx" ON "investment_interests" USING btree ("opportunity_id","status");--> statement-breakpoint
CREATE INDEX "invest_interests_user_created_idx" ON "investment_interests" USING btree ("user_id","created_at");--> statement-breakpoint
CREATE INDEX "invest_opps_status_featured_deadline_idx" ON "investment_opportunities" USING btree ("status","featured","deadline");--> statement-breakpoint
CREATE INDEX "invest_txs_user_processed_idx" ON "investment_transactions" USING btree ("user_id","processed_at");--> statement-breakpoint
CREATE INDEX "invest_txs_user_opp_type_idx" ON "investment_transactions" USING btree ("user_id","opportunity_id","type");--> statement-breakpoint
CREATE INDEX "invoices_user_created_idx" ON "invoices" USING btree ("user_id","created_at");--> statement-breakpoint
CREATE INDEX "invoices_sub_created_idx" ON "invoices" USING btree ("subscription_id","created_at");--> statement-breakpoint
CREATE INDEX "lend_commit_opp_status_created_idx" ON "lending_commitments" USING btree ("opportunity_id","status","created_at");--> statement-breakpoint
CREATE INDEX "lend_commit_user_status_created_idx" ON "lending_commitments" USING btree ("user_id","status","created_at");--> statement-breakpoint
CREATE UNIQUE INDEX "lend_commit_user_opp_active_uidx" ON "lending_commitments" USING btree ("user_id","opportunity_id") WHERE "lending_commitments"."status" in ('PENDING', 'CONFIRMED');--> statement-breakpoint
CREATE INDEX "lending_opps_status_created_idx" ON "lending_opportunities" USING btree ("status","created_at");--> statement-breakpoint
CREATE INDEX "lending_opps_template_status_created_idx" ON "lending_opportunities" USING btree ("template_id","status","created_at");--> statement-breakpoint
CREATE UNIQUE INDEX "lending_opportunity_return_allocations_event_commitment_uidx" ON "lending_opportunity_return_allocations" USING btree ("return_event_id","commitment_id");--> statement-breakpoint
CREATE UNIQUE INDEX "lending_opportunity_return_events_repayment_wallet_uidx" ON "lending_opportunity_return_events" USING btree ("repayment_id","wallet_transaction_id");--> statement-breakpoint
CREATE INDEX "lending_templates_status_created_idx" ON "lending_templates" USING btree ("status","created_at");--> statement-breakpoint
CREATE INDEX "loan_apps_borrower_created_idx" ON "loan_applications" USING btree ("borrower_id","created_at");--> statement-breakpoint
CREATE INDEX "loan_apps_opp_status_created_idx" ON "loan_applications" USING btree ("opportunity_id","status","created_at");--> statement-breakpoint
CREATE UNIQUE INDEX "loan_apps_borrower_active_uidx" ON "loan_applications" USING btree ("borrower_id") WHERE "loan_applications"."status" in ('DRAFT', 'SUBMITTED', 'WAITLISTED', 'UNDER_REVIEW', 'APPROVED', 'APPROVED_RESERVED', 'FUNDED', 'REPAYING');--> statement-breakpoint
CREATE INDEX "loan_repayments_app_due_idx" ON "loan_repayments" USING btree ("loan_application_id","due_date");--> statement-breakpoint
CREATE INDEX "loan_repayments_status_due_idx" ON "loan_repayments" USING btree ("status","due_date");--> statement-breakpoint
CREATE INDEX "member_sub_user_status_created_idx" ON "membership_subscriptions" USING btree ("user_id","status","created_at");--> statement-breakpoint
CREATE UNIQUE INDEX "member_sub_user_active_uidx" ON "membership_subscriptions" USING btree ("user_id") WHERE "membership_subscriptions"."status" in ('PENDING', 'ACTIVE');--> statement-breakpoint
CREATE INDEX "notification_deliveries_user_occurred_idx" ON "notification_deliveries" USING btree ("user_id","occurred_at");--> statement-breakpoint
CREATE INDEX "notification_deliveries_occurred_idx" ON "notification_deliveries" USING btree ("occurred_at");--> statement-breakpoint
CREATE INDEX "notifications_user_read_created_idx" ON "notifications" USING btree ("user_id","read","created_at","id");--> statement-breakpoint
CREATE INDEX "notifications_user_created_idx" ON "notifications" USING btree ("user_id","created_at","id");--> statement-breakpoint
CREATE INDEX "notifications_created_idx" ON "notifications" USING btree ("created_at");--> statement-breakpoint
CREATE INDEX "notifications_read_read_at_idx" ON "notifications" USING btree ("read","read_at");--> statement-breakpoint
CREATE UNIQUE INDEX "onboarding_activity_progress_unique_idx" ON "onboarding_activity_progress" USING btree ("enrollment_id","activity_id");--> statement-breakpoint
CREATE INDEX "onboarding_activity_progress_enrollment_idx" ON "onboarding_activity_progress" USING btree ("enrollment_id");--> statement-breakpoint
CREATE UNIQUE INDEX "onboarding_enrollments_user_program_unique_idx" ON "onboarding_enrollments" USING btree ("user_id","program_id");--> statement-breakpoint
CREATE INDEX "onboarding_enrollments_user_idx" ON "onboarding_enrollments" USING btree ("user_id");--> statement-breakpoint
CREATE INDEX "onboarding_enrollments_program_idx" ON "onboarding_enrollments" USING btree ("program_id");--> statement-breakpoint
CREATE UNIQUE INDEX "onboarding_program_weeks_unique_idx" ON "onboarding_program_weeks" USING btree ("program_id","week_number");--> statement-breakpoint
CREATE INDEX "onboarding_program_weeks_order_idx" ON "onboarding_program_weeks" USING btree ("program_id","sort_order");--> statement-breakpoint
CREATE INDEX "onboarding_programs_status_idx" ON "onboarding_programs" USING btree ("status");--> statement-breakpoint
CREATE INDEX "onboarding_programs_target_membership_idx" ON "onboarding_programs" USING btree ("target_membership_type");--> statement-breakpoint
CREATE INDEX "onboarding_programs_created_idx" ON "onboarding_programs" USING btree ("created_at");--> statement-breakpoint
CREATE INDEX "onboarding_week_activities_order_idx" ON "onboarding_week_activities" USING btree ("week_id","sort_order");--> statement-breakpoint
CREATE UNIQUE INDEX "onboarding_week_activities_unique_title_idx" ON "onboarding_week_activities" USING btree ("week_id","title");--> statement-breakpoint
CREATE INDEX "org_bank_settings_org_updated_idx" ON "organization_bank_settings" USING btree ("organization_id","updated_at");--> statement-breakpoint
CREATE INDEX "org_intakes_org_created_idx" ON "organization_intakes" USING btree ("organization_id","created_at");--> statement-breakpoint
CREATE INDEX "org_intakes_org_updated_idx" ON "organization_intakes" USING btree ("organization_id","updated_at");--> statement-breakpoint
CREATE INDEX "org_invites_org_status_created_idx" ON "organization_invitations" USING btree ("organization_id","status","created_at");--> statement-breakpoint
CREATE UNIQUE INDEX "org_invites_org_email_pending_uidx" ON "organization_invitations" USING btree ("organization_id","email") WHERE "organization_invitations"."status" = 'PENDING';--> statement-breakpoint
CREATE INDEX "org_member_obligation_org_member_status_idx" ON "organization_member_obligations" USING btree ("organization_id","member_user_id","status");--> statement-breakpoint
CREATE INDEX "org_member_obligation_org_created_idx" ON "organization_member_obligations" USING btree ("organization_id","created_at");--> statement-breakpoint
CREATE UNIQUE INDEX "org_members_org_user_uidx" ON "organization_members" USING btree ("organization_id","user_id");--> statement-breakpoint
CREATE INDEX "org_sub_org_status_created_idx" ON "organization_subscriptions" USING btree ("organization_id","status","created_at");--> statement-breakpoint
CREATE UNIQUE INDEX "org_sub_org_active_uidx" ON "organization_subscriptions" USING btree ("organization_id") WHERE "organization_subscriptions"."status" in ('PENDING', 'ACTIVE');--> statement-breakpoint
CREATE UNIQUE INDEX "payment_webhook_events_provider_event_uidx" ON "payment_webhook_events" USING btree ("provider","event_id");--> statement-breakpoint
CREATE INDEX "webhook_events_status_received_idx" ON "payment_webhook_events" USING btree ("status","received_at");--> statement-breakpoint
CREATE INDEX "payments_invoice_created_idx" ON "payments" USING btree ("invoice_id","created_at");--> statement-breakpoint
CREATE INDEX "payments_user_created_idx" ON "payments" USING btree ("user_id","created_at");--> statement-breakpoint
CREATE INDEX "payments_status_created_idx" ON "payments" USING btree ("status","created_at");--> statement-breakpoint
CREATE INDEX "savings_circles_org_idx" ON "savings_circles" USING btree ("organization_id","created_at");--> statement-breakpoint
CREATE INDEX "savings_circles_org_status_idx" ON "savings_circles" USING btree ("organization_id","status");--> statement-breakpoint
CREATE UNIQUE INDEX "savings_contributions_circle_user_due_uidx" ON "savings_contributions" USING btree ("circle_id","user_id","due_date");--> statement-breakpoint
CREATE INDEX "savings_contrib_circle_status_due_idx" ON "savings_contributions" USING btree ("circle_id","status","due_date");--> statement-breakpoint
CREATE INDEX "savings_contrib_user_due_idx" ON "savings_contributions" USING btree ("user_id","due_date");--> statement-breakpoint
CREATE UNIQUE INDEX "user_notification_prefs_user_channel_category_uidx" ON "user_notification_preferences" USING btree ("user_id","channel","category");--> statement-breakpoint
CREATE INDEX "push_tokens_user_enabled_idx" ON "user_push_tokens" USING btree ("user_id","is_enabled");--> statement-breakpoint
CREATE INDEX "users_membership_created_idx" ON "users" USING btree ("membership_status","membership_type","created_at");--> statement-breakpoint
CREATE UNIQUE INDEX "wallet_payment_intents_provider_ref_uidx" ON "wallet_payment_intents" USING btree ("provider","provider_reference");--> statement-breakpoint
CREATE INDEX "wallet_intents_user_created_idx" ON "wallet_payment_intents" USING btree ("user_id","created_at");--> statement-breakpoint
CREATE INDEX "wallet_intents_wallet_status_idx" ON "wallet_payment_intents" USING btree ("wallet_id","status","created_at");--> statement-breakpoint
CREATE UNIQUE INDEX "wallet_transactions_idempotency_uidx" ON "wallet_transactions" USING btree ("idempotency_key");--> statement-breakpoint
CREATE INDEX "wallet_txs_from_created_idx" ON "wallet_transactions" USING btree ("from_wallet_id","created_at");--> statement-breakpoint
CREATE INDEX "wallet_txs_to_created_idx" ON "wallet_transactions" USING btree ("to_wallet_id","created_at");--> statement-breakpoint
CREATE INDEX "wallet_txs_type_wallets_idx" ON "wallet_transactions" USING btree ("type","from_wallet_id","to_wallet_id");--> statement-breakpoint
CREATE UNIQUE INDEX "wallets_owner_uidx" ON "wallets" USING btree ("owner_type","owner_id");