-- Backfill any remaining orphan appointments (one booking per row)
INSERT INTO `Booking` (`id`, `userId`, `employeeId`, `providerId`, `startAt`, `endAt`, `status`, `createdAt`)
SELECT
    CONCAT('bk_', `id`),
    `userId`,
    `employeeId`,
    `providerId`,
    `startAt`,
    `endAt`,
    `status`,
    `createdAt`
FROM `Appointment`
WHERE `bookingId` IS NULL;

UPDATE `Appointment` SET `bookingId` = CONCAT('bk_', `id`) WHERE `bookingId` IS NULL;

-- Drop old FK and require bookingId
ALTER TABLE `Appointment` DROP FOREIGN KEY `Appointment_bookingId_fkey`;
ALTER TABLE `Appointment` MODIFY `bookingId` VARCHAR(191) NOT NULL;
ALTER TABLE `Appointment` ADD CONSTRAINT `Appointment_bookingId_fkey` FOREIGN KEY (`bookingId`) REFERENCES `Booking`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
