CREATE TABLE "company_settings" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "cycle_start_date" bigint NOT NULL, "status" text DEFAULT 'draft' NOT NULL, "created_at" bigint NOT NULL, "updated_at" bigint NOT NULL, "created_by" uuid NOT NULL, "updated_by" uuid NOT NULL ); --> statement-breakpoint CREATE TABLE "cycles" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "employee_id" uuid NOT NULL, "purpose" text NOT NULL, "cycle_number" integer NOT NULL, "status" text DEFAULT 'draft' NOT NULL, "created_at" bigint NOT NULL, "updated_at" bigint NOT NULL, "created_by" uuid NOT NULL, "updated_by" uuid NOT NULL ); --> statement-breakpoint CREATE TABLE "cycle_weekdays" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "cycle_id" uuid NOT NULL, "weekday" text NOT NULL, "start_branch_id" uuid NOT NULL, "end_branch_id" uuid NOT NULL, "route_geometry" jsonb NOT NULL ); --> statement-breakpoint CREATE TABLE "cycle_destinations" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "cycle_weekday_id" uuid NOT NULL, "customer_id" uuid NOT NULL, "sort_order" integer NOT NULL ); --> statement-breakpoint CREATE TABLE "plans" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "employee_id" uuid NOT NULL, "purpose" text NOT NULL, "date" bigint NOT NULL, "start_branch_id" uuid NOT NULL, "end_branch_id" uuid NOT NULL, "route_geometry" jsonb NOT NULL, "status" text DEFAULT 'draft' NOT NULL, "created_at" bigint NOT NULL, "updated_at" bigint NOT NULL, "created_by" uuid NOT NULL, "updated_by" uuid NOT NULL ); --> statement-breakpoint CREATE TABLE "plan_destinations" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "plan_id" uuid NOT NULL, "customer_id" uuid NOT NULL, "sort_order" integer NOT NULL ); --> statement-breakpoint CREATE TABLE "plan_invoices" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "plan_id" uuid NOT NULL, "invoice_id" uuid NOT NULL ); --> statement-breakpoint CREATE TABLE "plan_packing_slips" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "plan_id" uuid NOT NULL, "packing_slip_id" uuid NOT NULL ); --> statement-breakpoint ALTER TABLE "company_settings" ADD CONSTRAINT "company_settings_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "company_settings" ADD CONSTRAINT "company_settings_updated_by_users_id_fk" FOREIGN KEY ("updated_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "cycles" ADD CONSTRAINT "cycles_employee_id_employees_id_fk" FOREIGN KEY ("employee_id") REFERENCES "public"."employees"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "cycles" ADD CONSTRAINT "cycles_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "cycles" ADD CONSTRAINT "cycles_updated_by_users_id_fk" FOREIGN KEY ("updated_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "cycle_weekdays" ADD CONSTRAINT "cycle_weekdays_cycle_id_cycles_id_fk" FOREIGN KEY ("cycle_id") REFERENCES "public"."cycles"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "cycle_weekdays" ADD CONSTRAINT "cycle_weekdays_start_branch_id_branches_id_fk" FOREIGN KEY ("start_branch_id") REFERENCES "public"."branches"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "cycle_weekdays" ADD CONSTRAINT "cycle_weekdays_end_branch_id_branches_id_fk" FOREIGN KEY ("end_branch_id") REFERENCES "public"."branches"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "cycle_destinations" ADD CONSTRAINT "cycle_destinations_cycle_weekday_id_cycle_weekdays_id_fk" FOREIGN KEY ("cycle_weekday_id") REFERENCES "public"."cycle_weekdays"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "cycle_destinations" ADD CONSTRAINT "cycle_destinations_customer_id_customers_id_fk" FOREIGN KEY ("customer_id") REFERENCES "public"."customers"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "plans" ADD CONSTRAINT "plans_employee_id_employees_id_fk" FOREIGN KEY ("employee_id") REFERENCES "public"."employees"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "plans" ADD CONSTRAINT "plans_start_branch_id_branches_id_fk" FOREIGN KEY ("start_branch_id") REFERENCES "public"."branches"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "plans" ADD CONSTRAINT "plans_end_branch_id_branches_id_fk" FOREIGN KEY ("end_branch_id") REFERENCES "public"."branches"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "plans" ADD CONSTRAINT "plans_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "plans" ADD CONSTRAINT "plans_updated_by_users_id_fk" FOREIGN KEY ("updated_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "plan_destinations" ADD CONSTRAINT "plan_destinations_plan_id_plans_id_fk" FOREIGN KEY ("plan_id") REFERENCES "public"."plans"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "plan_destinations" ADD CONSTRAINT "plan_destinations_customer_id_customers_id_fk" FOREIGN KEY ("customer_id") REFERENCES "public"."customers"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "plan_invoices" ADD CONSTRAINT "plan_invoices_plan_id_plans_id_fk" FOREIGN KEY ("plan_id") REFERENCES "public"."plans"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "plan_invoices" ADD CONSTRAINT "plan_invoices_invoice_id_sales_invoices_id_fk" FOREIGN KEY ("invoice_id") REFERENCES "public"."sales_invoices"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "plan_packing_slips" ADD CONSTRAINT "plan_packing_slips_plan_id_plans_id_fk" FOREIGN KEY ("plan_id") REFERENCES "public"."plans"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "plan_packing_slips" ADD CONSTRAINT "plan_packing_slips_packing_slip_id_packing_slips_id_fk" FOREIGN KEY ("packing_slip_id") REFERENCES "public"."packing_slips"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint CREATE UNIQUE INDEX "cycles_employee_purpose_number_live_unique" ON "cycles" USING btree ("employee_id","purpose","cycle_number") WHERE "status" <> 'archived';--> statement-breakpoint CREATE INDEX "cycles_employee_id_idx" ON "cycles" USING btree ("employee_id");--> statement-breakpoint CREATE UNIQUE INDEX "cycle_weekdays_cycle_weekday_unique" ON "cycle_weekdays" USING btree ("cycle_id","weekday");--> statement-breakpoint CREATE INDEX "cycle_weekdays_cycle_id_idx" ON "cycle_weekdays" USING btree ("cycle_id");--> statement-breakpoint CREATE INDEX "cycle_destinations_weekday_id_idx" ON "cycle_destinations" USING btree ("cycle_weekday_id");--> statement-breakpoint CREATE UNIQUE INDEX "plans_employee_purpose_date_live_unique" ON "plans" USING btree ("employee_id","purpose","date") WHERE "status" <> 'archived';--> statement-breakpoint CREATE INDEX "plans_employee_id_idx" ON "plans" USING btree ("employee_id");--> statement-breakpoint CREATE UNIQUE INDEX "plan_destinations_plan_customer_unique" ON "plan_destinations" USING btree ("plan_id","customer_id");--> statement-breakpoint CREATE INDEX "plan_destinations_plan_id_idx" ON "plan_destinations" USING btree ("plan_id");--> statement-breakpoint CREATE UNIQUE INDEX "plan_invoices_plan_invoice_unique" ON "plan_invoices" USING btree ("plan_id","invoice_id");--> statement-breakpoint CREATE INDEX "plan_invoices_plan_id_idx" ON "plan_invoices" USING btree ("plan_id");--> statement-breakpoint CREATE UNIQUE INDEX "plan_packing_slips_plan_slip_unique" ON "plan_packing_slips" USING btree ("plan_id","packing_slip_id");--> statement-breakpoint CREATE INDEX "plan_packing_slips_plan_id_idx" ON "plan_packing_slips" USING btree ("plan_id");--> statement-breakpoint INSERT INTO "privilege_keys" ("code", "label", "sort_order") VALUES ('CONFIGURATION.SETTING', 'Company settings', 13), ('SALES.CYCLE', 'Sales cycles', 14), ('SALES.PLAN', 'Sales plans', 15), ('LOGISTICS.CYCLE', 'Logistics cycles', 16), ('LOGISTICS.PLAN', 'Logistics plans', 17);