-- Migration 036: Node Metrics Extended
-- Розширення node_cache метриками для Node Cabinet

-- ============================================================================
-- Розширити node_cache полями метрик
-- ============================================================================

-- CPU метрики
ALTER TABLE node_cache ADD COLUMN IF NOT EXISTS cpu_model text;
ALTER TABLE node_cache ADD COLUMN IF NOT EXISTS cpu_cores integer DEFAULT 0;
ALTER TABLE node_cache ADD COLUMN IF NOT EXISTS cpu_usage numeric(5,2) DEFAULT 0;

-- GPU метрики
ALTER TABLE node_cache ADD COLUMN IF NOT EXISTS gpu_model text;
ALTER TABLE node_cache ADD COLUMN IF NOT EXISTS gpu_vram_total integer DEFAULT 0;  -- MB
ALTER TABLE node_cache ADD COLUMN IF NOT EXISTS gpu_vram_used integer DEFAULT 0;   -- MB

-- RAM метрики
ALTER TABLE node_cache ADD COLUMN IF NOT EXISTS ram_total integer DEFAULT 0;  -- MB
ALTER TABLE node_cache ADD COLUMN IF NOT EXISTS ram_used integer DEFAULT 0;   -- MB

-- Disk метрики
ALTER TABLE node_cache ADD COLUMN IF NOT EXISTS disk_total integer DEFAULT 0;  -- MB
ALTER TABLE node_cache ADD COLUMN IF NOT EXISTS disk_used integer DEFAULT 0;   -- MB

-- Agent counts
ALTER TABLE node_cache ADD COLUMN IF NOT EXISTS agent_count_router integer DEFAULT 0;
ALTER TABLE node_cache ADD COLUMN IF NOT EXISTS agent_count_system integer DEFAULT 0;

-- Heartbeat
ALTER TABLE node_cache ADD COLUMN IF NOT EXISTS last_heartbeat timestamptz;

-- DAGI Router URL (для інтеграції)
ALTER TABLE node_cache ADD COLUMN IF NOT EXISTS dagi_router_url text;

-- ============================================================================
-- Оновити існуючі ноди базовими даними
-- ============================================================================

-- NODE1: Hetzner GEX44
UPDATE node_cache SET
    cpu_model = 'AMD Ryzen 9 5950X',
    cpu_cores = 16,
    gpu_model = 'RTX 4090',
    gpu_vram_total = 24576,  -- 24GB
    ram_total = 131072,      -- 128GB
    disk_total = 3840000,    -- ~3.8TB
    dagi_router_url = 'http://localhost:9102',
    last_heartbeat = NOW()
WHERE node_id = 'node-1-hetzner-gex44';

-- NODE2: MacBook Pro M4 Max
UPDATE node_cache SET
    cpu_model = 'Apple M4 Max',
    cpu_cores = 16,
    gpu_model = 'Apple M4 Max GPU',
    gpu_vram_total = 40960,  -- 40GB (unified memory)
    ram_total = 65536,       -- 64GB
    disk_total = 1024000,    -- 1TB
    dagi_router_url = 'http://localhost:9102',
    last_heartbeat = NOW()
WHERE node_id = 'node-2-macbook-m4max';

-- ============================================================================
-- Індекси для метрик
-- ============================================================================

CREATE INDEX IF NOT EXISTS idx_node_cache_last_heartbeat 
    ON node_cache(last_heartbeat DESC);

-- ============================================================================
-- Коментарі
-- ============================================================================

COMMENT ON COLUMN node_cache.cpu_model IS 'CPU model name';
COMMENT ON COLUMN node_cache.cpu_cores IS 'Number of CPU cores';
COMMENT ON COLUMN node_cache.cpu_usage IS 'Current CPU usage percentage (0-100)';
COMMENT ON COLUMN node_cache.gpu_model IS 'GPU model name';
COMMENT ON COLUMN node_cache.gpu_vram_total IS 'Total GPU VRAM in MB';
COMMENT ON COLUMN node_cache.gpu_vram_used IS 'Used GPU VRAM in MB';
COMMENT ON COLUMN node_cache.ram_total IS 'Total RAM in MB';
COMMENT ON COLUMN node_cache.ram_used IS 'Used RAM in MB';
COMMENT ON COLUMN node_cache.disk_total IS 'Total disk space in MB';
COMMENT ON COLUMN node_cache.disk_used IS 'Used disk space in MB';
COMMENT ON COLUMN node_cache.agent_count_router IS 'Number of agents in DAGI Router config';
COMMENT ON COLUMN node_cache.agent_count_system IS 'Number of agents in database (system)';
COMMENT ON COLUMN node_cache.last_heartbeat IS 'Last heartbeat timestamp from node';
COMMENT ON COLUMN node_cache.dagi_router_url IS 'URL of DAGI Router on this node';

-- ============================================================================
-- Результат
-- ============================================================================

SELECT 'Migration 036 completed: Node metrics fields added' AS result;

