package domain

import (
	"time"
)

// Dashboard represents a Signoz dashboard
type Dashboard struct {
	ID          string    `json:"id"`
	Name        string    `json:"name"`
	Description string    `json:"description"`
	Tags        []string  `json:"tags"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
	CreatedBy   string    `json:"created_by"`
	Panels      []Panel   `json:"panels,omitempty"`
}

// Panel represents a dashboard panel
type Panel struct {
	ID          string                 `json:"id"`
	Title       string                 `json:"title"`
	Type        string                 `json:"type"`
	Description string                 `json:"description"`
	Query       string                 `json:"query"`
	Options     map[string]interface{} `json:"options"`
	Position    PanelPosition          `json:"position"`
}

// PanelPosition represents panel layout position
type PanelPosition struct {
	X      int `json:"x"`
	Y      int `json:"y"`
	Width  int `json:"width"`
	Height int `json:"height"`
}

// DashboardData represents live dashboard data
type DashboardData struct {
	DashboardID string      `json:"dashboard_id"`
	Panels      []PanelData `json:"panels"`
	TimeRange   TimeRange   `json:"time_range"`
	Timestamp   time.Time   `json:"timestamp"`
}

// PanelData represents live panel data
type PanelData struct {
	PanelID string                 `json:"panel_id"`
	Data    map[string]interface{} `json:"data"`
	Status  string                 `json:"status"`
	Error   string                 `json:"error,omitempty"`
}

// TimeRange represents a time range for queries
type TimeRange struct {
	Start time.Time `json:"start"`
	End   time.Time `json:"end"`
}

// Service represents an instrumented service
type Service struct {
	ServiceName string            `json:"serviceName"`
	Environment string            `json:"environment"`
	Tags        map[string]string `json:"tags"`
	Operations  []string          `json:"operations"`
	CallRate    float64           `json:"callRate"`
	ErrorRate   float64           `json:"errorRate"`
	P99Latency  float64           `json:"p99Latency"`
	AvgLatency  float64           `json:"avgLatency"`
	LastSeen    time.Time         `json:"lastSeen"`
}

// APMMetrics represents APM metrics for a service
type APMMetrics struct {
	ServiceName string            `json:"serviceName"`
	TimeRange   TimeRange         `json:"timeRange"`
	Metrics     map[string]Metric `json:"metrics"`
}

// Metric represents a single metric with its values over time
type Metric struct {
	Name        string      `json:"name"`
	Unit        string      `json:"unit"`
	Description string      `json:"description"`
	DataPoints  []DataPoint `json:"dataPoints"`
}

// DataPoint represents a single data point in a time series
type DataPoint struct {
	Timestamp time.Time `json:"timestamp"`
	Value     float64   `json:"value"`
}

// QueryResult represents the result of a ClickHouse or builder query
type QueryResult struct {
	Columns []ColumnInfo    `json:"columns"`
	Rows    [][]interface{} `json:"rows"`
	Meta    QueryMeta       `json:"meta"`
}

// ColumnInfo represents column metadata
type ColumnInfo struct {
	Name string `json:"name"`
	Type string `json:"type"`
}

// QueryMeta represents query metadata
type QueryMeta struct {
	QueryID       string        `json:"queryId"`
	ExecutionTime time.Duration `json:"executionTime"`
	RowsAffected  int64         `json:"rowsAffected"`
	Statistics    QueryStats    `json:"statistics"`
}

// QueryStats represents query statistics
type QueryStats struct {
	ReadRows     int64 `json:"readRows"`
	ReadBytes    int64 `json:"readBytes"`
	WrittenRows  int64 `json:"writtenRows"`
	WrittenBytes int64 `json:"writtenBytes"`
}

// TraceLog represents trace or log data
type TraceLog struct {
	Type       string                 `json:"type"` // "trace" or "log"
	ID         string                 `json:"id"`
	Timestamp  time.Time              `json:"timestamp"`
	Service    string                 `json:"service"`
	Operation  string                 `json:"operation,omitempty"`
	Duration   time.Duration          `json:"duration,omitempty"`
	Status     string                 `json:"status"`
	Message    string                 `json:"message,omitempty"`
	Attributes map[string]interface{} `json:"attributes"`
	SpanID     string                 `json:"spanId,omitempty"`
	TraceID    string                 `json:"traceId,omitempty"`
	ParentID   string                 `json:"parentId,omitempty"`
}

// ConnectionStatus represents the status of Signoz connection
type ConnectionStatus struct {
	Connected   bool          `json:"connected"`
	URL         string        `json:"url"`
	Version     string        `json:"version,omitempty"`
	LastChecked time.Time     `json:"lastChecked"`
	Error       string        `json:"error,omitempty"`
	Latency     time.Duration `json:"latency,omitempty"`
}

// BuilderQuery represents a Signoz builder query
type BuilderQuery struct {
	QueryType   string                 `json:"queryType"`
	MetricName  string                 `json:"metricName"`
	Aggregation string                 `json:"aggregation"`
	Filters     []QueryFilter          `json:"filters"`
	GroupBy     []string               `json:"groupBy"`
	TimeRange   TimeRange              `json:"timeRange"`
	Step        time.Duration          `json:"step"`
	Options     map[string]interface{} `json:"options"`
}

// QueryFilter represents a query filter
type QueryFilter struct {
	Key      string      `json:"key"`
	Operator string      `json:"operator"`
	Value    interface{} `json:"value"`
}

// ClickHouseQuery represents a raw ClickHouse query
type ClickHouseQuery struct {
	SQL       string                 `json:"sql"`
	TimeRange TimeRange              `json:"timeRange"`
	Params    map[string]interface{} `json:"params"`
}
