// GigLez - Search Functionality

async function searchCaptures() {
    const query = document.getElementById('search-query').value;
    const frequency = document.getElementById('search-frequency').value;
    const protocol = document.getElementById('search-protocol').value;
    const dateStart = document.getElementById('search-date-start').value;
    const dateEnd = document.getElementById('search-date-end').value;
    const lat = document.getElementById('search-lat').value;
    const lon = document.getElementById('search-lon').value;
    const radius = document.getElementById('search-radius').value;

    // Build query parameters
    const params = new URLSearchParams();

    if (query) params.append('q', query);
    if (frequency) params.append('frequency', parseFloat(frequency) * 1e6);
    if (protocol) params.append('protocol', protocol);
    if (dateStart) params.append('date_start', dateStart);
    if (dateEnd) params.append('date_end', dateEnd);

    // Geographic search
    if (lat && lon && radius) {
        params.append('latitude', lat);
        params.append('longitude', lon);
        params.append('radius_km', radius);
    }

    try {
        const response = await fetch(`/api/v1/query/captures?${params.toString()}`);

        if (!response.ok) {
            throw new Error('Search failed');
        }

        const data = await response.json();
        displaySearchResults(data.captures || []);

    } catch (error) {
        console.error('Search error:', error);
        alert(`Search failed: ${error.message}`);
    }
}

function displaySearchResults(captures) {
    const resultsDiv = document.getElementById('search-results');
    const resultsList = document.getElementById('results-list');

    resultsDiv.style.display = 'block';

    if (captures.length === 0) {
        resultsList.innerHTML = '<p>No results found. Try adjusting your search criteria.</p>';
        return;
    }

    resultsList.innerHTML = captures.map(capture => createResultCard(capture)).join('');
}

function createResultCard(capture) {
    const freqMHz = (capture.frequency / 1e6).toFixed(2);
    const date = new Date(capture.timestamp || Date.now()).toLocaleString();

    const deviceName = capture.device_name || 'Unknown Device';
    const protocol = capture.protocol || 'RAW';
    const confidence = capture.match_confidence
        ? `${(capture.match_confidence * 100).toFixed(0)}%`
        : 'N/A';

    return `
        <div class="result-card" onclick="viewCaptureDetails(${capture.id})">
            <div class="result-header">
                <div class="result-title">${deviceName}</div>
                <div class="result-frequency">${freqMHz} MHz</div>
            </div>
            <div class="result-details">
                <div>
                    <strong>Protocol:</strong> ${protocol}
                </div>
                <div>
                    <strong>Confidence:</strong> ${confidence}
                </div>
                <div>
                    <strong>Captured:</strong> ${date}
                </div>
                <div>
                    <strong>Location:</strong> ${capture.latitude.toFixed(4)}, ${capture.longitude.toFixed(4)}
                </div>
            </div>
        </div>
    `;
}
