package main

import (
	"encoding/json"
	"io"
	"os"
)

// goctl 通过 stdin 传入的插件输入结构
type PluginInput struct {
	Api         *ApiSpec `json:"Api"`
	ApiFilePath string   `json:"ApiFilePath"`
	Style       string   `json:"Style"`
	Dir         string   `json:"Dir"`
}

type ApiSpec struct {
	Types   []TypeDef    `json:"Types"`
	Service ServiceSpec  `json:"Service"`
}

type TypeDef struct {
	RawName string   `json:"RawName"`
	Members []Member `json:"Members"`
}

type Member struct {
	Name string     `json:"Name"`
	Tag  string     `json:"Tag"`
	Type MemberType `json:"Type"`
}

type MemberType struct {
	RawName string `json:"RawName"`
}

type ServiceSpec struct {
	Name   string  `json:"Name"`
	Groups []Group `json:"Groups"`
}

type Group struct {
	Routes []Route `json:"Routes"`
}

type Route struct {
	Method       string   `json:"Method"`
	Path         string   `json:"Path"`
	Handler      string   `json:"Handler"`
	RequestType  *TypeDef `json:"RequestType"`
	ResponseType *TypeDef `json:"ResponseType"`
	AtDoc        AtDoc    `json:"AtDoc"`
}

type AtDoc struct {
	Properties map[string]string `json:"Properties"`
	Text       string            `json:"Text"`
}

func readInput() (*PluginInput, error) {
	data, err := io.ReadAll(os.Stdin)
	if err != nil {
		return nil, err
	}
	var input PluginInput
	if err := json.Unmarshal(data, &input); err != nil {
		return nil, err
	}
	return &input, nil
}
