-- TIP: Transceiver Intelligence Platform
-- Migration 006: Whitebox / Open Networking Switch Extensions
--
-- Adds columns for whitebox ODM/OEM switches, open networking OS support,
-- OCP compliance, and hardware details needed for disaggregated networking.

-- ============================================================
-- EXTEND switches TABLE with whitebox-specific columns
-- ============================================================

-- Whitebox classification
ALTER TABLE switches ADD COLUMN IF NOT EXISTS is_whitebox BOOLEAN DEFAULT FALSE;
ALTER TABLE switches ADD COLUMN IF NOT EXISTS is_ocp_accepted BOOLEAN DEFAULT FALSE;
ALTER TABLE switches ADD COLUMN IF NOT EXISTS ocp_status TEXT CHECK (ocp_status IN ('Accepted', 'Inspired', 'None', NULL));

-- Open Networking OS support
ALTER TABLE switches ADD COLUMN IF NOT EXISTS supported_nos TEXT[] DEFAULT '{}';
ALTER TABLE switches ADD COLUMN IF NOT EXISTS onl_compatible BOOLEAN DEFAULT FALSE;
ALTER TABLE switches ADD COLUMN IF NOT EXISTS dent_compatible BOOLEAN DEFAULT FALSE;
ALTER TABLE switches ADD COLUMN IF NOT EXISTS cumulus_compatible BOOLEAN DEFAULT FALSE;
ALTER TABLE switches ADD COLUMN IF NOT EXISTS fboss_compatible BOOLEAN DEFAULT FALSE;
ALTER TABLE switches ADD COLUMN IF NOT EXISTS stratum_compatible BOOLEAN DEFAULT FALSE;

-- Hardware details (important for whitebox — CPU/RAM/storage determine NOS capability)
ALTER TABLE switches ADD COLUMN IF NOT EXISTS cpu TEXT;
ALTER TABLE switches ADD COLUMN IF NOT EXISTS cpu_cores INTEGER;
ALTER TABLE switches ADD COLUMN IF NOT EXISTS ram_gb NUMERIC;
ALTER TABLE switches ADD COLUMN IF NOT EXISTS storage_gb NUMERIC;
ALTER TABLE switches ADD COLUMN IF NOT EXISTS storage_type TEXT;

-- ASIC generation tracking (extends existing asic_vendor/asic_model)
ALTER TABLE switches ADD COLUMN IF NOT EXISTS asic_series TEXT;
ALTER TABLE switches ADD COLUMN IF NOT EXISTS asic_process_nm INTEGER;

-- Physical / form factor
ALTER TABLE switches ADD COLUMN IF NOT EXISTS front_panel_ports TEXT;
ALTER TABLE switches ADD COLUMN IF NOT EXISTS mgmt_ports TEXT;
ALTER TABLE switches ADD COLUMN IF NOT EXISTS console_ports TEXT;

-- Transceiver form factors supported (derived from ports_config, but explicit for search)
ALTER TABLE switches ADD COLUMN IF NOT EXISTS transceiver_form_factors TEXT[] DEFAULT '{}';

-- External references
ALTER TABLE switches ADD COLUMN IF NOT EXISTS catalog_url TEXT;
ALTER TABLE switches ADD COLUMN IF NOT EXISTS sonic_hwsku TEXT;
ALTER TABLE switches ADD COLUMN IF NOT EXISTS onie_support BOOLEAN DEFAULT FALSE;

-- Scraping metadata
ALTER TABLE switches ADD COLUMN IF NOT EXISTS last_scraped TIMESTAMPTZ;
ALTER TABLE switches ADD COLUMN IF NOT EXISTS scrape_source TEXT;

-- ============================================================
-- INDEXES for whitebox queries
-- ============================================================
CREATE INDEX IF NOT EXISTS idx_switches_is_whitebox ON switches (is_whitebox) WHERE is_whitebox = TRUE;
CREATE INDEX IF NOT EXISTS idx_switches_sonic ON switches (sonic_compatible) WHERE sonic_compatible = TRUE;
CREATE INDEX IF NOT EXISTS idx_switches_ocp ON switches (is_ocp_accepted) WHERE is_ocp_accepted = TRUE;
CREATE INDEX IF NOT EXISTS idx_switches_asic_series ON switches (asic_series);
CREATE INDEX IF NOT EXISTS idx_switches_supported_nos ON switches USING GIN (supported_nos);
CREATE INDEX IF NOT EXISTS idx_switches_transceiver_ff ON switches USING GIN (transceiver_form_factors);

-- ============================================================
-- UPDATE search vector trigger to include whitebox fields
-- ============================================================
CREATE OR REPLACE FUNCTION switches_search_vector_update() RETURNS trigger AS $$
BEGIN
    NEW.search_vector :=
        setweight(to_tsvector('english', COALESCE(NEW.model, '')), 'A') ||
        setweight(to_tsvector('english', COALESCE(NEW.series, '')), 'A') ||
        setweight(to_tsvector('english', COALESCE(NEW.category, '')), 'B') ||
        setweight(to_tsvector('english', COALESCE(NEW.asic_vendor, '')), 'B') ||
        setweight(to_tsvector('english', COALESCE(NEW.asic_model, '')), 'C') ||
        setweight(to_tsvector('english', COALESCE(NEW.asic_series, '')), 'C') ||
        setweight(to_tsvector('english', COALESCE(NEW.sonic_hwsku, '')), 'C') ||
        setweight(to_tsvector('english', COALESCE(NEW.cpu, '')), 'D') ||
        setweight(to_tsvector('english', COALESCE(array_to_string(NEW.supported_nos, ' '), '')), 'C') ||
        setweight(to_tsvector('english', COALESCE(array_to_string(NEW.tags, ' '), '')), 'D');
    NEW.updated_at := NOW();
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- ============================================================
-- EXTEND vendors TABLE for whitebox ODMs
-- ============================================================
ALTER TABLE vendors ADD COLUMN IF NOT EXISTS is_odm BOOLEAN DEFAULT FALSE;
ALTER TABLE vendors ADD COLUMN IF NOT EXISTS is_whitebox_vendor BOOLEAN DEFAULT FALSE;
ALTER TABLE vendors ADD COLUMN IF NOT EXISTS ocp_member BOOLEAN DEFAULT FALSE;
ALTER TABLE vendors ADD COLUMN IF NOT EXISTS sonic_contributor BOOLEAN DEFAULT FALSE;
