ALTER TABLE model_catalog_providers
  ADD COLUMN IF NOT EXISTS provider_code text,
  ADD COLUMN IF NOT EXISTS icon_path text,
  ADD COLUMN IF NOT EXISTS source text NOT NULL DEFAULT 'gateway';

UPDATE model_catalog_providers
SET provider_code = provider_key
WHERE provider_code IS NULL OR provider_code = '';

UPDATE model_catalog_providers
SET provider_code = 'openai',
    icon_path = NULL,
    source = 'server-main.integration-platform',
    metadata = metadata || '{"syncSource":"server-main.integration-platform","sourceCode":"openai"}'::jsonb,
    updated_at = now()
WHERE provider_key = 'openai';

UPDATE model_catalog_providers
SET provider_code = 'google-gemini',
    icon_path = 'https://static.51easyai.com/gemini-color.png',
    source = 'server-main.integration-platform',
    metadata = metadata || '{"syncSource":"server-main.integration-platform","sourceCode":"google-gemini"}'::jsonb,
    updated_at = now()
WHERE provider_key = 'gemini';

ALTER TABLE model_catalog_providers
  ALTER COLUMN provider_code SET NOT NULL;

CREATE UNIQUE INDEX IF NOT EXISTS idx_model_catalog_provider_code
  ON model_catalog_providers(provider_code);
