package crud

import (
	"git.nspix.com/golang/rest/orm/schema"
	"git.nspix.com/golang/rest/scenario"
	"strings"
)

func (crud *CRUD) generateParams(e *Entity, scenario string) string {
	var sb strings.Builder
	schemas := schema.VisibleField(e.Module, e.stmt.Table, scenario)
	sb.WriteString("| 参数名称 | 参数类型 | 参数说明 |")
	sb.WriteByte('\n')
	sb.WriteString("| -------- | ------- | -------- |")
	sb.WriteByte('\n')
	for _, sc := range schemas {
		sb.WriteByte('|')
		sb.WriteString(sc.Column)
		sb.WriteByte('|')
		sb.WriteString(sc.Type)
		sb.WriteByte('|')
		sb.WriteString(sc.Label)
		sb.WriteByte('|')
		sb.WriteByte('\n')
	}
	return sb.String()
}

func (crud *CRUD) generateArgs(schemas []*schema.Schema) string {
	var sb strings.Builder
	sb.WriteString("| 参数名称 | 参数类型 | 参数说明 |")
	sb.WriteByte('\n')
	sb.WriteString("| -------- | ------- | -------- |")
	sb.WriteByte('\n')
	for _, sc := range schemas {
		sb.WriteByte('|')
		sb.WriteString(sc.Column)
		sb.WriteByte('|')
		sb.WriteString(sc.Type)
		sb.WriteByte('|')
		sb.WriteString(sc.Label)
		sb.WriteByte('|')
		sb.WriteByte('\n')
	}
	return sb.String()
}

func (crud *CRUD) generateDoc(e *Entity) []byte {
	var sb strings.Builder
	sb.WriteString("")
	sb.WriteString("# " + e.naming.label + "接口")
	sb.WriteByte('\n')
	sb.WriteByte('\n')

	if e.hasScenarios(ScenarioList) {
		sb.WriteString("## 获取" + e.naming.label + "列表")
		sb.WriteByte('\n')
		sb.WriteString("> 接口用于搜索" + e.naming.label + "列表数据")
		sb.WriteByte('\n')
		sb.WriteByte('\n')
		sb.WriteString("**请求方法**  ")
		sb.WriteString("GET")
		sb.WriteByte('\n')
		sb.WriteByte('\n')
		sb.WriteString("**请求地址** ")
		sb.WriteString(e.Urls[ScenarioList])
		sb.WriteByte('\n')
		sb.WriteString("### 请求参数")
		sb.WriteByte('\n')
		sb.WriteString(crud.generateParams(e, scenario.Search))
		sb.WriteByte('\n')
		sb.WriteString("### 返回参数")
		sb.WriteByte('\n')
		sb.WriteString(crud.generateArgs([]*schema.Schema{
			{Column: "errno", Type: "integer", Label: "错误代码,为0表示成功，否则是失败"},
			{Column: "errmsg", Type: "string", Label: "错误消息"},
			{Column: "result", Type: "array", Label: "返回" + e.naming.label + "对象数据"},
		}))
		sb.WriteByte('\n')
		sb.WriteString("---------\n")
		sb.WriteByte('\n')
		sb.WriteByte('\n')
		sb.WriteByte('\n')
	}

	if e.hasScenarios(ScenarioCreate) {
		sb.WriteString("## 创建" + e.naming.label)
		sb.WriteByte('\n')
		sb.WriteString("> 接口用于创建" + e.naming.label)
		sb.WriteByte('\n')
		sb.WriteByte('\n')
		sb.WriteString("**请求方法**  ")
		sb.WriteString("POST")
		sb.WriteByte('\n')
		sb.WriteByte('\n')
		sb.WriteString("**请求地址** ")
		sb.WriteString(e.Urls[ScenarioCreate])
		sb.WriteByte('\n')
		sb.WriteString("### 请求参数")
		sb.WriteByte('\n')
		sb.WriteString(crud.generateParams(e, scenario.Create))
		sb.WriteByte('\n')
		sb.WriteString("### 返回数据")
		sb.WriteByte('\n')
		sb.WriteString(crud.generateArgs([]*schema.Schema{
			{Column: "errno", Type: "integer", Label: "错误代码,为0表示成功，否则是失败"},
			{Column: "errmsg", Type: "string", Label: "错误消息"},
			{Column: "result", Type: "object", Label: "成功返回创建的ID和状态信息"},
		}))
		sb.WriteByte('\n')
		sb.WriteString("---------\n")
		sb.WriteByte('\n')
		sb.WriteByte('\n')
		sb.WriteByte('\n')
	}

	if e.hasScenarios(ScenarioUpdate) {
		sb.WriteString("## 更新" + e.naming.label)
		sb.WriteByte('\n')
		sb.WriteString("> 接口用于更新" + e.naming.label)
		sb.WriteByte('\n')
		sb.WriteByte('\n')
		sb.WriteString("**请求方法**  ")
		sb.WriteString("PUT")
		sb.WriteByte('\n')
		sb.WriteByte('\n')
		sb.WriteString("**请求地址** ")
		sb.WriteString(e.Urls[ScenarioUpdate])
		sb.WriteByte('\n')
		sb.WriteString("### 请求参数")
		sb.WriteByte('\n')
		sb.WriteString(crud.generateParams(e, scenario.Update))
		sb.WriteByte('\n')
		sb.WriteString("### 返回数据")
		sb.WriteByte('\n')
		sb.WriteString(crud.generateArgs([]*schema.Schema{
			{Column: "errno", Type: "integer", Label: "错误代码,为0表示成功，否则是失败"},
			{Column: "errmsg", Type: "string", Label: "错误消息"},
			{Column: "result", Type: "object", Label: "成功返回更新的ID和状态信息"},
		}))
		sb.WriteByte('\n')
		sb.WriteString("---------\n")
		sb.WriteByte('\n')
		sb.WriteByte('\n')
		sb.WriteByte('\n')
	}

	if e.hasScenarios(ScenarioDelete) {
		sb.WriteString("## 删除" + e.naming.label)
		sb.WriteByte('\n')
		sb.WriteString("> 接口用于删除" + e.naming.label)
		sb.WriteByte('\n')
		sb.WriteByte('\n')
		sb.WriteString("**请求方法**  ")
		sb.WriteString("DELETE")
		sb.WriteByte('\n')
		sb.WriteByte('\n')
		sb.WriteString("**请求地址** ")
		sb.WriteString(e.Urls[ScenarioDelete])
		sb.WriteByte('\n')
		sb.WriteString("### 返回数据")
		sb.WriteByte('\n')
		sb.WriteString(crud.generateArgs([]*schema.Schema{
			{Column: "errno", Type: "integer", Label: "错误代码,为0表示成功，否则是失败"},
			{Column: "errmsg", Type: "string", Label: "错误消息"},
			{Column: "result", Type: "object", Label: "成功返回删除的ID和状态信息"},
		}))
		sb.WriteByte('\n')
		sb.WriteString("---------\n")
	}

	if e.hasScenarios(ScenarioView) {
		sb.WriteString("## 获取" + e.naming.label + "详情")
		sb.WriteByte('\n')
		sb.WriteString("> 接口用于获取" + e.naming.label + "详情")
		sb.WriteByte('\n')
		sb.WriteByte('\n')
		sb.WriteString("**请求方法**  ")
		sb.WriteString("GET")
		sb.WriteByte('\n')
		sb.WriteByte('\n')
		sb.WriteString("**请求地址** ")
		sb.WriteString(e.Urls[ScenarioDelete])
		sb.WriteByte('\n')
		sb.WriteString("**请求参数** ")
		sb.WriteByte('\n')
		sb.WriteByte('\n')
		sb.WriteString(crud.generateArgs([]*schema.Schema{
			{Column: "format", Type: "string", Label: "是否格式化输出，如果值为true，会对数据进行格式化输出，否则输出原始数据"},
		}))
		sb.WriteByte('\n')
		sb.WriteString("### 返回数据")
		sb.WriteByte('\n')
		sb.WriteString(crud.generateParams(e, scenario.View))
		sb.WriteByte('\n')
		sb.WriteString("---------\n")
	}
	return []byte(sb.String())
}
