# Provisioning Config Schema - Main configuration schema
# Version: 1.0.0
#
# This is the top-level schema that defines the complete provisioning configuration
# structure. It imports all other schemas and combines them into a unified config.

let Service = import "service.ncl" in
let Database = import "database.ncl" in
let Network = import "network.ncl" in
let Storage = import "storage.ncl" in
let Monitoring = import "monitoring.ncl" in
let Security = import "security.ncl" in
let Deployment = import "deployment.ncl" in

{
  # Project metadata
  Project = {
    name
      | String
      | doc "Project name (lowercase alphanumeric with hyphens)"
      | {
        label = "valid project name",
        predicate = fun s =>
          std.string.is_match "^[a-z0-9-]+$" s &&
          std.string.length s >= 1 && std.string.length s <= 64
      },

    version
      | String
      | doc "Project version (semantic versioning: X.Y.Z)"
      | {
        label = "valid version",
        predicate = fun s =>
          std.string.is_match "^[0-9]+\\.[0-9]+\\.[0-9]+$" s
      }
      | default = "0.1.0",

    description
      | String
      | doc "Project description"
      | {
        label = "valid description",
        predicate = fun s => std.string.length s <= 500
      }
      | optional,

    author
      | String
      | doc "Project author"
      | optional,

    license
      | String
      | doc "Project license (e.g., 'MIT', 'Apache-2.0')"
      | optional,

    repository
      | String
      | doc "Repository URL"
      | optional,

    tags
      | Array String
      | doc "Project tags for categorization"
      | default = [],
  },

  # Main provisioning configuration
  ProvisioningConfig = {
    project
      | Project
      | doc "Project metadata and information",

    services
      | {_: Service.Service}
      | doc "Service definitions (key: service name, value: service config)"
      | {
        label = "valid services",
        predicate = fun services =>
          std.record.length services > 0
      },

    database
      | Database.DatabaseConfig
      | doc "Database configuration"
      | default = {type = 'none},

    network
      | Network.NetworkConfig
      | doc "Network configuration"
      | default = {},

    storage
      | Storage.StorageConfig
      | doc "Storage and volume configuration"
      | default = {},

    deployment
      | Deployment.DeploymentConfig
      | doc "Deployment configuration (Docker Compose, Kubernetes)"
      | default = {target = 'docker},

    monitoring
      | Monitoring.MonitoringConfig
      | doc "Monitoring configuration (Prometheus, Grafana, etc.)"
      | default = {},

    security
      | Security.SecurityConfig
      | doc "Security configuration (TLS, secrets, auth)"
      | default = {},

    environment
      | {_: String}
      | doc "Global environment variables for all services"
      | default = {},

    metadata
      | {
        generated_at
          | String
          | doc "Timestamp when config was generated"
          | optional,

        generated_by
          | String
          | doc "Tool/user that generated the config"
          | optional,

        schema_version
          | String
          | doc "Schema version used"
          | default = "1.0.0",

        custom
          | {_: _}
          | doc "Custom metadata fields"
          | default = {},
      }
      | doc "Configuration metadata"
      | default = {},
  },

  # Export the main schema
  export = ProvisioningConfig,
}
