<?php

declare(strict_types=1);

namespace App\Framework\Search;

/**
 * Main search service providing a fluent API
 */
final readonly class SearchService
{
    public function __construct(
        private SearchEngine $engine,
        private SearchIndexManager $indexManager
    ) {
    }

    /**
     * Create a search query builder for an entity type
     */
    public function for(string $entityType): SearchQueryBuilder
    {
        return new SearchQueryBuilder($this->engine, $entityType);
    }

    /**
     * Execute a pre-built search query
     */
    public function search(SearchQuery $query): SearchResult
    {
        return $this->engine->search($query);
    }

    /**
     * Index a document
     */
    public function index(string $entityType, string $id, array $document): bool
    {
        return $this->engine->index($entityType, $id, $document);
    }

    /**
     * Index multiple documents at once
     *
     * @param SearchDocument[] $documents
     */
    public function bulkIndex(array $documents): BulkIndexResult
    {
        return $this->engine->bulkIndex($documents);
    }

    /**
     * Delete a document from the search index
     */
    public function delete(string $entityType, string $id): bool
    {
        return $this->engine->delete($entityType, $id);
    }

    /**
     * Update an existing document
     */
    public function update(string $entityType, string $id, array $document): bool
    {
        return $this->engine->update($entityType, $id, $document);
    }

    /**
     * Get search engine statistics
     */
    public function getStats(): SearchEngineStats
    {
        return $this->engine->getStats();
    }

    /**
     * Check if search engine is available
     */
    public function isAvailable(): bool
    {
        return $this->engine->isAvailable();
    }

    /**
     * Get the underlying search engine
     */
    public function getEngine(): SearchEngine
    {
        return $this->engine;
    }

    /**
     * Get the index manager
     */
    public function getIndexManager(): SearchIndexManager
    {
        return $this->indexManager;
    }
}
