INSERT INTO integration_platforms (
  provider, platform_key, name, base_url, auth_type, credentials, config,
  default_pricing_mode, default_discount_factor, retry_policy, rate_limit_policy, priority, status
)
VALUES
  (
    'suno', 'suno-simulation', 'Suno Music Simulation',
    'https://api.cqtai.com/api/cqt', 'bearer',
    '{"mode":"simulation"}'::jsonb,
    '{"testMode":true,"seed":"audio-music-openapi","sourceSpecType":"suno"}'::jsonb,
    'inherit_discount', 1,
    '{"enabled":true,"maxAttempts":2,"retryOn":["rate_limit","timeout","server_error","network"]}'::jsonb,
    '{"rules":[{"metric":"rpm","limit":60,"windowSeconds":60},{"metric":"concurrent","limit":5,"leaseTtlSeconds":120}]}'::jsonb,
    930,
    'enabled'
  ),
  (
    'minimax', 'minimax-speech-simulation', 'MiniMax Speech Simulation',
    'https://api.minimaxi.com/v1', 'bearer',
    '{"mode":"simulation"}'::jsonb,
    '{"testMode":true,"seed":"audio-music-openapi","sourceSpecType":"minimax"}'::jsonb,
    'inherit_discount', 1,
    '{"enabled":true,"maxAttempts":2,"retryOn":["rate_limit","timeout","server_error","network"]}'::jsonb,
    '{"rules":[{"metric":"rpm","limit":60,"windowSeconds":60},{"metric":"concurrent","limit":5,"leaseTtlSeconds":120}]}'::jsonb,
    940,
    'enabled'
  )
ON CONFLICT (platform_key) DO UPDATE
SET name = EXCLUDED.name,
    base_url = EXCLUDED.base_url,
    auth_type = EXCLUDED.auth_type,
    credentials = EXCLUDED.credentials,
    config = EXCLUDED.config,
    default_pricing_mode = EXCLUDED.default_pricing_mode,
    default_discount_factor = EXCLUDED.default_discount_factor,
    retry_policy = EXCLUDED.retry_policy,
    rate_limit_policy = EXCLUDED.rate_limit_policy,
    priority = EXCLUDED.priority,
    status = EXCLUDED.status,
    updated_at = now();

INSERT INTO platform_models (
  platform_id, base_model_id, model_name, provider_model_name, model_alias, model_type, display_name,
  capabilities, pricing_mode, billing_config, retry_policy, rate_limit_policy, enabled
)
SELECT p.id,
       b.id,
       b.provider_model_name,
       b.provider_model_name,
       b.display_name,
       b.model_type,
       b.display_name,
       b.capabilities,
       'inherit_discount',
       b.base_billing_config,
       '{"enabled":true,"maxAttempts":2}'::jsonb,
       b.default_rate_limit_policy,
       true
FROM integration_platforms p
JOIN base_model_catalog b ON b.provider_key = p.provider
WHERE p.platform_key IN ('suno-simulation', 'minimax-speech-simulation')
  AND b.status = 'active'
  AND b.model_type ?| ARRAY['audio_generate','text_to_speech']
ON CONFLICT (platform_id, model_name) DO UPDATE
SET base_model_id = EXCLUDED.base_model_id,
    provider_model_name = EXCLUDED.provider_model_name,
    model_alias = EXCLUDED.model_alias,
    display_name = EXCLUDED.display_name,
    model_type = EXCLUDED.model_type,
    capabilities = EXCLUDED.capabilities,
    pricing_mode = EXCLUDED.pricing_mode,
    billing_config = EXCLUDED.billing_config,
    retry_policy = EXCLUDED.retry_policy,
    rate_limit_policy = EXCLUDED.rate_limit_policy,
    enabled = EXCLUDED.enabled,
    updated_at = now();
