-- =========================================================
-- MIGRAZIONE 003: Fix network_logs columns (dest_ip -> destination_ip)
-- =========================================================
-- Assicura che le colonne di network_logs usino i nomi corretti

-- Rinomina dest_ip -> destination_ip se esiste
DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_name = 'network_logs' AND column_name = 'dest_ip'
    ) THEN
        ALTER TABLE network_logs RENAME COLUMN dest_ip TO destination_ip;
        RAISE NOTICE 'Colonna dest_ip rinominata in destination_ip';
    END IF;
END $$;

-- Rinomina dest_port -> destination_port se esiste  
DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_name = 'network_logs' AND column_name = 'dest_port'
    ) THEN
        ALTER TABLE network_logs RENAME COLUMN dest_port TO destination_port;
        RAISE NOTICE 'Colonna dest_port rinominata in destination_port';
    END IF;
END $$;

-- Rinomina src_ip -> source_ip se esiste
DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_name = 'network_logs' AND column_name = 'src_ip'
    ) THEN
        ALTER TABLE network_logs RENAME COLUMN src_ip TO source_ip;
        RAISE NOTICE 'Colonna src_ip rinominata in source_ip';
    END IF;
END $$;

-- Rinomina src_port -> source_port se esiste
DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_name = 'network_logs' AND column_name = 'src_port'
    ) THEN
        ALTER TABLE network_logs RENAME COLUMN src_port TO source_port;
        RAISE NOTICE 'Colonna src_port rinominata in source_port';
    END IF;
END $$;

SELECT 'Migrazione 003 completata!' AS status;
