<?php

$bool = static function (string $name, bool $default): bool {
    $value = getenv($name);
    if ($value === false || $value === '') {
        return $default;
    }

    return in_array(strtolower((string) $value), ['1', 'true', 'yes', 'on'], true);
};

$hosts = getenv('OPENSEARCH_HOSTS') ?: getenv('OPENSEARCH_HOST') ?: 'http://127.0.0.1:9200';
$hosts = array_values(array_filter(array_map('trim', explode(',', $hosts))));

return [
    /*
     * OpenSearch connection configuration.
     *
     * Recommended environment variables:
     * - OPENSEARCH_HOSTS=http://127.0.0.1:9200
     * - OPENSEARCH_USERNAME=admin
     * - OPENSEARCH_PASSWORD=...
     * - OPENSEARCH_SSL_VERIFY=false
     * - OPENSEARCH_INDEX_CHUNKS=proofdb_chunks
     */
    'default' => [
        'hosts' => $hosts,
        'username' => getenv('OPENSEARCH_USERNAME') ?: null,
        'password' => getenv('OPENSEARCH_PASSWORD') ?: null,
        'ssl_verify' => $bool('OPENSEARCH_SSL_VERIFY', false),
        'timeout' => (float) (getenv('OPENSEARCH_TIMEOUT') ?: 30),
        'connect_timeout' => (float) (getenv('OPENSEARCH_CONNECT_TIMEOUT') ?: 5),
    ],

    'indices' => [
        'chunks' => getenv('OPENSEARCH_INDEX_CHUNKS') ?: 'proofdb_chunks',
    ],

    'bulk' => [
        'refresh' => getenv('OPENSEARCH_BULK_REFRESH') ?: 'false',
        'chunk_size' => (int) (getenv('OPENSEARCH_BULK_CHUNK_SIZE') ?: 500),
    ],

    'vector' => [
        'dimensions' => (int) (getenv('OPENSEARCH_VECTOR_DIMENSIONS') ?: 2048),
        'space_type' => getenv('OPENSEARCH_VECTOR_SPACE_TYPE') ?: 'cosinesimil',
        'engine' => getenv('OPENSEARCH_VECTOR_ENGINE') ?: 'lucene',
    ],
];
