-- AlterTable
ALTER TABLE "monthly_user_roi" ADD COLUMN "managerId" TEXT;
ALTER TABLE "monthly_user_roi" ADD COLUMN "numberOfDays" INTEGER;

-- Update existing rows with a default value.
-- For numberOfDays, we'll use 30 as a default.
UPDATE "monthly_user_roi" SET "numberOfDays" = 30;

-- For managerId, we will use the first user's id as a default.
UPDATE monthly_user_roi AS mroi
SET "managerId" = u."managerId"
FROM users AS u
WHERE 
    mroi."userId" = u.id             
    AND mroi."managerId" IS NULL;  

-- Now that the columns have values, we can make them NOT NULL as the schema expects.
ALTER TABLE "monthly_user_roi" ALTER COLUMN "managerId" SET NOT NULL;
ALTER TABLE "monthly_user_roi" ALTER COLUMN "numberOfDays" SET NOT NULL;

-- Finally, add the foreign key constraint.
ALTER TABLE "monthly_user_roi" ADD CONSTRAINT "monthly_user_roi_managerId_fkey" FOREIGN KEY ("managerId") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
