-- Migration 008: Force INET/CIDR types (unconditional)
-- Fixes issues where columns remained TEXT after conditional migration 007
-- Date: 2026-01-02

BEGIN;

-- ============================================================================
-- FORCE DROP AND RECREATE ALL INET COLUMNS
-- This is unconditional - always executes regardless of current state
-- ============================================================================

-- Drop indexes first (if exist)
DROP INDEX IF EXISTS public_blacklist_ip_inet_idx;
DROP INDEX IF EXISTS public_blacklist_cidr_inet_idx;
DROP INDEX IF EXISTS whitelist_ip_inet_idx;

-- ============================================================================
-- FIX public_blacklist_ips TABLE
-- ============================================================================

-- Drop columns unconditionally
ALTER TABLE public_blacklist_ips DROP COLUMN IF EXISTS ip_inet;
ALTER TABLE public_blacklist_ips DROP COLUMN IF EXISTS cidr_inet;

-- Recreate with correct INET/CIDR types
ALTER TABLE public_blacklist_ips ADD COLUMN ip_inet inet;
ALTER TABLE public_blacklist_ips ADD COLUMN cidr_inet cidr;

-- Populate from existing text data
UPDATE public_blacklist_ips 
SET 
    ip_inet = CASE 
        WHEN ip_address ~ '/' THEN ip_address::inet
        ELSE ip_address::inet
    END,
    cidr_inet = CASE 
        WHEN cidr_range IS NOT NULL AND cidr_range != '' THEN cidr_range::cidr
        WHEN ip_address ~ '/' THEN ip_address::cidr
        ELSE (ip_address || '/32')::cidr
    END
WHERE ip_inet IS NULL;

-- Create GiST indexes for fast INET/CIDR containment operators
CREATE INDEX public_blacklist_ip_inet_idx ON public_blacklist_ips USING gist(ip_inet inet_ops);
CREATE INDEX public_blacklist_cidr_inet_idx ON public_blacklist_ips USING gist(cidr_inet inet_ops);

-- ============================================================================
-- FIX whitelist TABLE
-- ============================================================================

-- Drop column unconditionally
ALTER TABLE whitelist DROP COLUMN IF EXISTS ip_inet;

-- Recreate with correct INET type
ALTER TABLE whitelist ADD COLUMN ip_inet inet;

-- Populate from existing text data
UPDATE whitelist
SET ip_inet = CASE
    WHEN ip_address ~ '/' THEN ip_address::inet
    ELSE ip_address::inet
END
WHERE ip_inet IS NULL;

-- Create index for whitelist
CREATE INDEX whitelist_ip_inet_idx ON whitelist USING gist(ip_inet inet_ops);

-- ============================================================================
-- UPDATE SCHEMA VERSION
-- ============================================================================

UPDATE schema_version SET version = 8, applied_at = NOW() WHERE id = 1;

COMMIT;

-- ============================================================================
-- VERIFICATION
-- ============================================================================

SELECT 'Migration 008 completed successfully' as status;
SELECT version, applied_at FROM schema_version WHERE id = 1;

-- Verify column types
SELECT 
    table_name, 
    column_name, 
    data_type
FROM information_schema.columns 
WHERE 
    (table_name = 'public_blacklist_ips' AND column_name IN ('ip_inet', 'cidr_inet'))
    OR (table_name = 'whitelist' AND column_name = 'ip_inet')
ORDER BY table_name, column_name;
