// Package models defines the database entities for the Control Center Go backend.
// Structs map 1:1 to the PostgreSQL schema defined in backend/migrations/.
package models

import (
	"time"

	"github.com/jackc/pgx/v5/pgtype"
)

// AgentStatus represents the possible lifecycle states of an agent.
type AgentStatus string

const (
	AgentStatusActive  AgentStatus = "active"
	AgentStatusIdle    AgentStatus = "idle"
	AgentStatusThinking AgentStatus = "thinking"
	AgentStatusError   AgentStatus = "error"
	AgentStatusOffline AgentStatus = "offline"
)

// Agent represents a registered agent and its current state.
type Agent struct {
	ID           pgtype.UUID `db:"id" json:"id"`
	Name         string      `db:"name" json:"name"`
	Status       AgentStatus `db:"status" json:"status"`
	Task         *string     `db:"task" json:"task,omitempty"`
	Progress     int32       `db:"progress" json:"progress"`
	SessionKey   *string     `db:"session_key" json:"session_key,omitempty"`
	Channel      *string     `db:"channel" json:"channel,omitempty"`
	LastActivity time.Time   `db:"last_activity" json:"last_activity"`
	CreatedAt    time.Time   `db:"created_at" json:"created_at"`
	UpdatedAt    time.Time   `db:"updated_at" json:"updated_at"`
}

// SessionStatus represents the possible states of an agent session.
type SessionStatus string

const (
	SessionStatusRunning    SessionStatus = "running"
	SessionStatusCompleted  SessionStatus = "completed"
	SessionStatusCrashed    SessionStatus = "crashed"
	SessionStatusTerminated SessionStatus = "terminated"
)

// Session tracks an agent session over time.
type Session struct {
	ID        pgtype.UUID  `db:"id" json:"id"`
	AgentID   pgtype.UUID  `db:"agent_id" json:"agent_id"`
	StartedAt time.Time    `db:"started_at" json:"started_at"`
	EndedAt   *time.Time   `db:"ended_at" json:"ended_at,omitempty"`
	Status    SessionStatus `db:"status" json:"status"`
}

// TaskLogStatus represents the possible states of a task log entry.
type TaskLogStatus string

const (
	TaskLogStatusPending   TaskLogStatus = "pending"
	TaskLogStatusRunning   TaskLogStatus = "running"
	TaskLogStatusCompleted TaskLogStatus = "completed"
	TaskLogStatusFailed    TaskLogStatus = "failed"
	TaskLogStatusCancelled TaskLogStatus = "cancelled"
)

// TaskLog records a historical task assigned to an agent.
type TaskLog struct {
	ID            pgtype.UUID  `db:"id" json:"id"`
	AgentID       pgtype.UUID  `db:"agent_id" json:"agent_id"`
	Task          string       `db:"task" json:"task"`
	Status        TaskLogStatus `db:"status" json:"status"`
	StartedAt     time.Time    `db:"started_at" json:"started_at"`
	CompletedAt   *time.Time   `db:"completed_at" json:"completed_at,omitempty"`
	ErrorMessage  *string      `db:"error_message" json:"error_message,omitempty"`
}

// ProjectStatus represents the possible states of a project.
type ProjectStatus string

const (
	ProjectStatusPlanned     ProjectStatus = "planned"
	ProjectStatusInProgress  ProjectStatus = "in_progress"
	ProjectStatusCompleted   ProjectStatus = "completed"
	ProjectStatusPaused      ProjectStatus = "paused"
	ProjectStatusCancelled   ProjectStatus = "cancelled"
)

// Project represents a project managed by the Control Center.
type Project struct {
	ID          pgtype.UUID  `db:"id" json:"id"`
	Name        string       `db:"name" json:"name"`
	Description *string      `db:"description" json:"description,omitempty"`
	Status      ProjectStatus `db:"status" json:"status"`
	AgentID     *pgtype.UUID `db:"agent_id" json:"agent_id,omitempty"`
	CreatedAt   time.Time    `db:"created_at" json:"created_at"`
	UpdatedAt   time.Time    `db:"updated_at" json:"updated_at"`
}

// AgentEvent represents an event in the agent lifecycle or telemetry stream.
type AgentEvent struct {
	ID        pgtype.UUID `db:"id" json:"id"`
	AgentID   pgtype.UUID `db:"agent_id" json:"agent_id"`
	EventType string      `db:"event_type" json:"event_type"`
	Payload   *map[string]interface{} `db:"payload" json:"payload,omitempty"`
	CreatedAt time.Time   `db:"created_at" json:"created_at"`
}
