package provider

import (
	"context"
	"encoding/json"
	"net/http"
	"net/url"
	"time"
)

type Capability string

const (
	CapabilityChat       Capability = "chat"
	CapabilityResponses  Capability = "responses"
	CapabilityEmbeddings Capability = "embeddings"
	CapabilityMessages   Capability = "messages"
	CapabilityModels     Capability = "models"
)

type Adapter interface {
	Name() string
	Target() *url.URL
	Capabilities() []Capability
	Prepare(*http.Request)
}

type Credentials struct {
	APIKey string `json:"api_key"`
}

type ConnectionTestResult struct {
	Connected  bool   `json:"connected"`
	StatusCode int    `json:"status_code,omitempty"`
	LatencyMS  int64  `json:"latency_ms"`
	Message    string `json:"message"`
}

type Model struct {
	ID              string          `json:"id"`
	ProviderID      string          `json:"provider_id"`
	ProviderModelID string          `json:"provider_model_id"`
	OwnedBy         string          `json:"owned_by,omitempty"`
	Metadata        json.RawMessage `json:"metadata"`
	Enabled         bool            `json:"enabled"`
	DiscoveredAt    time.Time       `json:"discovered_at"`
	LastSeenAt      time.Time       `json:"last_seen_at"`
}

type DiscoveredModel struct {
	ProviderModelID string
	OwnedBy         string
	Metadata        json.RawMessage
}

type ModelSyncResult struct {
	Discovered int       `json:"discovered"`
	Active     int       `json:"active"`
	Disabled   int       `json:"disabled"`
	SyncedAt   time.Time `json:"synced_at"`
}

type CredentialRotation struct {
	ProviderID  string
	FromVersion int
	ToVersion   int
	Ciphertext  []byte
}

type CredentialRotationResult struct {
	ActiveVersion  int   `json:"active_version"`
	LoadedVersions []int `json:"loaded_versions"`
	Rotated        int   `json:"rotated"`
	Skipped        int   `json:"skipped"`
}

type AdminOperations interface {
	TestConnection(context.Context, string) (ConnectionTestResult, error)
	SyncModels(context.Context, string, string) (ModelSyncResult, error)
	ListModels(context.Context, string) ([]Model, error)
	RotateCredentials(context.Context, string) (CredentialRotationResult, error)
}
