import Foundation
import SearchModels
import ServicesModels
import SharedConstants
// `Services.HIGQuery` value type lifted to the `ServicesModels` target
// so callers can construct HIG queries without importing the full
// `Services` target.

// MARK: - HIG Search Service

/// Service for searching Human Interface Guidelines.
/// Delegates to Services.DocsSearchService with HIG-specific source filtering.
extension Services {
    public actor HIGSearchService {
        private let docsService: Services.DocsSearchService

        /// Initialize with an existing docs service.
        public init(docsService: Services.DocsSearchService) {
            self.docsService = docsService
        }

        /// Initialize with any `Search.Database` conformer.
        /// Production: pass a `Search.Index`; tests pass a mock.
        public init(database: any Search.Database) {
            docsService = Services.DocsSearchService(database: database)
        }

        // MARK: - Search Methods

        /// Search HIG with specialized query
        public func search(_ query: Services.HIGQuery) async throws -> [Search.Result] {
            // Build effective query with platform/category filters
            var effectiveText = query.text
            if let platform = query.platform {
                effectiveText += " \(platform)"
            }
            if let category = query.category {
                effectiveText += " \(category)"
            }

            let searchQuery = Services.SearchQuery(
                text: effectiveText,
                source: Shared.Constants.SourcePrefix.hig,
                limit: query.limit,
                includeArchive: false
            )

            return try await docsService.search(searchQuery)
        }

        /// Simple text search in HIG
        public func search(text: String, limit: Int = Shared.Constants.Limit.defaultSearchLimit) async throws -> [Search.Result] {
            try await search(Services.HIGQuery(text: text, limit: limit))
        }

        /// Search HIG for a specific platform
        public func search(text: String, platform: String, limit: Int = Shared.Constants.Limit.defaultSearchLimit) async throws -> [Search.Result] {
            try await search(Services.HIGQuery(text: text, platform: platform, limit: limit))
        }

        /// Search HIG for a specific category
        public func search(text: String, category: String, limit: Int = Shared.Constants.Limit.defaultSearchLimit) async throws -> [Search.Result] {
            try await search(Services.HIGQuery(text: text, category: category, limit: limit))
        }

        // MARK: - Document Access

        /// Read a HIG document by URI
        public func read(uri: String, format: Search.DocumentFormat = .json) async throws -> String? {
            try await docsService.read(uri: uri, format: format)
        }

        /// Disconnect from the underlying database
        public func disconnect() async {
            await docsService.disconnect()
        }
    }
}
