-- Migration to merge Doctors and Nurses into HR (Employees)
-- Step 1: Add new columns to hold Employee IDs
ALTER TABLE visits ADD COLUMN IF NOT EXISTS doctor_employee_id INT NULL;
ALTER TABLE appointments ADD COLUMN IF NOT EXISTS doctor_employee_id INT NULL;
ALTER TABLE appointments ADD COLUMN IF NOT EXISTS nurse_employee_id INT NULL;

-- Step 2: Migrate data (if doctors/nurses have employee_id set)
-- Update Visits
UPDATE visits v 
JOIN doctors d ON v.doctor_id = d.id 
SET v.doctor_employee_id = d.employee_id 
WHERE d.employee_id IS NOT NULL;

-- Update Appointments (Doctor)
UPDATE appointments a 
JOIN doctors d ON a.doctor_id = d.id 
SET a.doctor_employee_id = d.employee_id 
WHERE d.employee_id IS NOT NULL;

-- Update Appointments (Nurse)
UPDATE appointments a 
JOIN nurses n ON a.nurse_id = n.id 
SET a.nurse_employee_id = n.employee_id 
WHERE n.employee_id IS NOT NULL;

-- Step 3: Drop old Foreign Keys (Constraint names might vary, so we try standard names or rely on DROP COLUMN to drop FKs in some DBs, but explicitly dropping FK is safer)
-- Finding constraint names is hard in SQL script without dynamic SQL. 
-- However, in MariaDB/MySQL, dropping the column usually drops the FK.
-- But to be safe, we will try to drop the standard named constraints if known, or just proceed with DROP COLUMN which should work if no other constraints block it.

ALTER TABLE visits DROP FOREIGN KEY IF EXISTS visits_ibfk_2; -- doctor_id
ALTER TABLE visits DROP FOREIGN KEY IF EXISTS fk_visit_nurse;
ALTER TABLE visits DROP FOREIGN KEY IF EXISTS fk_visit_doctor_employee;
ALTER TABLE appointments DROP FOREIGN KEY IF EXISTS appointments_ibfk_2; -- doctor_id
ALTER TABLE appointments DROP FOREIGN KEY IF EXISTS appointments_ibfk_3; -- nurse_id
ALTER TABLE appointments DROP FOREIGN KEY IF EXISTS fk_appointment_nurse;
ALTER TABLE appointments DROP FOREIGN KEY IF EXISTS fk_appt_doctor_employee;
ALTER TABLE appointments DROP FOREIGN KEY IF EXISTS fk_appt_nurse_employee;

-- Also drop keys/indexes if they exist separate from FK
ALTER TABLE visits DROP KEY IF EXISTS doctor_id;
ALTER TABLE appointments DROP KEY IF EXISTS doctor_id;
ALTER TABLE appointments DROP KEY IF EXISTS nurse_id;

-- Step 4: Drop old columns
ALTER TABLE visits DROP COLUMN doctor_id;
ALTER TABLE appointments DROP COLUMN doctor_id;
ALTER TABLE appointments DROP COLUMN nurse_id;

-- Step 5: Rename new columns to match standard naming (or keep them and add FK)
-- Let's rename them back to doctor_id and nurse_id but now they point to employees
ALTER TABLE visits CHANGE COLUMN doctor_employee_id doctor_id INT NULL;
ALTER TABLE appointments CHANGE COLUMN doctor_employee_id doctor_id INT NULL;
ALTER TABLE appointments CHANGE COLUMN nurse_employee_id nurse_id INT NULL;

-- Step 6: Add new Foreign Keys to employees
ALTER TABLE visits ADD CONSTRAINT fk_visit_doctor_employee FOREIGN KEY (doctor_id) REFERENCES employees(id) ON DELETE SET NULL;
ALTER TABLE appointments ADD CONSTRAINT fk_appt_doctor_employee FOREIGN KEY (doctor_id) REFERENCES employees(id) ON DELETE SET NULL;
ALTER TABLE appointments ADD CONSTRAINT fk_appt_nurse_employee FOREIGN KEY (nurse_id) REFERENCES employees(id) ON DELETE SET NULL;

-- Step 7: Drop obsolete tables
DROP TABLE IF EXISTS doctor_holidays; -- If exists
DROP TABLE IF EXISTS doctors;
DROP TABLE IF EXISTS nurses;
