ALTER TABLE designations
    ADD COLUMN IF NOT EXISTS code VARCHAR(64),
    ADD COLUMN IF NOT EXISTS department_id UUID REFERENCES departments(id) ON DELETE SET NULL,
    ADD COLUMN IF NOT EXISTS description TEXT,
    ADD COLUMN IF NOT EXISTS level VARCHAR(100),
    ADD COLUMN IF NOT EXISTS can_manage_team BOOLEAN NOT NULL DEFAULT false,
    ADD COLUMN IF NOT EXISTS can_approve BOOLEAN NOT NULL DEFAULT false,
    ADD COLUMN IF NOT EXISTS is_active BOOLEAN NOT NULL DEFAULT true,
    ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW();

UPDATE designations
SET updated_at = COALESCE(updated_at, created_at, NOW());

CREATE UNIQUE INDEX IF NOT EXISTS idx_designations_code_unique
    ON designations (LOWER(code))
    WHERE code IS NOT NULL;

CREATE INDEX IF NOT EXISTS idx_designations_is_active
    ON designations (is_active);

CREATE INDEX IF NOT EXISTS idx_designations_department_id
    ON designations (department_id);
