# Infrastructure - Docker Compose Schema
# Type-safe Docker Compose configuration generation

{
  # Valid Docker Compose versions
  ComposeVersion = [| 'v3, 'v3_8, 'v3_9 |],

  # Service resource limits
  ResourceLimits = {
    cpus | String,
    memory | String,
  },

  # Service deployment configuration
  DeployConfig = {
    resources | {limits | ResourceLimits} | default = {
      resources = {
        limits = {
          cpus = "1.0",
          memory = "512M",
        }
      }
    },
    replicas | Number | default = 1,
    restart_policy | [| 'no, 'always, 'on_failure, 'unless_stopped |] | default = 'unless_stopped,
  },

  # Health check configuration
  HealthCheck = {
    test | Array String,
    interval | String,
    timeout | String,
    retries | Number | default = 3,
    start_period | String | default = "0s",
  },

  # Service port mapping
  PortMapping = {
    container_port | Number | {
      predicate = fun n => n > 0 && n < 65536,
    },
    host_port | Number | {
      predicate = fun n => n > 0 && n < 65536,
    },
    protocol | [| 'tcp, 'udp |] | default = 'tcp,
  },

  # Volume configuration
  Volume = {
    path | String,
    volume_name | String | optional,
    bind_mount | String | optional,
    read_only | Bool | default = false,
  },

  # Service dependency
  Dependency = {
    service | String,
    condition | [| 'service_started, 'service_healthy, 'service_completed_successfully |]
      | default = 'service_started,
  },

  # Docker Compose Service definition
  Service = {
    image | String,
    container_name | String | optional,
    command | Array String | optional,
    environment | {_ | String} | default = {},
    ports | Array PortMapping | default = [],
    volumes | Array Volume | default = [],
    depends_on | Array Dependency | default = [],
    deploy | DeployConfig | default = {
      resources = {
        limits = {
          cpus = "1.0",
          memory = "512M",
        }
      },
      replicas = 1,
      restart_policy = 'unless_stopped,
    },
    healthcheck | HealthCheck | optional,
    restart_policy | [| 'no, 'always, 'on_failure, 'unless_stopped |] | default = 'unless_stopped,
    networks | Array String | default = ["provisioning-net"],
    profiles | Array String | default = [],
  },

  # Named volume definition
  NamedVolume = {
    driver | String | default = "local",
    driver_opts | {_ | String} | default = {},
  },

  # Network definition
  Network = {
    driver | String | default = "bridge",
    ipam | {
      driver | String | default = "default",
      config | Array {
        subnet | String | optional,
      } | default = [],
    } | default = {
      driver = "default",
      config = [],
    },
  },

  # Complete Docker Compose configuration
  DockerCompose = {
    version | String | default = "3.9",
    services | {_ | Service},
    volumes | {_ | NamedVolume} | default = {},
    networks | {_ | Network} | default = {
      provisioning_net = {
        driver = "bridge",
      }
    },
  },

  # Solo mode preset
  soloModePreset = {
    orchestrator = {
      deploy.resources.limits = {
        cpus = "1.0",
        memory = "1024M",
      },
    },
    control_center = {
      deploy.resources.limits = {
        cpus = "0.5",
        memory = "512M",
      },
    },
    coredns = {
      deploy.resources.limits = {
        cpus = "0.25",
        memory = "256M",
      },
    },
    oci_registry = {
      deploy.resources.limits = {
        cpus = "0.5",
        memory = "512M",
      },
    },
    kms = {
      deploy.resources.limits = {
        cpus = "0.5",
        memory = "512M",
      },
    },
  },

  # Multiuser mode preset
  multiuserModePreset = {
    orchestrator = {
      deploy.resources.limits = {
        cpus = "2.0",
        memory = "2048M",
      },
      deploy.replicas = 2,
    },
    control_center = {
      deploy.resources.limits = {
        cpus = "1.0",
        memory = "1024M",
      },
      deploy.replicas = 2,
    },
    extension_registry = {
      deploy.resources.limits = {
        cpus = "1.0",
        memory = "1024M",
      },
    },
    postgres = {
      deploy.resources.limits = {
        cpus = "1.0",
        memory = "2048M",
      },
    },
    oci_registry = {
      deploy.resources.limits = {
        cpus = "1.0",
        memory = "1024M",
      },
    },
  },

  # Enterprise mode preset
  enterpriseModePreset = {
    orchestrator = {
      deploy.resources.limits = {
        cpus = "4.0",
        memory = "4096M",
      },
      deploy.replicas = 3,
    },
    control_center = {
      deploy.resources.limits = {
        cpus = "2.0",
        memory = "2048M",
      },
      deploy.replicas = 3,
    },
    prometheus = {
      deploy.resources.limits = {
        cpus = "1.0",
        memory = "2048M",
      },
    },
    grafana = {
      deploy.resources.limits = {
        cpus = "1.0",
        memory = "1024M",
      },
    },
    elasticsearch = {
      deploy.resources.limits = {
        cpus = "2.0",
        memory = "2048M",
      },
    },
    kibana = {
      deploy.resources.limits = {
        cpus = "1.0",
        memory = "512M",
      },
    },
  },
}
