package entitys

import (
	"ai_scheduler/internal/data/constants"
	"ai_scheduler/internal/data/model"
	"context"
	"encoding/json"

	"gitea.cdlsxd.cn/self-tools/l_request"
	"github.com/gofiber/websocket/v2"
	"github.com/ollama/ollama/api"
)

// ChatRequest 聊天请求
type ChatRequest struct {
	UserInput       string          `json:"user_input" binding:"required"`
	Caller          string          `json:"caller" binding:"required"`
	SessionID       string          `json:"session_id"`
	ChatRequestMeta ChatRequestMeta `json:"meta,omitempty"`
}
type ChatRequestMeta struct {
	Authorization string `json:"authorization"`
}

type FirstSockRequest struct {
	Authorization string `json:"authorization"`
	SessionID     string `json:"session_id"`
	AppKey        string `json:"app_key"`
}

type ChatSockRequest struct {
	Text      string `json:"text" binding:"required"`
	Img       string `json:"img" binding:"required"` // 多图片使用 英文, 分割
	File      string `json:"file" binding:"required"`
	Tags      string `json:"tags" binding:"required"`
	MarkHis   int64  `json:"mark_his" `
	Caller    string `json:"caller" binding:"required"`
	SessionID string `json:"session_id"`
}

// ChatResponse 聊天响应
type ChatResponse struct {
	Status   string `json:"status"`
	Message  string `json:"message"`
	Data     any    `json:"data,omitempty"`
	TaskCode string `json:"task_code,omitempty"`
}

// ToolCall 工具调用
type ToolCall struct {
	ID       string          `json:"id"`
	Type     string          `json:"type"`
	Function FunctionCall    `json:"function"`
	Result   json.RawMessage `json:"result,omitempty"`
}

// FunctionCall 函数调用
type FunctionCall struct {
	Name      string          `json:"name"`
	Arguments json.RawMessage `json:"arguments"`
}

// ToolDefinition 工具定义
type ToolDefinition struct {
	Type     string      `json:"type"`
	Function FunctionDef `json:"function"`
	AuthFunc AuthFunc    `json:"function"`
}
type AuthFunc func(rec *Recognize) (token []byte, err error)

// FunctionDef 函数定义
type FunctionDef struct {
	Name        string                 `json:"name"`
	Description string                 `json:"description"`
	Parameters  map[string]interface{} `json:"parameters"`
}

// Tool 工具接口
type Tool interface {
	Name() string
	Description() string
	Definition(ctx context.Context) ToolDefinition
	Execute(ctx context.Context, requireData *Recognize) error
}

type ConfigDataHttp struct {
	Param   map[string]interface{} `json:"param"`
	Request map[string]interface{} `json:"request"`
}

type ConfigDataTool struct {
	Param   map[string]interface{} `json:"param"`
	Request map[string]interface{} `json:"request"`
	Tool    string                 `json:"tool"`
}

type ConfigDataReport struct {
	Time string `json:"time"`
}

// Message 消息
type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type FuncApi struct {
	Param   interface{}       `json:"param"`
	Request l_request.Request `json:"request"`
}

type TaskConfig struct {
	Param interface{} `json:"param"`
}

type TaskConfigDetail struct {
	Param ConfigParam `json:"param"`
}
type ConfigParam struct {
	Properties map[string]api.ToolProperty
	Required   []string `json:"required"`
	Type       string   `json:"type"`
}
type Match struct {
	Confidence any    `json:"confidence"`
	Index      string `json:"index"`
	IsMatch    bool   `json:"is_match"`
	Parameters string `json:"parameters"`
	Reasoning  string `json:"reasoning"`
	History    []byte `json:"history"`
	UserInput  string `json:"user_input"`
	Auth       string `json:"auth"`
	Chat       string `json:"chat"`
}
type ChatHis struct {
	SessionId string       `json:"session_id"`
	Messages  []HisMessage `json:"messages"`
	Context   HisContext   `json:"context"`
}
type HisMessage struct {
	Role      constants.Caller `json:"role"`
	Content   string           `json:"content"`
	Timestamp string           `json:"timestamp"`
}

type HisContext struct {
	UserLanguage string `json:"user_language"` // 用户语言
	SystemMode   string `json:"system_mode"`   // 系统模式，
}

type RequireData struct {
	Session       string
	Key           string
	Sys           model.AiSy
	Histories     []model.AiChatHi
	SessionInfo   model.AiSession
	Tasks         []model.AiTask
	Task          model.AiTask
	Match         Match
	Req           *ChatSockRequest
	Auth          string
	Ch            chan Response
	KnowledgeConf KnowledgeBaseRequest
	ImgByte       []api.ImageData
	ImgUrls       []string
}

type KnowledgeBaseRequest struct {
	Session string // 知识库会话id
	ApiKey  string // 知识库apiKey
	Query   string // 用户输入
}

// RouterService 路由服务接口
type RouterService interface {
	RouteWithSocket(c *websocket.Conn, req *ChatSockRequest) error
}
