-- 11_sync.sql
-- Tablas de sincronizacion y notificaciones
-- Version: 1.0
-- Fecha: 2025-12-30

-- HST_MIRROR: Espejo de entidades HST para sincronizacion local
CREATE TABLE IF NOT EXISTS hst_mirror (
    id SERIAL PRIMARY KEY,
    ref VARCHAR(100) NOT NULL UNIQUE,
    h_maestro VARCHAR(64) NOT NULL UNIQUE,
    nombre_es VARCHAR(255),
    nombre_en VARCHAR(255),
    grupo VARCHAR(50),
    hst_subdominio VARCHAR(255),
    hst_ruta VARCHAR(255),
    local_subdominio VARCHAR(255) DEFAULT NULL,
    local_ruta VARCHAR(255) DEFAULT NULL,
    activo BOOLEAN DEFAULT TRUE,
    hst_activo BOOLEAN DEFAULT TRUE,
    sincronizado_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_hst_mirror_h_maestro ON hst_mirror(h_maestro);
CREATE INDEX IF NOT EXISTS idx_hst_mirror_ref ON hst_mirror(ref);

-- NOTIFICATION: Sistema de notificaciones
CREATE TABLE IF NOT EXISTS notification (
    id VARCHAR(20) PRIMARY KEY,
    type VARCHAR(40),
    body TEXT,
    is_read BOOLEAN DEFAULT FALSE,
    is_deleted BOOLEAN DEFAULT FALSE,
    fk_user_id VARCHAR(20),
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS notification_created_at_index ON notification(created_at);
CREATE INDEX IF NOT EXISTS notification_fk_user_id_index ON notification(fk_user_id);
