ALTER TABLE "users" ADD COLUMN "employee_code" text;--> statement-breakpoint
ALTER TABLE "users" ADD COLUMN "department_id" integer;--> statement-breakpoint
ALTER TABLE "users" ADD COLUMN "position_id" integer;--> statement-breakpoint
ALTER TABLE "users" ADD COLUMN "company_name" text;--> statement-breakpoint
ALTER TABLE "users" ADD COLUMN "hired_at" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "training_records" ADD COLUMN "user_id" text;--> statement-breakpoint

UPDATE "users" AS "u"
SET
  "employee_code" = "e"."employee_code",
  "department_id" = "e"."department_id",
  "position_id" = "e"."position_id",
  "company_name" = COALESCE("e"."company", "o"."name"),
  "hired_at" = "e"."hired_at",
  "updated_at" = NOW()
FROM "employees" AS "e"
INNER JOIN "organizations" AS "o" ON "o"."id" = "e"."organization_id"
WHERE "u"."email" IS NOT NULL
  AND "e"."email" IS NOT NULL
  AND LOWER("u"."email") = LOWER("e"."email");--> statement-breakpoint

INSERT INTO "users" (
  "id",
  "name",
  "email",
  "password_hash",
  "system_role",
  "is_active",
  "employee_code",
  "department_id",
  "position_id",
  "company_name",
  "hired_at",
  "active_organization_id"
)
SELECT
  CONCAT('legacy-employee-', "e"."id", '-', "e"."organization_id") AS "id",
  "e"."full_name",
  COALESCE(LOWER("e"."email"), CONCAT('employee-', "e"."id", '@training-system.local')),
  '$2b$10$sl8.hKi0l/KXnouzGgiRvOpNgZ9TT8RFvdPjQbgxCV5WirdWv1uBq',
  'standard',
  false,
  "e"."employee_code",
  "e"."department_id",
  "e"."position_id",
  COALESCE("e"."company", "o"."name"),
  "e"."hired_at",
  "e"."organization_id"
FROM "employees" AS "e"
INNER JOIN "organizations" AS "o" ON "o"."id" = "e"."organization_id"
LEFT JOIN "users" AS "u"
  ON (
    ("e"."email" IS NOT NULL AND LOWER("u"."email") = LOWER("e"."email"))
    OR ("u"."employee_code" IS NOT NULL AND "u"."employee_code" = "e"."employee_code")
  )
WHERE "u"."id" IS NULL;--> statement-breakpoint

INSERT INTO "memberships" (
  "id",
  "user_id",
  "organization_id",
  "role",
  "permissions"
)
SELECT
  CONCAT('legacy-membership-', "e"."id", '-', "e"."organization_id"),
  CONCAT('legacy-employee-', "e"."id", '-', "e"."organization_id"),
  "e"."organization_id",
  'member',
  ARRAY['products:read']::text[]
FROM "employees" AS "e"
LEFT JOIN "memberships" AS "m"
  ON "m"."user_id" = CONCAT('legacy-employee-', "e"."id", '-', "e"."organization_id")
  AND "m"."organization_id" = "e"."organization_id"
WHERE "m"."id" IS NULL
  AND EXISTS (
    SELECT 1
    FROM "users" AS "u"
    WHERE "u"."id" = CONCAT('legacy-employee-', "e"."id", '-', "e"."organization_id")
  );--> statement-breakpoint

UPDATE "training_records" AS "tr"
SET "user_id" = COALESCE(
  (
    SELECT "u"."id"
    FROM "users" AS "u"
    INNER JOIN "employees" AS "e" ON LOWER("u"."email") = LOWER("e"."email")
    WHERE "e"."id" = "tr"."employee_id"
    LIMIT 1
  ),
  CONCAT(
    'legacy-employee-',
    "tr"."employee_id",
    '-',
    "tr"."organization_id"
  )
)
WHERE "tr"."user_id" IS NULL;--> statement-breakpoint

ALTER TABLE "training_records" ALTER COLUMN "user_id" SET NOT NULL;--> 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 cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "training_records" DROP CONSTRAINT "training_records_employee_id_employees_id_fk";--> statement-breakpoint
ALTER TABLE "training_records" ADD CONSTRAINT "training_records_employee_id_employees_id_fk" FOREIGN KEY ("employee_id") REFERENCES "public"."employees"("id") ON DELETE set null ON UPDATE no action;
