-- ============================================================================
-- PROVISIONING RAG SYSTEM - SURREALDB SCHEMA
-- ============================================================================
-- This schema defines the RAG database structure for semantic document search,
-- deployment analytics, and graph-based relationships.
--
-- Namespace: control_center
-- Database: rag
-- ============================================================================

-- ============================================================================
-- DOCUMENTS TABLE - Indexed documents for semantic search
-- ============================================================================
DEFINE TABLE documents SCHEMAFULL
    COMMENT = "Vector-embedded documents for RAG retrieval"
    PERMISSIONS
        FOR select ALLOW (published = true OR $auth.role = 'admin')
        FOR create ALLOW $auth.role = 'admin'
        FOR update ALLOW $auth.role = 'admin'
        FOR delete ALLOW $auth.role = 'admin';

-- Document fields
DEFINE FIELD id ON documents TYPE string
    COMMENT = "Unique document chunk ID"
    ASSERT string::len($value) > 0;

DEFINE FIELD source_path ON documents TYPE string
    COMMENT = "Path to source file (docs/user/api/etc.)"
    ASSERT string::len($value) > 0;

DEFINE FIELD doc_type ON documents TYPE string
    COMMENT = "Document type: markdown, kcl, nushell, rust"
    ASSERT $value in ["markdown", "kcl", "nushell", "rust"];

DEFINE FIELD category ON documents TYPE string
    COMMENT = "Document category: user_guide, architecture, api, operations, troubleshooting";

DEFINE FIELD heading_path ON documents TYPE array<string>
    COMMENT = "Heading hierarchy (H1 > H2 > H3) for context preservation"
    DEFAULT [];

DEFINE FIELD content ON documents TYPE string
    COMMENT = "Document text content (pre-chunking)"
    ASSERT string::len($value) > 0;

DEFINE FIELD embedding ON documents TYPE array<float>
    COMMENT = "Vector embedding (1536 dimensions for text-embedding-3-small)"
    ASSERT array::len($value) = 1536;

DEFINE FIELD metadata ON documents TYPE object
    COMMENT = "Additional metadata (schema name, function signature, etc.)"
    DEFAULT {};

DEFINE FIELD git_commit ON documents TYPE string
    COMMENT = "Git commit hash when document was indexed";

DEFINE FIELD indexed_at ON documents TYPE datetime
    COMMENT = "Timestamp when document was indexed"
    DEFAULT time::now();

DEFINE FIELD updated_at ON documents TYPE datetime
    COMMENT = "Timestamp when document was last updated"
    DEFAULT time::now();

DEFINE FIELD published ON documents TYPE bool
    COMMENT = "Document is published and searchable"
    DEFAULT true;

-- Indexes
DEFINE INDEX idx_source_path ON documents FIELDS source_path;
DEFINE INDEX idx_doc_type ON documents FIELDS doc_type;
DEFINE INDEX idx_category ON documents FIELDS category;
DEFINE INDEX idx_embedding ON documents FIELDS embedding HNSW
    DIMENSION 1536
    DISTANCE cosine
    EF_CONSTRUCTION 200
    EF_SEARCH 100
    COMMENT = "HNSW index for fast vector similarity search";

-- ============================================================================
-- DEPLOYMENTS TABLE - Deployment events for analytics and learning
-- ============================================================================
DEFINE TABLE deployments SCHEMAFULL
    COMMENT = "Deployment events for analytics and RAG learning"
    PERMISSIONS
        FOR select ALLOW (workspace = $auth.workspace OR $auth.role = 'admin')
        FOR create ALLOW $auth.role IN ['user', 'admin']
        FOR update ALLOW $auth.role = 'admin'
        FOR delete ALLOW $auth.role = 'admin';

-- Deployment event fields
DEFINE FIELD id ON deployments TYPE string
    COMMENT = "Unique deployment event ID (UUID)"
    ASSERT string::len($value) > 0;

DEFINE FIELD workspace ON deployments TYPE string
    COMMENT = "Workspace name"
    ASSERT string::len($value) > 0;

DEFINE FIELD infrastructure ON deployments TYPE string
    COMMENT = "Infrastructure name (infra ID)"
    ASSERT string::len($value) > 0;

DEFINE FIELD event_type ON deployments TYPE string
    COMMENT = "Event type: server_create, taskserv_install, cluster_deploy, error, success"
    ASSERT $value in ["server_create", "taskserv_install", "cluster_deploy", "error", "success"];

DEFINE FIELD resource_type ON deployments TYPE string
    COMMENT = "Resource type: Server, Taskserv, Cluster"
    ASSERT $value in ["server", "taskserv", "cluster", "unknown"];

DEFINE FIELD resource_name ON deployments TYPE string
    COMMENT = "Resource name (e.g., 'web-01', 'kubernetes')"
    ASSERT string::len($value) > 0;

DEFINE FIELD provider ON deployments TYPE string
    COMMENT = "Provider: upcloud, aws, local, etc."
    ASSERT string::len($value) > 0;

DEFINE FIELD status ON deployments TYPE string
    COMMENT = "Event status: success, failed, partial, in_progress"
    ASSERT $value in ["success", "failed", "partial", "in_progress"];

DEFINE FIELD duration_ms ON deployments TYPE int
    COMMENT = "Duration of operation in milliseconds"
    ASSERT $value >= 0;

DEFINE FIELD config_snapshot ON deployments TYPE object
    COMMENT = "Configuration snapshot used for this deployment"
    DEFAULT {};

DEFINE FIELD config_text ON deployments TYPE string
    COMMENT = "JSON-serialized config for semantic search"
    DEFAULT "";

DEFINE FIELD embedding ON deployments TYPE array<float>
    COMMENT = "Vector embedding of config_text for semantic search"
    ASSERT array::len($value) = 1536;

DEFINE FIELD resources ON deployments TYPE object {
    cpu_cores: float,
    memory_mb: int,
    disk_gb: int,
    cost_usd: float,
}
    COMMENT = "Resources consumed in this deployment"
    DEFAULT {};

DEFINE FIELD error ON deployments TYPE object {
    type: string,
    message: string,
    stack_trace: option<string>,
}
    COMMENT = "Error information if status = failed"
    DEFAULT {};

DEFINE FIELD git_commit ON deployments TYPE string
    COMMENT = "Git commit hash at time of deployment";

DEFINE FIELD cli_version ON deployments TYPE string
    COMMENT = "CLI version used for deployment";

DEFINE FIELD user_id ON deployments TYPE string
    COMMENT = "User who initiated the deployment";

DEFINE FIELD timestamp ON deployments TYPE datetime
    COMMENT = "Event timestamp"
    DEFAULT time::now();

-- Indexes
DEFINE INDEX idx_workspace ON deployments FIELDS workspace;
DEFINE INDEX idx_infrastructure ON deployments FIELDS infrastructure;
DEFINE INDEX idx_status ON deployments FIELDS status;
DEFINE INDEX idx_provider ON deployments FIELDS provider;
DEFINE INDEX idx_timestamp ON deployments FIELDS timestamp;
DEFINE INDEX idx_deployment_embedding ON deployments FIELDS embedding HNSW
    DIMENSION 1536
    DISTANCE cosine
    EF_CONSTRUCTION 200
    EF_SEARCH 100
    COMMENT = "HNSW index for semantic deployment search";

-- ============================================================================
-- GRAPH RELATIONSHIPS - Connect documents and deployments
-- ============================================================================

-- Documents related to other documents (e.g., "See also" links)
DEFINE TABLE doc_relates_to TYPE RELATION
    FROM documents
    TO documents
    COMMENT = "Related documents (concept links, cross-references)";

DEFINE FIELD relevance_score ON doc_relates_to TYPE float DEFAULT 0.0;
DEFINE FIELD reason ON doc_relates_to TYPE string;

-- Deployments depending on other deployments
DEFINE TABLE deployment_depends_on TYPE RELATION
    FROM deployments
    TO deployments
    COMMENT = "Deployment dependencies (e.g., cluster depends on servers)";

DEFINE FIELD dependency_type ON deployment_depends_on TYPE string
    ASSERT $value in ["sequential", "parallel", "conditional"];

-- Documents related to deployments (e.g., "This doc explains that deployment")
DEFINE TABLE doc_explains_deployment TYPE RELATION
    FROM documents
    TO deployments
    COMMENT = "Documentation explaining a deployment pattern";

DEFINE FIELD confidence ON doc_explains_deployment TYPE float DEFAULT 0.0;

-- ============================================================================
-- METADATA TABLES - System metadata
-- ============================================================================

-- Indexing status and metadata
DEFINE TABLE rag_index_metadata SCHEMAFULL
    COMMENT = "RAG indexing metadata and statistics";

DEFINE FIELD id ON rag_index_metadata TYPE string;
DEFINE FIELD last_reindex ON rag_index_metadata TYPE datetime DEFAULT time::now();
DEFINE FIELD total_documents ON rag_index_metadata TYPE int DEFAULT 0;
DEFINE FIELD total_deployments ON rag_index_metadata TYPE int DEFAULT 0;
DEFINE FIELD doc_type_counts ON rag_index_metadata TYPE object DEFAULT {};
DEFINE FIELD last_commit ON rag_index_metadata TYPE string;
DEFINE FIELD reindex_duration_ms ON rag_index_metadata TYPE int DEFAULT 0;
DEFINE FIELD reindex_status ON rag_index_metadata TYPE string DEFAULT "idle";
    -- idle, indexing, completed, failed

-- Embedding model metadata
DEFINE TABLE embedding_models SCHEMAFULL
    COMMENT = "Embedding model configuration and metadata";

DEFINE FIELD model_name ON embedding_models TYPE string PRIMARY;
DEFINE FIELD provider ON embedding_models TYPE string;
DEFINE FIELD dimension ON embedding_models TYPE int;
DEFINE FIELD created_at ON embedding_models TYPE datetime DEFAULT time::now();
DEFINE FIELD token_cost_per_1k ON embedding_models TYPE float;
DEFINE FIELD description ON embedding_models TYPE string;

-- ============================================================================
-- HELPER FUNCTIONS
-- ============================================================================

-- Function to calculate embedding similarity
DEFINE FUNCTION fn::vector_similarity(
    v1: array<float>,
    v2: array<float>
) -> float {
    -- Cosine similarity = dot_product / (norm1 * norm2)
    -- SurrealDB handles this natively via HNSW
    RETURN vector::similarity::cosine(v1, v2);
};

-- Function to chunk documents by size
DEFINE FUNCTION fn::chunk_document(
    content: string,
    chunk_size: int,
    overlap: int
) -> array<object> {
    -- Placeholder - actual chunking done at application level
    RETURN [];
};

-- ============================================================================
-- GRANTS - Access control
-- ============================================================================

-- Grant permissions for admin role
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE documents TO ROLE admin;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE deployments TO ROLE admin;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE rag_index_metadata TO ROLE admin;

-- Grant read-only access for users
GRANT SELECT ON TABLE documents TO ROLE user;
GRANT SELECT ON TABLE deployments TO ROLE user;
GRANT SELECT ON TABLE rag_index_metadata TO ROLE user;

-- ============================================================================
-- INITIALIZATION
-- ============================================================================

-- Create initial embedding model metadata
INSERT INTO embedding_models (model_name, provider, dimension, token_cost_per_1k, description)
VALUES (
    "text-embedding-3-small",
    "openai",
    1536,
    0.00002,
    "OpenAI text-embedding-3-small model for document embeddings"
);

INSERT INTO embedding_models (model_name, provider, dimension, token_cost_per_1k, description)
VALUES (
    "text-embedding-3-large",
    "openai",
    3072,
    0.00013,
    "OpenAI text-embedding-3-large model for higher-quality embeddings"
);

-- Initialize index metadata
INSERT INTO rag_index_metadata (id, total_documents, total_deployments)
VALUES ("default", 0, 0);
