// Copyright (c) 2026 Alibaba Group and its affiliates

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//     http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sdk

import "strings"

const (
	dashScopeTaskGroupAIGC            = "aigc"
	dashScopeTaskTextGeneration       = "text-generation"
	dashScopeTaskMultimodalGeneration = "multimodal-generation"
	dashScopeFunctionGeneration       = "generation"
)

// modelRoute carries the DashScope routing tuple (task_group / task / function)
// for a particular model.
type modelRoute struct {
	taskGroup string
	task      string
	function  string
}

// modelPrefixRoutes maps lowercase model-name fragments to the DashScope task.
// Matching is longest-substring-wins (see routeForModel), so the order here is
// presentational only: overlapping fragments resolve by length, not table order.
var modelPrefixRoutes = []struct {
	prefix string
	task   string
}{
	// Text generation
	{"qwen3-max", dashScopeTaskTextGeneration},
	{"qwen3.7-max", dashScopeTaskTextGeneration},
	{"glm-5.1", dashScopeTaskTextGeneration},
	{"deepseek-v3", dashScopeTaskTextGeneration},
	{"deepseek-v3.2", dashScopeTaskTextGeneration},
	{"deepseek-v4-pro", dashScopeTaskTextGeneration},
	{"deepseek-v4-flash", dashScopeTaskTextGeneration},

	// Multimodal generation
	{"qwen3.5", dashScopeTaskMultimodalGeneration},
	{"qwen3.6", dashScopeTaskMultimodalGeneration},
	{"kimi-k2.6", dashScopeTaskMultimodalGeneration},
	{"minimax-m2.5", dashScopeTaskMultimodalGeneration},
}

// routeForModel returns the DashScope routing tuple for the given model name.
// Matching is case-insensitive and longest-substring-wins; unknown models fall
// back to text-generation so new model names don't fail closed at the gateway.
func routeForModel(model string) modelRoute {
	lower := strings.ToLower(model)
	bestTask := dashScopeTaskTextGeneration
	bestLen := -1
	for _, r := range modelPrefixRoutes {
		if len(r.prefix) > bestLen && strings.Contains(lower, r.prefix) {
			bestTask = r.task
			bestLen = len(r.prefix)
		}
	}
	return modelRoute{
		taskGroup: dashScopeTaskGroupAIGC,
		task:      bestTask,
		function:  dashScopeFunctionGeneration,
	}
}
