-- 007_core_identity_map.sql — таблица соответствий ID между системами

CREATE TABLE core.identity_map (
    id bigserial PRIMARY KEY,
    entity_type text NOT NULL CHECK (entity_type IN ('employee','project','client')),
    core_id bigint,
    bitra_id text,
    eva_id text,
    bitrix_id bigint,
    confidence text NOT NULL CHECK (confidence IN ('auto','confirmed','manual')),
    match_key text,
    confirmed_by text,
    confirmed_at timestamptz,
    created_at timestamptz DEFAULT now(),
    UNIQUE (entity_type, bitra_id, eva_id, bitrix_id)
);

CREATE INDEX idx_identity_map_core ON core.identity_map (entity_type, core_id);
CREATE INDEX idx_identity_map_confidence ON core.identity_map (confidence) WHERE confidence = 'manual';
