-- MARGARET Log Table
-- Deploy on CORP PostgreSQL

CREATE TABLE IF NOT EXISTS margaret_log (
    id BIGSERIAL PRIMARY KEY,
    h_instancia VARCHAR(64) NOT NULL,
    h_entrada VARCHAR(64) NOT NULL,
    contenedor JSONB NOT NULL,
    r2_paths JSONB DEFAULT '{}',
    estado VARCHAR(20) DEFAULT 'recibido',
    procesado_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT NOW(),
    CONSTRAINT margaret_log_h_entrada_unique UNIQUE (h_entrada)
);

-- Grant permissions
GRANT ALL PRIVILEGES ON TABLE margaret_log TO corp;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO corp;

-- Index for performance
CREATE INDEX IF NOT EXISTS idx_margaret_log_h_instancia ON margaret_log(h_instancia);
CREATE INDEX IF NOT EXISTS idx_margaret_log_estado ON margaret_log(estado);
CREATE INDEX IF NOT EXISTS idx_margaret_log_created_at ON margaret_log(created_at);
