-- ESET sync support: hardware contacts + incidents cache

CREATE TABLE IF NOT EXISTS hardware_contacts (
    id SERIAL PRIMARY KEY,
    hardware_id INT NOT NULL REFERENCES hardware_assets(id) ON DELETE CASCADE,
    contact_id INT NOT NULL REFERENCES contacts(id) ON DELETE CASCADE,
    role VARCHAR(50) DEFAULT 'primary',
    source VARCHAR(50) DEFAULT 'eset',
    created_at TIMESTAMP DEFAULT NOW(),
    UNIQUE(hardware_id, contact_id)
);

CREATE INDEX IF NOT EXISTS idx_hardware_contacts_hardware ON hardware_contacts(hardware_id);
CREATE INDEX IF NOT EXISTS idx_hardware_contacts_contact ON hardware_contacts(contact_id);

CREATE TABLE IF NOT EXISTS eset_incidents (
    id SERIAL PRIMARY KEY,
    incident_uuid VARCHAR(100) UNIQUE,
    severity VARCHAR(50),
    status VARCHAR(50),
    device_uuid VARCHAR(100),
    detected_at TIMESTAMP,
    last_seen TIMESTAMP,
    payload JSONB,
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_eset_incidents_severity ON eset_incidents(severity);
CREATE INDEX IF NOT EXISTS idx_eset_incidents_device ON eset_incidents(device_uuid);
