CREATE TABLE "custom_kpi_entries" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"kpi_id" uuid NOT NULL,
	"user_id" uuid NOT NULL,
	"period_key" text NOT NULL,
	"target_value" double precision DEFAULT 0 NOT NULL,
	"actual_value" double precision DEFAULT 0 NOT NULL,
	"note" text,
	"updated_by" uuid,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "custom_kpis" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"name" text NOT NULL,
	"unit" text NOT NULL,
	"created_by" uuid,
	"is_active" boolean DEFAULT true NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "custom_kpi_entries" ADD CONSTRAINT "custom_kpi_entries_kpi_id_custom_kpis_id_fk" FOREIGN KEY ("kpi_id") REFERENCES "public"."custom_kpis"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "custom_kpi_entries" ADD CONSTRAINT "custom_kpi_entries_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 "custom_kpi_entries_uniq" ON "custom_kpi_entries" USING btree ("kpi_id","user_id","period_key");--> statement-breakpoint
CREATE INDEX "custom_kpi_entries_user_period_idx" ON "custom_kpi_entries" USING btree ("user_id","period_key");