CREATE TYPE "public"."alert_severity" AS ENUM('INFO', 'WARNING', 'CRITICAL');--> statement-breakpoint
CREATE TYPE "public"."alert_status" AS ENUM('OPEN', 'ACKNOWLEDGED', 'RESOLVED');--> statement-breakpoint
CREATE TYPE "public"."alert_type" AS ENUM('DEALER_INACTIVE', 'DEALER_REVENUE_DROP', 'PRODUCT_NO_REVENUE', 'PRODUCT_VOLUME_DROP', 'TERRITORY_NO_REVENUE', 'TERRITORY_REVENUE_DROP', 'STAFF_UNDERPERFORM');--> statement-breakpoint
CREATE TYPE "public"."class_tier" AS ENUM('A', 'B', 'C', 'D', 'E');--> statement-breakpoint
CREATE TYPE "public"."customer_stage" AS ENUM('LEAD', 'PROSPECT', 'DEALER');--> statement-breakpoint
CREATE TYPE "public"."order_status" AS ENUM('DRAFT', 'CONFIRMED', 'CANCELLED');--> statement-breakpoint
CREATE TYPE "public"."period_type" AS ENUM('MONTH', 'QUARTER', 'YEAR');--> statement-breakpoint
CREATE TYPE "public"."plan_item_status" AS ENUM('PLANNED', 'DONE', 'MISSED');--> statement-breakpoint
CREATE TYPE "public"."product_role" AS ENUM('CORE', 'POTENTIAL', 'RECOVERY', 'NORMAL');--> statement-breakpoint
CREATE TYPE "public"."program_type" AS ENUM('NEW_PRODUCT', 'FOCUS_PRODUCT', 'CAMPAIGN');--> statement-breakpoint
CREATE TYPE "public"."target_scope" AS ENUM('COMPANY', 'TERRITORY', 'USER', 'CUSTOMER');--> statement-breakpoint
CREATE TYPE "public"."territory_level" AS ENUM('REGION', 'PROVINCE', 'DISTRICT', 'WARD');--> statement-breakpoint
CREATE TYPE "public"."user_role" AS ENUM('DIRECTOR', 'MANAGER', 'SALES');--> statement-breakpoint
CREATE TABLE "alerts" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"type" "alert_type" NOT NULL,
	"severity" "alert_severity" DEFAULT 'WARNING' NOT NULL,
	"title" text NOT NULL,
	"message" text NOT NULL,
	"entity_type" text NOT NULL,
	"entity_id" uuid,
	"period_key" text NOT NULL,
	"metrics" jsonb,
	"status" "alert_status" DEFAULT 'OPEN' NOT NULL,
	"acknowledged_by" uuid,
	"acknowledged_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "audit_logs" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"user_id" uuid,
	"action" text NOT NULL,
	"entity_type" text NOT NULL,
	"entity_id" text,
	"payload" jsonb,
	"ip_address" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "channels" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"code" text NOT NULL,
	"name" text NOT NULL,
	"is_active" boolean DEFAULT true NOT NULL
);
--> statement-breakpoint
CREATE TABLE "check_ins" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"user_id" uuid NOT NULL,
	"customer_id" uuid,
	"plan_item_id" uuid,
	"checked_at" timestamp with time zone DEFAULT now() NOT NULL,
	"lat" double precision NOT NULL,
	"lng" double precision NOT NULL,
	"accuracy_meters" double precision,
	"distance_meters" double precision,
	"out_of_range" boolean DEFAULT false NOT NULL,
	"photo_path" text,
	"note" text
);
--> statement-breakpoint
CREATE TABLE "customer_classifications" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"customer_id" uuid NOT NULL,
	"period_key" text NOT NULL,
	"avg_monthly_revenue" bigint DEFAULT 0 NOT NULL,
	"revenue_tier" "class_tier",
	"sku_count" integer DEFAULT 0 NOT NULL,
	"sku_tier" "class_tier",
	"reorder_count" integer DEFAULT 0 NOT NULL,
	"months_purchased_in_quarter" integer DEFAULT 0 NOT NULL,
	"months_purchased_in_year" integer DEFAULT 0 NOT NULL,
	"months_inactive" integer DEFAULT 0 NOT NULL,
	"computed_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "customer_locations" (
	"customer_id" uuid PRIMARY KEY NOT NULL,
	"tenant_id" uuid NOT NULL,
	"lat" double precision NOT NULL,
	"lng" double precision NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "customer_stage_history" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"customer_id" uuid NOT NULL,
	"from_stage" "customer_stage",
	"to_stage" "customer_stage" NOT NULL,
	"changed_by" uuid,
	"note" text,
	"changed_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "customers" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"code" text NOT NULL,
	"org_name" text NOT NULL,
	"contact_name" text,
	"channel_id" uuid,
	"territory_id" uuid,
	"address" text,
	"phone" text,
	"email" text,
	"owner_id" uuid,
	"stage" "customer_stage" DEFAULT 'LEAD' NOT NULL,
	"dealer_since" date,
	"is_key" boolean DEFAULT false NOT NULL,
	"note" text,
	"is_active" boolean DEFAULT true NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "daily_reports" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"user_id" uuid NOT NULL,
	"report_date" date NOT NULL,
	"completion_pct" integer DEFAULT 0 NOT NULL,
	"route_compliant" boolean DEFAULT true NOT NULL,
	"content" text,
	"results" text,
	"proposals" text,
	"pending" text,
	"next_steps" text,
	"submitted_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "events" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"name" text NOT NULL,
	"event_date" date NOT NULL,
	"kind" text DEFAULT 'Chuyên đề' NOT NULL,
	"territory_id" uuid,
	"owner_id" uuid,
	"invited_count" integer DEFAULT 0 NOT NULL,
	"attended_count" integer DEFAULT 0 NOT NULL,
	"result_order_count" integer DEFAULT 0 NOT NULL,
	"result_revenue" bigint DEFAULT 0 NOT NULL,
	"note" text
);
--> statement-breakpoint
CREATE TABLE "forecasts" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"period_key" text NOT NULL,
	"product_id" uuid NOT NULL,
	"forecast_qty" integer DEFAULT 0 NOT NULL,
	"adjusted_qty" integer,
	"method" text DEFAULT 'MOVING_AVG_3M' NOT NULL,
	"confidence" double precision,
	"note" text,
	"computed_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "kpi_monthly" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"user_id" uuid NOT NULL,
	"period_key" text NOT NULL,
	"revenue" bigint DEFAULT 0 NOT NULL,
	"revenue_target" bigint DEFAULT 0 NOT NULL,
	"achievement_pct" integer DEFAULT 0 NOT NULL,
	"order_count" integer DEFAULT 0 NOT NULL,
	"visit_count" integer DEFAULT 0 NOT NULL,
	"plan_compliance_pct" integer DEFAULT 0 NOT NULL,
	"new_customer_count" integer DEFAULT 0 NOT NULL,
	"active_dealer_count" integer DEFAULT 0 NOT NULL,
	"computed_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "order_lines" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"order_id" uuid NOT NULL,
	"product_id" uuid NOT NULL,
	"qty_sold" integer DEFAULT 0 NOT NULL,
	"qty_promo" integer DEFAULT 0 NOT NULL,
	"unit_price" bigint DEFAULT 0 NOT NULL,
	"amount" bigint DEFAULT 0 NOT NULL,
	"program_id" uuid,
	"order_date" date NOT NULL
);
--> statement-breakpoint
CREATE TABLE "orders" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"code" text NOT NULL,
	"customer_id" uuid NOT NULL,
	"order_date" date NOT NULL,
	"sales_user_id" uuid,
	"territory_id" uuid,
	"channel_id" uuid,
	"status" "order_status" DEFAULT 'CONFIRMED' NOT NULL,
	"total_amount" bigint DEFAULT 0 NOT NULL,
	"promo_value" bigint DEFAULT 0 NOT NULL,
	"note" text,
	"created_by" uuid,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "plan_items" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"plan_id" uuid NOT NULL,
	"plan_date" date NOT NULL,
	"content" text NOT NULL,
	"objective" text,
	"time_slot" text,
	"customer_id" uuid,
	"territory_id" uuid,
	"route_note" text,
	"status" "plan_item_status" DEFAULT 'PLANNED' NOT NULL,
	"sort_order" integer DEFAULT 0 NOT NULL
);
--> statement-breakpoint
CREATE TABLE "product_groups" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"code" text NOT NULL,
	"name" text NOT NULL
);
--> statement-breakpoint
CREATE TABLE "products" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"code" text NOT NULL,
	"name" text NOT NULL,
	"group_id" uuid,
	"technology_id" uuid,
	"unit" text DEFAULT 'Hộp' NOT NULL,
	"unit_price" bigint DEFAULT 0 NOT NULL,
	"role" "product_role" DEFAULT 'NORMAL' NOT NULL,
	"is_active" boolean DEFAULT true NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "program_products" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"program_id" uuid NOT NULL,
	"product_id" uuid NOT NULL
);
--> statement-breakpoint
CREATE TABLE "programs" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"code" text NOT NULL,
	"name" text NOT NULL,
	"type" "program_type" NOT NULL,
	"start_date" date,
	"end_date" date,
	"revenue_target" bigint DEFAULT 0,
	"qty_target" integer DEFAULT 0,
	"is_active" boolean DEFAULT true NOT NULL
);
--> statement-breakpoint
CREATE TABLE "targets" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"period_type" "period_type" NOT NULL,
	"period_key" text NOT NULL,
	"scope_type" "target_scope" NOT NULL,
	"scope_id" uuid,
	"revenue_target" bigint DEFAULT 0 NOT NULL,
	"order_target" integer DEFAULT 0 NOT NULL,
	"new_customer_target" integer DEFAULT 0 NOT NULL,
	"sku_target" integer DEFAULT 0 NOT NULL,
	"note" text
);
--> statement-breakpoint
CREATE TABLE "technologies" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"code" text NOT NULL,
	"name" text NOT NULL
);
--> statement-breakpoint
CREATE TABLE "tenants" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"name" text NOT NULL,
	"code" text NOT NULL,
	"tax_code" text,
	"is_active" boolean DEFAULT true NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "tenants_code_unique" UNIQUE("code")
);
--> statement-breakpoint
CREATE TABLE "territories" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"code" text NOT NULL,
	"name" text NOT NULL,
	"level" "territory_level" NOT NULL,
	"parent_id" uuid,
	"is_active" boolean DEFAULT true NOT NULL
);
--> statement-breakpoint
CREATE TABLE "user_territories" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"user_id" uuid NOT NULL,
	"territory_id" uuid NOT NULL
);
--> statement-breakpoint
CREATE TABLE "users" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"email" text NOT NULL,
	"password_hash" text NOT NULL,
	"full_name" text NOT NULL,
	"phone" text,
	"role" "user_role" DEFAULT 'SALES' NOT NULL,
	"territory_id" uuid,
	"is_active" boolean DEFAULT true NOT NULL,
	"last_login_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "weekly_plans" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"user_id" uuid NOT NULL,
	"week_start" date NOT NULL,
	"objective" text,
	"summary" text,
	"next_week_objective" text,
	"submitted_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "check_ins" ADD CONSTRAINT "check_ins_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "customer_classifications" ADD CONSTRAINT "customer_classifications_customer_id_customers_id_fk" FOREIGN KEY ("customer_id") REFERENCES "public"."customers"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "customer_locations" ADD CONSTRAINT "customer_locations_customer_id_customers_id_fk" FOREIGN KEY ("customer_id") REFERENCES "public"."customers"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "customer_stage_history" ADD CONSTRAINT "customer_stage_history_customer_id_customers_id_fk" FOREIGN KEY ("customer_id") REFERENCES "public"."customers"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "daily_reports" ADD CONSTRAINT "daily_reports_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "forecasts" ADD CONSTRAINT "forecasts_product_id_products_id_fk" FOREIGN KEY ("product_id") REFERENCES "public"."products"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "kpi_monthly" ADD CONSTRAINT "kpi_monthly_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "order_lines" ADD CONSTRAINT "order_lines_order_id_orders_id_fk" FOREIGN KEY ("order_id") REFERENCES "public"."orders"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "order_lines" ADD CONSTRAINT "order_lines_product_id_products_id_fk" FOREIGN KEY ("product_id") REFERENCES "public"."products"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "orders" ADD CONSTRAINT "orders_customer_id_customers_id_fk" FOREIGN KEY ("customer_id") REFERENCES "public"."customers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "plan_items" ADD CONSTRAINT "plan_items_plan_id_weekly_plans_id_fk" FOREIGN KEY ("plan_id") REFERENCES "public"."weekly_plans"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "program_products" ADD CONSTRAINT "program_products_program_id_programs_id_fk" FOREIGN KEY ("program_id") REFERENCES "public"."programs"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "program_products" ADD CONSTRAINT "program_products_product_id_products_id_fk" FOREIGN KEY ("product_id") REFERENCES "public"."products"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "user_territories" ADD CONSTRAINT "user_territories_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_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "weekly_plans" ADD CONSTRAINT "weekly_plans_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "alerts_uniq" ON "alerts" USING btree ("tenant_id","type","entity_id","period_key");--> statement-breakpoint
CREATE INDEX "alerts_tenant_status_idx" ON "alerts" USING btree ("tenant_id","status","created_at");--> statement-breakpoint
CREATE INDEX "audit_tenant_created_idx" ON "audit_logs" USING btree ("tenant_id","created_at");--> statement-breakpoint
CREATE UNIQUE INDEX "channels_tenant_code_idx" ON "channels" USING btree ("tenant_id","code");--> statement-breakpoint
CREATE INDEX "check_ins_tenant_user_time_idx" ON "check_ins" USING btree ("tenant_id","user_id","checked_at");--> statement-breakpoint
CREATE INDEX "check_ins_customer_idx" ON "check_ins" USING btree ("customer_id");--> statement-breakpoint
CREATE UNIQUE INDEX "customer_classifications_uniq" ON "customer_classifications" USING btree ("customer_id","period_key");--> statement-breakpoint
CREATE INDEX "customer_classifications_period_idx" ON "customer_classifications" USING btree ("tenant_id","period_key");--> statement-breakpoint
CREATE INDEX "customer_stage_history_cust_idx" ON "customer_stage_history" USING btree ("customer_id");--> statement-breakpoint
CREATE UNIQUE INDEX "customers_tenant_code_idx" ON "customers" USING btree ("tenant_id","code");--> statement-breakpoint
CREATE INDEX "customers_tenant_stage_idx" ON "customers" USING btree ("tenant_id","stage");--> statement-breakpoint
CREATE INDEX "customers_tenant_owner_idx" ON "customers" USING btree ("tenant_id","owner_id");--> statement-breakpoint
CREATE INDEX "customers_tenant_territory_idx" ON "customers" USING btree ("tenant_id","territory_id");--> statement-breakpoint
CREATE UNIQUE INDEX "daily_reports_uniq" ON "daily_reports" USING btree ("user_id","report_date");--> statement-breakpoint
CREATE INDEX "daily_reports_tenant_date_idx" ON "daily_reports" USING btree ("tenant_id","report_date");--> statement-breakpoint
CREATE INDEX "events_tenant_date_idx" ON "events" USING btree ("tenant_id","event_date");--> statement-breakpoint
CREATE UNIQUE INDEX "forecasts_uniq" ON "forecasts" USING btree ("tenant_id","period_key","product_id");--> statement-breakpoint
CREATE UNIQUE INDEX "kpi_monthly_uniq" ON "kpi_monthly" USING btree ("user_id","period_key");--> statement-breakpoint
CREATE INDEX "order_lines_order_idx" ON "order_lines" USING btree ("order_id");--> statement-breakpoint
CREATE INDEX "order_lines_tenant_product_date_idx" ON "order_lines" USING btree ("tenant_id","product_id","order_date");--> statement-breakpoint
CREATE INDEX "order_lines_tenant_program_idx" ON "order_lines" USING btree ("tenant_id","program_id");--> statement-breakpoint
CREATE UNIQUE INDEX "orders_tenant_code_idx" ON "orders" USING btree ("tenant_id","code");--> statement-breakpoint
CREATE INDEX "orders_tenant_date_idx" ON "orders" USING btree ("tenant_id","order_date");--> statement-breakpoint
CREATE INDEX "orders_tenant_customer_idx" ON "orders" USING btree ("tenant_id","customer_id");--> statement-breakpoint
CREATE INDEX "orders_tenant_sales_idx" ON "orders" USING btree ("tenant_id","sales_user_id");--> statement-breakpoint
CREATE INDEX "plan_items_plan_idx" ON "plan_items" USING btree ("plan_id");--> statement-breakpoint
CREATE INDEX "plan_items_tenant_date_idx" ON "plan_items" USING btree ("tenant_id","plan_date");--> statement-breakpoint
CREATE UNIQUE INDEX "product_groups_tenant_code_idx" ON "product_groups" USING btree ("tenant_id","code");--> statement-breakpoint
CREATE UNIQUE INDEX "products_tenant_code_idx" ON "products" USING btree ("tenant_id","code");--> statement-breakpoint
CREATE INDEX "products_tenant_group_idx" ON "products" USING btree ("tenant_id","group_id");--> statement-breakpoint
CREATE UNIQUE INDEX "program_products_uniq" ON "program_products" USING btree ("program_id","product_id");--> statement-breakpoint
CREATE UNIQUE INDEX "programs_tenant_code_idx" ON "programs" USING btree ("tenant_id","code");--> statement-breakpoint
CREATE UNIQUE INDEX "targets_uniq" ON "targets" USING btree ("tenant_id","period_key","scope_type","scope_id");--> statement-breakpoint
CREATE INDEX "targets_tenant_period_idx" ON "targets" USING btree ("tenant_id","period_key");--> statement-breakpoint
CREATE UNIQUE INDEX "technologies_tenant_code_idx" ON "technologies" USING btree ("tenant_id","code");--> statement-breakpoint
CREATE UNIQUE INDEX "territories_tenant_code_idx" ON "territories" USING btree ("tenant_id","code");--> statement-breakpoint
CREATE INDEX "territories_parent_idx" ON "territories" USING btree ("parent_id");--> statement-breakpoint
CREATE UNIQUE INDEX "user_territories_uniq" ON "user_territories" USING btree ("user_id","territory_id");--> statement-breakpoint
CREATE UNIQUE INDEX "users_tenant_email_idx" ON "users" USING btree ("tenant_id","email");--> statement-breakpoint
CREATE INDEX "users_tenant_role_idx" ON "users" USING btree ("tenant_id","role");--> statement-breakpoint
CREATE UNIQUE INDEX "weekly_plans_uniq" ON "weekly_plans" USING btree ("user_id","week_start");