WITH minimax_speech_models AS (
  SELECT *
  FROM (
    VALUES
      (
        'minimax:speech-2.8-hd',
        'speech-2.8-hd',
        'MiniMax-Speech-2.8-HD',
        '{"originalTypes":["text_to_speech"]}'::jsonb,
        '{"source":"server-main.integration-platform","sourceProviderCode":"minimax","sourceProviderName":"MiniMax","sourceSpecType":"minimax","originalTypes":["text_to_speech"],"alias":"MiniMax-Speech-2.8-HD","description":"","iconPath":"https://static.51easyai.com/minimax-color.png","billingType":"external-api","billingMode":"","referenceModel":"","modelWeight":null,"selectable":true,"rawModel":{"name":"speech-2.8-hd","types":["text_to_speech"],"alias":"MiniMax-Speech-2.8-HD","icon_path":"https://static.51easyai.com/minimax-color.png"}}'::jsonb
      ),
      (
        'minimax:speech-2.8-turbo',
        'speech-2.8-turbo',
        'MiniMax-Speech-2.8-Turbo',
        '{"originalTypes":["text_to_speech"]}'::jsonb,
        '{"source":"server-main.integration-platform","sourceProviderCode":"minimax","sourceProviderName":"MiniMax","sourceSpecType":"minimax","originalTypes":["text_to_speech"],"alias":"MiniMax-Speech-2.8-Turbo","description":"","iconPath":"https://static.51easyai.com/minimax-color.png","billingType":"external-api","billingMode":"","referenceModel":"","modelWeight":null,"selectable":true,"rawModel":{"name":"speech-2.8-turbo","types":["text_to_speech"],"alias":"MiniMax-Speech-2.8-Turbo","icon_path":"https://static.51easyai.com/minimax-color.png"}}'::jsonb
      )
  ) AS item(canonical_model_key, provider_model_name, display_name, capabilities, metadata)
)
INSERT INTO base_model_catalog (
  provider_id, provider_key, canonical_model_key, provider_model_name, model_type, display_name,
  capabilities, base_billing_config, default_rate_limit_policy, metadata, catalog_type, default_snapshot, status
)
SELECT (SELECT id FROM model_catalog_providers WHERE provider_key = 'minimax' OR provider_code = 'minimax' LIMIT 1),
       'minimax',
       item.canonical_model_key,
       item.provider_model_name,
       '["text_to_speech"]'::jsonb,
       item.display_name,
       item.capabilities,
       '{"text":{"basePrice":0.01,"baseWeight":1},"audio":{"basePrice":1,"baseWeight":1},"currency":"resource"}'::jsonb,
       '{"rules":[{"metric":"rpm","limit":60,"windowSeconds":60},{"metric":"concurrent","limit":5,"leaseTtlSeconds":120}]}'::jsonb,
       item.metadata,
       'system',
       jsonb_build_object(
         'providerKey', 'minimax',
         'canonicalModelKey', item.canonical_model_key,
         'providerModelName', item.provider_model_name,
         'modelType', jsonb_build_array('text_to_speech'),
         'modelAlias', item.display_name,
         'displayName', item.display_name,
         'capabilities', item.capabilities,
         'baseBillingConfig', '{"text":{"basePrice":0.01,"baseWeight":1},"audio":{"basePrice":1,"baseWeight":1},"currency":"resource"}'::jsonb,
         'defaultRateLimitPolicy', '{"rules":[{"metric":"rpm","limit":60,"windowSeconds":60},{"metric":"concurrent","limit":5,"leaseTtlSeconds":120}]}'::jsonb,
         'metadata', item.metadata,
         'status', 'active'
       ),
       'active'
FROM minimax_speech_models item
ON CONFLICT (canonical_model_key) DO UPDATE
SET provider_id = EXCLUDED.provider_id,
    provider_key = EXCLUDED.provider_key,
    provider_model_name = CASE WHEN base_model_catalog.customized_at IS NULL THEN EXCLUDED.provider_model_name ELSE base_model_catalog.provider_model_name END,
    model_type = CASE WHEN base_model_catalog.customized_at IS NULL THEN EXCLUDED.model_type ELSE base_model_catalog.model_type END,
    display_name = CASE WHEN base_model_catalog.customized_at IS NULL THEN EXCLUDED.display_name ELSE base_model_catalog.display_name END,
    capabilities = CASE WHEN base_model_catalog.customized_at IS NULL THEN EXCLUDED.capabilities ELSE base_model_catalog.capabilities END,
    base_billing_config = CASE WHEN base_model_catalog.customized_at IS NULL THEN EXCLUDED.base_billing_config ELSE base_model_catalog.base_billing_config END,
    default_rate_limit_policy = CASE WHEN base_model_catalog.customized_at IS NULL THEN EXCLUDED.default_rate_limit_policy ELSE base_model_catalog.default_rate_limit_policy END,
    metadata = CASE WHEN base_model_catalog.customized_at IS NULL THEN EXCLUDED.metadata ELSE base_model_catalog.metadata END,
    catalog_type = CASE WHEN base_model_catalog.customized_at IS NULL THEN 'system' ELSE base_model_catalog.catalog_type END,
    default_snapshot = CASE WHEN base_model_catalog.customized_at IS NULL THEN EXCLUDED.default_snapshot ELSE base_model_catalog.default_snapshot END,
    status = CASE WHEN base_model_catalog.customized_at IS NULL THEN 'active' ELSE base_model_catalog.status END,
    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":1}'::jsonb,
       b.default_rate_limit_policy,
       true
FROM integration_platforms p
JOIN base_model_catalog b ON b.canonical_model_key IN ('minimax:speech-2.8-hd', 'minimax:speech-2.8-turbo')
WHERE p.provider = 'minimax'
  AND p.deleted_at IS NULL
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();
