-- Migration: Update reporting_manager_id to use employee_id instead of user_id
-- Date: 2025-10-29
-- Description: Change reporting_manager_id from UUID to VARCHAR(50) to reference employee_id

-- First, drop the existing foreign key constraint
ALTER TABLE users DROP CONSTRAINT IF EXISTS users_reporting_manager_id_fkey;

-- Add the location column if it doesn't exist
ALTER TABLE users ADD COLUMN IF NOT EXISTS location JSONB;

-- Change the column type from UUID to VARCHAR(50)
ALTER TABLE users ALTER COLUMN reporting_manager_id TYPE VARCHAR(50);

-- Add the new foreign key constraint referencing employee_id
ALTER TABLE users ADD CONSTRAINT users_reporting_manager_id_fkey 
    FOREIGN KEY (reporting_manager_id) REFERENCES users(employee_id);

-- Update the index
DROP INDEX IF EXISTS idx_users_reporting_manager;
CREATE INDEX idx_users_reporting_manager ON users(reporting_manager_id);

-- Ensure employee_id has unique constraint (it should already exist)
-- This is just to make sure the constraint is in place
ALTER TABLE users ADD CONSTRAINT IF NOT EXISTS users_employee_id_unique UNIQUE (employee_id);
