package domain

import (
	"time"
)

// AuditStatus represents the status of a command execution.
type AuditStatus string

const (
	AuditStatusRunning   AuditStatus = "running"
	AuditStatusSuccess   AuditStatus = "success"
	AuditStatusError     AuditStatus = "error"
	AuditStatusCancelled AuditStatus = "cancelled"
)

// IsValid checks if the audit status is a valid value.
func (s AuditStatus) IsValid() bool {
	switch s {
	case AuditStatusRunning, AuditStatusSuccess, AuditStatusError, AuditStatusCancelled:
		return true
	}
	return false
}

// AuditLogEntry represents a single audit log entry for command execution.
type AuditLogEntry struct {
	ID              string      `json:"id"`
	APIKeyID        string      `json:"api_key_id"`
	CommandID       string      `json:"command_id"`
	ProjectID       string      `json:"project_id"`
	CommandType     CommandType `json:"command_type"`
	Args            string      `json:"args,omitempty"` // JSON-encoded args
	ClientIP        string      `json:"client_ip,omitempty"`
	UserAgent       string      `json:"user_agent,omitempty"`
	StartedAt       time.Time   `json:"started_at"`
	CompletedAt     *time.Time  `json:"completed_at,omitempty"`
	ExitCode        *int        `json:"exit_code,omitempty"`
	DurationMs      *int64      `json:"duration_ms,omitempty"`
	Status          AuditStatus `json:"status"`
	ErrorMessage    string      `json:"error_message,omitempty"`
	OutputSizeBytes int64       `json:"output_size_bytes"`
	CreatedAt       time.Time   `json:"created_at"`
}

// AuditResult contains the result of a completed command for audit logging.
type AuditResult struct {
	ExitCode        int
	DurationMs      int64
	Status          AuditStatus
	ErrorMessage    string
	OutputSizeBytes int64
}

// AuditFilters defines the filters for querying audit logs.
type AuditFilters struct {
	// ProjectID filters by project ID.
	ProjectID string

	// APIKeyID filters by API key ID.
	APIKeyID string

	// CommandType filters by command type (claude, shell, git).
	CommandType CommandType

	// Status filters by audit status.
	Status AuditStatus

	// StartTime filters entries created at or after this time.
	StartTime *time.Time

	// EndTime filters entries created before this time.
	EndTime *time.Time

	// Limit is the maximum number of entries to return.
	Limit int

	// Offset is the number of entries to skip (for pagination).
	Offset int
}

// DefaultAuditFilters returns default filter values.
func DefaultAuditFilters() AuditFilters {
	return AuditFilters{
		Limit:  100,
		Offset: 0,
	}
}
