CREATE TABLE "admin_messages" ( "id" serial PRIMARY KEY NOT NULL, "text" text NOT NULL, "audience" text NOT NULL, "company_id" integer, "user_id" integer, "recipients" integer DEFAULT 0 NOT NULL, "sent_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "blacklist" ( "id" serial PRIMARY KEY NOT NULL, "company_id" integer NOT NULL, "phone" text NOT NULL, "added_by_id" integer, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "client_leads" ( "client_id" integer NOT NULL, "lead_id" integer NOT NULL, "state" text DEFAULT 'offered' NOT NULL, "shown_at" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "clients" ( "id" serial PRIMARY KEY NOT NULL, "company_id" integer NOT NULL, "name" text NOT NULL, "phone" text, "kind" text NOT NULL, "request" jsonb DEFAULT '{}'::jsonb NOT NULL, "source" text, "assignee_id" integer NOT NULL, "status_id" integer NOT NULL, "reject_reason_id" integer, "next_action_at" timestamp with time zone, "next_action_note" text, "next_action_notified_at" timestamp with time zone, "status_changed_at" timestamp with time zone DEFAULT now() NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "companies" ( "id" serial PRIMARY KEY NOT NULL, "name" text NOT NULL, "plan" text NOT NULL, "billing_model" text DEFAULT 'fixed' NOT NULL, "planned_plan" text, "planned_billing_model" text, "planned_from" date, "subscription_ends_at" timestamp with time zone, "grace_days" integer DEFAULT 15 NOT NULL, "access_enabled" boolean DEFAULT false NOT NULL, "setup_fee_invoiced" boolean DEFAULT false NOT NULL, "settings" jsonb NOT NULL, "last_activity_at" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "deals" ( "id" serial PRIMARY KEY NOT NULL, "company_id" integer NOT NULL, "lead_id" integer, "client_id" integer, "agent_id" integer NOT NULL, "commission" numeric(14, 2) NOT NULL, "currency" text NOT NULL, "deal_date" date NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "email_tokens" ( "token_hash" text PRIMARY KEY NOT NULL, "user_id" integer NOT NULL, "kind" text NOT NULL, "expires_at" timestamp with time zone NOT NULL ); --> statement-breakpoint CREATE TABLE "events" ( "id" serial PRIMARY KEY NOT NULL, "company_id" integer NOT NULL, "lead_id" integer, "client_id" integer, "user_id" integer, "type" text NOT NULL, "old_value" text, "new_value" text, "text" text, "at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "filters" ( "id" serial PRIMARY KEY NOT NULL, "company_id" integer NOT NULL, "owner_user_id" integer, "name" text NOT NULL, "enabled" boolean DEFAULT true NOT NULL, "params" jsonb NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "invites" ( "token" text PRIMARY KEY NOT NULL, "company_id" integer NOT NULL, "role" text DEFAULT 'agent' NOT NULL, "created_by_id" integer NOT NULL, "revoked_at" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "invoices" ( "id" serial PRIMARY KEY NOT NULL, "company_id" integer NOT NULL, "kind" text NOT NULL, "period_start" date, "period_end" date, "amount" numeric(14, 2) NOT NULL, "currency" text DEFAULT 'USD' NOT NULL, "details" jsonb, "status" text DEFAULT 'pending' NOT NULL, "paid_at" timestamp with time zone, "paid_method" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "lead_notifications" ( "id" serial PRIMARY KEY NOT NULL, "lead_id" integer NOT NULL, "user_id" integer NOT NULL, "chat_id" bigint, "message_id" integer, "has_photo" boolean DEFAULT false NOT NULL, "sent_at" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "leads" ( "id" serial PRIMARY KEY NOT NULL, "company_id" integer NOT NULL, "listing_id" integer, "source" text NOT NULL, "url" text, "title" text NOT NULL, "description" text, "deal_type" text, "property_type" text, "region" text, "district" text, "address" text, "price" numeric(14, 2), "currency" text, "rooms" integer, "area" numeric(10, 2), "floor" integer, "floors_total" integer, "photos" jsonb DEFAULT '[]'::jsonb NOT NULL, "author_name" text, "phone" text, "assignee_id" integer, "status_id" integer NOT NULL, "reject_reason_id" integer, "next_action_at" timestamp with time zone, "next_action_note" text, "next_action_notified_at" timestamp with time zone, "taken_at" timestamp with time zone, "status_changed_at" timestamp with time zone DEFAULT now() NOT NULL, "pool_warned_at" timestamp with time zone, "previous_lead_id" integer, "created_by_id" integer, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "listings" ( "id" serial PRIMARY KEY NOT NULL, "external_id" text NOT NULL, "url" text NOT NULL, "title" text NOT NULL, "description" text, "deal_type" text, "property_type" text, "region" text, "district" text, "address" text, "price" numeric(14, 2), "currency" text, "rooms" integer, "area" numeric(10, 2), "floor" integer, "floors_total" integer, "photos" jsonb DEFAULT '[]'::jsonb NOT NULL, "author_name" text, "phone" text, "is_agency" boolean DEFAULT false NOT NULL, "published_at" timestamp with time zone, "active" boolean DEFAULT true NOT NULL, "removed_at" timestamp with time zone, "last_checked_at" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "outbox" ( "id" serial PRIMARY KEY NOT NULL, "kind" text NOT NULL, "user_id" integer NOT NULL, "lead_id" integer, "payload" jsonb DEFAULT '{}'::jsonb NOT NULL, "attempts" integer DEFAULT 0 NOT NULL, "run_at" timestamp with time zone DEFAULT now() NOT NULL, "done_at" timestamp with time zone, "error" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "parser_runs" ( "id" serial PRIMARY KEY NOT NULL, "started_at" timestamp with time zone DEFAULT now() NOT NULL, "finished_at" timestamp with time zone, "status" text DEFAULT 'running' NOT NULL, "found_new" integer DEFAULT 0 NOT NULL, "errors" text ); --> statement-breakpoint CREATE TABLE "parser_state" ( "category_id" integer PRIMARY KEY NOT NULL, "baseline_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "payments" ( "id" serial PRIMARY KEY NOT NULL, "invoice_id" integer NOT NULL, "provider" text NOT NULL, "external_id" text, "amount" numeric(14, 2) NOT NULL, "currency" text NOT NULL, "status" text NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "pipeline_statuses" ( "id" serial PRIMARY KEY NOT NULL, "company_id" integer NOT NULL, "pipeline" text NOT NULL, "code" text, "name" text NOT NULL, "position" integer NOT NULL, "pool_return" boolean DEFAULT false NOT NULL, "deleted_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "reject_reasons" ( "id" serial PRIMARY KEY NOT NULL, "company_id" integer NOT NULL, "pipeline" text NOT NULL, "code" text, "name" text NOT NULL, "position" integer NOT NULL, "deleted_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "seen_listings" ( "external_id" text PRIMARY KEY NOT NULL, "seen_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "sessions" ( "token_hash" text PRIMARY KEY NOT NULL, "user_id" integer NOT NULL, "acting_company_id" integer, "expires_at" timestamp with time zone NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "users" ( "id" serial PRIMARY KEY NOT NULL, "company_id" integer, "role" text NOT NULL, "name" text NOT NULL, "telegram_id" bigint, "telegram_username" text, "email" text, "email_verified_at" timestamp with time zone, "password_hash" text, "lang" text DEFAULT 'ru' NOT NULL, "quiet_from" text, "quiet_to" text, "notifications_paused" boolean DEFAULT false NOT NULL, "telegram_link_token" text, "removed_at" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint ALTER TABLE "admin_messages" ADD CONSTRAINT "admin_messages_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "admin_messages" ADD CONSTRAINT "admin_messages_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 "blacklist" ADD CONSTRAINT "blacklist_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "blacklist" ADD CONSTRAINT "blacklist_added_by_id_users_id_fk" FOREIGN KEY ("added_by_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "client_leads" ADD CONSTRAINT "client_leads_client_id_clients_id_fk" FOREIGN KEY ("client_id") REFERENCES "public"."clients"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "client_leads" ADD CONSTRAINT "client_leads_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "clients" ADD CONSTRAINT "clients_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "clients" ADD CONSTRAINT "clients_assignee_id_users_id_fk" FOREIGN KEY ("assignee_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "clients" ADD CONSTRAINT "clients_status_id_pipeline_statuses_id_fk" FOREIGN KEY ("status_id") REFERENCES "public"."pipeline_statuses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "clients" ADD CONSTRAINT "clients_reject_reason_id_reject_reasons_id_fk" FOREIGN KEY ("reject_reason_id") REFERENCES "public"."reject_reasons"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "deals" ADD CONSTRAINT "deals_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "deals" ADD CONSTRAINT "deals_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "deals" ADD CONSTRAINT "deals_client_id_clients_id_fk" FOREIGN KEY ("client_id") REFERENCES "public"."clients"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "deals" ADD CONSTRAINT "deals_agent_id_users_id_fk" FOREIGN KEY ("agent_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "email_tokens" ADD CONSTRAINT "email_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 "events" ADD CONSTRAINT "events_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "events" ADD CONSTRAINT "events_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "events" ADD CONSTRAINT "events_client_id_clients_id_fk" FOREIGN KEY ("client_id") REFERENCES "public"."clients"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "events" ADD CONSTRAINT "events_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 "filters" ADD CONSTRAINT "filters_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "filters" ADD CONSTRAINT "filters_owner_user_id_users_id_fk" FOREIGN KEY ("owner_user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "invites" ADD CONSTRAINT "invites_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "invites" ADD CONSTRAINT "invites_created_by_id_users_id_fk" FOREIGN KEY ("created_by_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "invoices" ADD CONSTRAINT "invoices_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "lead_notifications" ADD CONSTRAINT "lead_notifications_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "lead_notifications" ADD CONSTRAINT "lead_notifications_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 "leads" ADD CONSTRAINT "leads_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "leads" ADD CONSTRAINT "leads_listing_id_listings_id_fk" FOREIGN KEY ("listing_id") REFERENCES "public"."listings"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "leads" ADD CONSTRAINT "leads_assignee_id_users_id_fk" FOREIGN KEY ("assignee_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "leads" ADD CONSTRAINT "leads_status_id_pipeline_statuses_id_fk" FOREIGN KEY ("status_id") REFERENCES "public"."pipeline_statuses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "leads" ADD CONSTRAINT "leads_reject_reason_id_reject_reasons_id_fk" FOREIGN KEY ("reject_reason_id") REFERENCES "public"."reject_reasons"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "leads" ADD CONSTRAINT "leads_created_by_id_users_id_fk" FOREIGN KEY ("created_by_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "outbox" ADD CONSTRAINT "outbox_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "outbox" ADD CONSTRAINT "outbox_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade 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 "pipeline_statuses" ADD CONSTRAINT "pipeline_statuses_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "reject_reasons" ADD CONSTRAINT "reject_reasons_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "sessions" ADD CONSTRAINT "sessions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "sessions" ADD CONSTRAINT "sessions_acting_company_id_companies_id_fk" FOREIGN KEY ("acting_company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "users" ADD CONSTRAINT "users_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint CREATE UNIQUE INDEX "blacklist_uq" ON "blacklist" USING btree ("company_id","phone");--> statement-breakpoint CREATE UNIQUE INDEX "client_leads_uq" ON "client_leads" USING btree ("client_id","lead_id");--> statement-breakpoint CREATE INDEX "clients_company_idx" ON "clients" USING btree ("company_id","status_id");--> statement-breakpoint CREATE INDEX "deals_company_date_idx" ON "deals" USING btree ("company_id","deal_date");--> statement-breakpoint CREATE UNIQUE INDEX "deals_lead_uq" ON "deals" USING btree ("lead_id");--> statement-breakpoint CREATE UNIQUE INDEX "deals_client_uq" ON "deals" USING btree ("client_id");--> statement-breakpoint CREATE INDEX "events_lead_idx" ON "events" USING btree ("lead_id");--> statement-breakpoint CREATE INDEX "events_client_idx" ON "events" USING btree ("client_id");--> statement-breakpoint CREATE INDEX "filters_company_idx" ON "filters" USING btree ("company_id");--> statement-breakpoint CREATE INDEX "invoices_company_idx" ON "invoices" USING btree ("company_id");--> statement-breakpoint CREATE INDEX "lead_notifications_lead_idx" ON "lead_notifications" USING btree ("lead_id");--> statement-breakpoint CREATE INDEX "leads_company_status_idx" ON "leads" USING btree ("company_id","status_id");--> statement-breakpoint CREATE INDEX "leads_company_assignee_idx" ON "leads" USING btree ("company_id","assignee_id");--> statement-breakpoint CREATE UNIQUE INDEX "leads_company_listing_uq" ON "leads" USING btree ("company_id","listing_id") WHERE "leads"."previous_lead_id" is null and "leads"."listing_id" is not null;--> statement-breakpoint CREATE UNIQUE INDEX "listings_external_uq" ON "listings" USING btree ("external_id");--> statement-breakpoint CREATE INDEX "listings_phone_idx" ON "listings" USING btree ("phone");--> statement-breakpoint CREATE INDEX "outbox_due_idx" ON "outbox" USING btree ("run_at") WHERE "outbox"."done_at" is null;--> statement-breakpoint CREATE INDEX "statuses_company_idx" ON "pipeline_statuses" USING btree ("company_id","pipeline");--> statement-breakpoint CREATE UNIQUE INDEX "users_telegram_uq" ON "users" USING btree ("telegram_id");--> statement-breakpoint CREATE UNIQUE INDEX "users_email_uq" ON "users" USING btree (lower("email"));--> statement-breakpoint CREATE INDEX "users_company_idx" ON "users" USING btree ("company_id");