-- Final cleanup for Doctors/Nurses migration

-- Fix Patient Queue Doctor ID (References doctors)
ALTER TABLE patient_queue ADD COLUMN IF NOT EXISTS doctor_employee_id INT NULL;

-- Migrate data
UPDATE patient_queue q 
JOIN doctors d ON q.doctor_id = d.id 
SET q.doctor_employee_id = d.employee_id 
WHERE d.employee_id IS NOT NULL;

-- Drop old FK
ALTER TABLE patient_queue DROP FOREIGN KEY IF EXISTS patient_queue_ibfk_3;

-- Drop old column
ALTER TABLE patient_queue DROP COLUMN doctor_id;

-- Rename new column
ALTER TABLE patient_queue CHANGE COLUMN doctor_employee_id doctor_id INT NULL;

-- Add new FK to employees
ALTER TABLE patient_queue ADD CONSTRAINT fk_queue_doctor_employee FOREIGN KEY (doctor_id) REFERENCES employees(id) ON DELETE SET NULL;

-- Now drop doctors table
DROP TABLE IF EXISTS doctors;
