package protocol

import (
	"fmt"
	"strings"
	"xboard-go/internal/model"
)

func GenerateClash(servers []model.Server, user model.User) (string, error) {
	var builder strings.Builder

	builder.WriteString("proxies:\n")
	var proxyNames []string
	for _, s := range servers {
		// Basic VMess conversion for Clash
		proxy := fmt.Sprintf("  - name: \"%s\"\n    type: vmess\n    server: %s\n    port: %s\n    uuid: %s\n    alterId: 0\n    cipher: auto\n",
			s.Name, s.Host, s.Port, user.UUID)
		builder.WriteString(proxy)
		proxyNames = append(proxyNames, fmt.Sprintf("\"%s\"", s.Name))
	}

	builder.WriteString("\nproxy-groups:\n")
	builder.WriteString("  - name: Proxy\n    type: select\n    proxies:\n      - DIRECT\n")
	for _, name := range proxyNames {
		builder.WriteString("      - " + name + "\n")
	}

	builder.WriteString("\nrules:\n")
	builder.WriteString("  - MATCH,Proxy\n")

	return builder.String(), nil
}
