CREATE TABLE "user_employee_maps" (
	"id" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "user_employee_maps_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),
	"organization_id" text NOT NULL,
	"user_id" text NOT NULL,
	"employee_id" integer NOT NULL,
	"provider" text,
	"provider_user_id" text,
	"source" text DEFAULT 'manual' NOT NULL,
	"is_primary" boolean DEFAULT true NOT NULL,
	"linked_at" timestamp with time zone DEFAULT now() NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "training_records" DROP CONSTRAINT "training_records_user_id_users_id_fk";
--> statement-breakpoint
ALTER TABLE "employee_training_targets" ALTER COLUMN "user_id" DROP NOT NULL;--> statement-breakpoint
ALTER TABLE "training_records" ALTER COLUMN "user_id" DROP NOT NULL;--> statement-breakpoint
ALTER TABLE "training_records" ADD COLUMN "submitted_by_user_id" text;--> statement-breakpoint
ALTER TABLE "training_records" ADD COLUMN "reviewed_by_user_id" text;--> statement-breakpoint
ALTER TABLE "user_employee_maps" ADD CONSTRAINT "user_employee_maps_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "user_employee_maps" ADD CONSTRAINT "user_employee_maps_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_employee_maps" ADD CONSTRAINT "user_employee_maps_employee_id_employees_id_fk" FOREIGN KEY ("employee_id") REFERENCES "public"."employees"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "user_employee_maps_org_user_employee_idx" ON "user_employee_maps" USING btree ("organization_id","user_id","employee_id");--> statement-breakpoint
CREATE UNIQUE INDEX "employee_training_targets_org_employee_year_idx" ON "employee_training_targets" USING btree ("organization_id","employee_id","year");--> statement-breakpoint
ALTER TABLE "training_records" ADD CONSTRAINT "training_records_submitted_by_user_id_users_id_fk" FOREIGN KEY ("submitted_by_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "training_records" ADD CONSTRAINT "training_records_reviewed_by_user_id_users_id_fk" FOREIGN KEY ("reviewed_by_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "training_records" ADD CONSTRAINT "training_records_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint

INSERT INTO "user_employee_maps" (
	"organization_id",
	"user_id",
	"employee_id",
	"provider",
	"provider_user_id",
	"source",
	"is_primary"
)
SELECT
	m."organization_id",
	u."id",
	u."employee_id",
	u."provider",
	u."provider_user_id",
	'legacy_backfill',
	true
FROM "users" u
INNER JOIN "memberships" m ON m."user_id" = u."id"
WHERE u."employee_id" IS NOT NULL
ON CONFLICT ("organization_id","user_id","employee_id") DO NOTHING;--> statement-breakpoint

UPDATE "training_records"
SET "submitted_by_user_id" = COALESCE("submitted_by_user_id", "created_by")
WHERE "submitted_by_user_id" IS NULL;--> statement-breakpoint

UPDATE "training_records"
SET "reviewed_by_user_id" = COALESCE("reviewed_by_user_id", "reviewed_by")
WHERE "reviewed_by_user_id" IS NULL AND "reviewed_by" IS NOT NULL;
