package api

import (
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

// Response represents a standard API response
type Response struct {
	Success bool        `json:"success"`
	Data    interface{} `json:"data,omitempty"`
	Error   string      `json:"error,omitempty"`
	Code    string      `json:"code,omitempty"`
}

// SuccessResponse creates a success response
func SuccessResponse(c *gin.Context, data interface{}) {
	c.JSON(http.StatusOK, Response{
		Success: true,
		Data:    data,
	})
}

// ErrorResponse creates an error response
func ErrorResponse(c *gin.Context, status int, err string, code string) {
	c.JSON(status, Response{
		Success: false,
		Error:   err,
		Code:    code,
	})
}

// StateData represents state response data
type StateData struct {
	TenantID     string      `json:"tenantId"`
	State        interface{} `json:"state"`
	Version      int         `json:"version"`
	LastModified string      `json:"lastModified"`
}

// ValidationError represents a validation error
type ValidationError struct {
	RuleID   string `json:"ruleId"`
	Field    string `json:"field"`
	Message  string `json:"message"`
	Severity string `json:"severity"`
}

// CheckpointResult represents checkpoint validation result
type CheckpointResult struct {
	CheckpointID string            `json:"checkpointId"`
	Passed       bool              `json:"passed"`
	ValidatedAt  string            `json:"validatedAt"`
	ValidatedBy  string            `json:"validatedBy"`
	Errors       []ValidationError `json:"errors"`
	Warnings     []ValidationError `json:"warnings"`
}

// SearchResult represents a RAG search result
type SearchResult struct {
	ID         string            `json:"id"`
	Content    string            `json:"content"`
	Source     string            `json:"source"`
	Score      float64           `json:"score"`
	Metadata   map[string]string `json:"metadata,omitempty"`
	Highlights []string          `json:"highlights,omitempty"`
}

// GenerateRequest represents a document generation request
type GenerateRequest struct {
	TenantID    string                 `json:"tenantId" binding:"required"`
	Context     map[string]interface{} `json:"context"`
	Template    string                 `json:"template,omitempty"`
	Language    string                 `json:"language,omitempty"`
	UseRAG      bool                   `json:"useRag"`
	RAGQuery    string                 `json:"ragQuery,omitempty"`
	MaxTokens   int                    `json:"maxTokens,omitempty"`
	Temperature float64                `json:"temperature,omitempty"`
}

// GenerateResponse represents a document generation response
type GenerateResponse struct {
	Content      string         `json:"content"`
	GeneratedAt  string         `json:"generatedAt"`
	Model        string         `json:"model"`
	TokensUsed   int            `json:"tokensUsed"`
	RAGSources   []SearchResult `json:"ragSources,omitempty"`
	Confidence   float64        `json:"confidence,omitempty"`
}

// Timestamps helper
func now() string {
	return time.Now().UTC().Format(time.RFC3339)
}
