-- 013_rename_talents_to_identities.sql
-- Rename 'talents' to 'identities' to reflect broader scope (news, family, social, etc.)

-- 1. Rename 'talents' table to 'identities'
ALTER TABLE public.talents RENAME TO identities;
ALTER TABLE dev.talents RENAME TO identities;

-- 2. Rename 'talent_id' column in 'identity_bindings' to 'identity_id'
-- Note: We use dynamic SQL to avoid errors if the column is already renamed or doesn't exist
DO $$ 
BEGIN
    -- Public schema
    IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'identity_bindings' AND column_name = 'talent_id') THEN
        ALTER TABLE public.identity_bindings RENAME COLUMN talent_id TO identity_id;
    END IF;

    -- Dev schema
    IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'dev' AND table_name = 'identity_bindings' AND column_name = 'talent_id') THEN
        ALTER TABLE dev.identity_bindings RENAME COLUMN talent_id TO identity_id;
    END IF;
END $$;

-- 3. Add index for the new column name
CREATE INDEX IF NOT EXISTS idx_identity_bindings_identity_id ON identity_bindings(identity_id);
