-- 002_assets.sql: Asset management tables

CREATE TABLE IF NOT EXISTS hardware_assets (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    device_uid      TEXT NOT NULL REFERENCES devices(device_uid) ON DELETE CASCADE,
    cpu_model       TEXT,
    cpu_cores       INTEGER,
    memory_total_mb INTEGER,
    disk_model      TEXT,
    disk_total_mb   INTEGER,
    gpu_model       TEXT,
    motherboard     TEXT,
    serial_number   TEXT,
    reported_at     TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at      TEXT NOT NULL DEFAULT (datetime('now')),
    UNIQUE(device_uid)
);

CREATE TABLE IF NOT EXISTS software_assets (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    device_uid      TEXT NOT NULL REFERENCES devices(device_uid) ON DELETE CASCADE,
    name            TEXT NOT NULL,
    version         TEXT,
    publisher       TEXT,
    install_date    TEXT,
    install_path    TEXT,
    updated_at      TEXT NOT NULL DEFAULT (datetime('now')),
    UNIQUE(device_uid, name, version)
);

CREATE TABLE IF NOT EXISTS asset_changes (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    device_uid      TEXT NOT NULL REFERENCES devices(device_uid) ON DELETE CASCADE,
    change_type     TEXT NOT NULL CHECK(change_type IN ('hardware', 'software_added', 'software_removed')),
    change_detail   TEXT NOT NULL,
    detected_at     TEXT NOT NULL DEFAULT (datetime('now'))
);

-- Indexes
CREATE INDEX IF NOT EXISTS idx_software_device ON software_assets(device_uid);
CREATE INDEX IF NOT EXISTS idx_asset_changes_time ON asset_changes(detected_at);
CREATE INDEX IF NOT EXISTS idx_asset_changes_device ON asset_changes(device_uid);
