// Copyright 2025 CloudWeGo Authors
//
// 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 tpl

// ClientTplV2 is the template for generating client.go. Enabled when streamx is specified.
var ClientTplV2 string = `// Code generated by Kitex {{.Version}}. DO NOT EDIT.

package {{ToLower .ServiceName}}
import (
	{{- range $path, $aliases := .Imports}}
		{{- if not $aliases}}
			"{{$path}}"
		{{- else}}
			{{- range $alias, $is := $aliases}}
				{{$alias}} "{{$path}}"
			{{- end}}
		{{- end}}
	{{- end}}
)
// Client is designed to provide IDL-compatible methods with call-option parameter for kitex framework.
type Client interface {
{{- range .AllMethods}}
{{- if (or .ClientStreaming .ServerStreaming)}}
	{{.Name}}(ctx context.Context {{if not .ClientStreaming}}{{range .Args}}, {{.RawName}} {{.Type}}{{end}}{{end}}, callOptions ...streamcall.Option ) (stream {{.ServiceName}}_{{.RawName}}Client, err error)
{{- else}}
	{{.Name}}(ctx context.Context {{range .Args}}, {{.RawName}} {{.Type}}{{end}}, callOptions ...callopt.Option ) ({{if not .Void}}r {{.Resp.Type}}, {{end}}err error)
{{- end}}
{{- end}}
}

{{range .AllMethods}}
{{- if or .ClientStreaming .ServerStreaming}}
{{$arg := index .Args 0}}
{{- if and .ClientStreaming .ServerStreaming}}
type {{.ServiceName}}_{{.RawName}}Client streaming.BidiStreamingClient[{{NotPtr $arg.Type}},{{NotPtr .Resp.Type}}]
{{- else if .ClientStreaming}}
type {{.ServiceName}}_{{.RawName}}Client streaming.ClientStreamingClient[{{NotPtr $arg.Type}},{{NotPtr .Resp.Type}}]
{{- else}}
type {{.ServiceName}}_{{.RawName}}Client streaming.ServerStreamingClient[{{NotPtr .Resp.Type}}]
{{- end}}
{{- end}}
{{end}}

// NewClient creates a client for the service defined in IDL.
func NewClient(destService string, opts ...client.Option) (Client, error) {
	var options []client.Option
	options = append(options, client.WithDestService(destService))

    {{template "@client.go-NewClient-option" .}}
	{{if .HasStreaming}}
	{{- if eq $.Codec "thrift"}}{{/* thrift streaming enable ttheader streaming protocol by default */}}
	options = append(options, client.WithTransportProtocol(transport.TTHeaderStreaming))
	{{- else}}
	options = append(options, client.WithTransportProtocol(transport.GRPC))
	{{- end}}
	{{end}}
	options = append(options, opts...)

    kc, err := client.NewClient(serviceInfo(), options...)
	if err != nil {
		return nil, err
	}
	{{- if .FrugalPretouch}}
	pretouch()
	{{- end}}{{/* if .FrugalPretouch */}}
	return &k{{.ServiceName}}Client{
		kClient: newServiceClient(kc),
	}, nil
}

// MustNewClient creates a client for the service defined in IDL. It panics if any error occurs.
func MustNewClient(destService string, opts ...client.Option) Client {
	kc, err := NewClient(destService, opts...)
	if err != nil {
		panic(err)
	}
	return kc
}

type k{{.ServiceName}}Client struct {
	*kClient
}

{{range .AllMethods}}
{{- if or .ClientStreaming .ServerStreaming}}
func (p *k{{$.ServiceName}}Client) {{.Name}}(ctx context.Context {{if not .ClientStreaming}}{{range .Args}}, {{.RawName}} {{.Type}}{{end}}{{end}}, callOptions ...streamcall.Option ) (stream {{.ServiceName}}_{{.RawName}}Client, err error) {
	ctx = client.NewCtxWithCallOptions(ctx, streamcall.GetCallOptions(callOptions))
	return p.kClient.{{.Name}}(ctx{{if not .ClientStreaming}}{{range .Args}}, {{.RawName}}{{end}}{{end}})
}
{{- else}}
func (p *k{{$.ServiceName}}Client) {{.Name}}(ctx context.Context {{range .Args}}, {{.RawName}} {{.Type}}{{end}}, callOptions ...callopt.Option ) ({{if not .Void}}r {{.Resp.Type}}, {{end}}err error) {
	ctx = client.NewCtxWithCallOptions(ctx, callOptions)
	return p.kClient.{{.Name}}(ctx{{range .Args}}, {{.RawName}}{{end}})
}
{{- end}}
{{end}}

{{template "@client.go-EOF" .}}
`
