# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2025-2026, by Samuel Williams.

require "async/service/environment"
require "async/service/managed/environment"

require_relative "service"
require_relative "server"

module Async
	module Service
		module Supervisor
			# An environment mixin for supervisor services.
			#
			# Provides configuration and setup for supervisor processes that monitor workers.
			module Environment
				include Async::Service::Managed::Environment
				
				# The service class to use for the supervisor.
				# @returns [Class]
				def service_class
					Supervisor::Service
				end
				
				# The name of the supervisor
				# @returns [String]
				def name
					"supervisor"
				end
				
				# The IPC path to use for communication with the supervisor.
				# @returns [String]
				def ipc_path
					::File.expand_path("supervisor.ipc", root)
				end
				
				# The endpoint the supervisor will bind to.
				# @returns [::IO::Endpoint::Generic]
				def endpoint
					::IO::Endpoint.unix(ipc_path)
				end
				
				# Number of supervisor instances (always 1).
				# @returns [Integer]
				def count
					1
				end
				
				# Options to use when creating the container.
				# Merges with Managed::Environment defaults.
				def container_options
					super.merge(restart: true, count: self.count)
				end
				
				# Get the list of monitors to run in the supervisor.
				#
				# Override this method to provide custom monitors.
				#
				# @returns [Array] The list of monitor instances.
				def monitors
					[]
				end
				
				# Create the supervisor server instance.
				#
				# @returns [Server] The supervisor server.
				def make_server(endpoint)
					Server.new(endpoint: endpoint, monitors: self.monitors)
				end
			end
		end
	end
end

