{% extends "base.html" %}

{% block content %}
<style>
    .tag-cloud {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
        padding: 15px;
        border-radius: 8px;
        background: rgba(0, 0, 0, 0.05);
        margin-bottom: 20px;
    }

    .tag {
        display: inline-block;
        padding: 5px 10px;
        border-radius: 15px;
        background-color: #dc3545;
        color: white;
        cursor: pointer;
        font-size: 14px;
        transition: all 0.3s ease;
        user-select: none;
    }

    .tag.selected {
        background-color: #28a745;
    }

    .tag.excluded {
        background-color: #343a40;
        text-decoration: line-through;
    }

    .tag:hover {
        transform: translateY(-2px);
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
    }

    .hidden-select {
        display: none !important;
    }

    .site-input-container {
        position: relative;
    }

    .site-input {
        width: 100%;
    }

    .selected-sites {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
        padding: 10px 0;
    }

    .selected-site {
        background-color: #214e7b;
        padding: 2px 8px;
        border-radius: 12px;
        font-size: 14px;
        display: inline-flex;
        align-items: center;
        gap: 5px;
    }

    .remove-site {
        cursor: pointer;
        color: #dc3545;
        font-weight: bold;
    }

    .section-header {
        cursor: pointer;
        padding: 1rem;
        background: rgba(255, 255, 255, 0.05);
        border-radius: 4px;
        margin-bottom: 0.5rem;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .section-content {
        padding: 1rem;
        display: none;
    }

    .section-content.show {
        display: block;
    }

    .chevron::after {
        content: '▼';
        transition: transform 0.2s;
    }

    .chevron.collapsed::after {
        transform: rotate(-90deg);
    }

    .main-search-section {
        background: rgba(255, 255, 255, 0.03);
        padding: 2rem;
        border-radius: 8px;
        margin-bottom: 2rem;
    }

    .search-button {
        width: 100%;
        padding: 1rem;
        font-size: 1.2rem;
        margin-top: 2rem;
    }
</style>

<div class="form-container">
    {% if error %}
    <div class="alert alert-danger">{{ error }}</div>
    {% endif %}

    <form method="POST" action="{{ url_for('search') }}" class="mb-4">
        <!-- Main Search Section -->
        <div class="main-search-section">
            <div class="mb-4">
                <label for="usernames" class="form-label h5">Usernames to Search</label>
                <textarea class="form-control" id="usernames" name="usernames" rows="3" required
                    placeholder="Enter one or more usernames (separated by spaces or commas)..."></textarea>
            </div>

            <div class="row align-items-center">
                <div class="col-md-6">
                    <label for="top_sites" class="form-label">Number of Sites</label>
                    <input type="number" class="form-control" id="top_sites" name="top_sites" min="1" max="10000"
                        placeholder="Default: 500">
                </div>
                <div class="col-md-6">
                    <label for="timeout" class="form-label">Timeout (seconds)</label>
                    <input type="number" class="form-control" id="timeout" name="timeout" min="1"
                        placeholder="Default: 30">
                </div>
                <div class="col-12 mt-3">
                    <div class="form-check">
                        <input type="checkbox" class="form-check-input" id="all_sites" name="all_sites"
                            onchange="document.getElementById('top_sites').disabled = this.checked;">
                        <label class="form-check-label" for="all_sites">Search All Sites</label>
                    </div>
                </div>
            </div>
        </div>

        <!-- Filters Section -->
        <div class="mb-4">
            <div class="section-header" onclick="toggleSection('filters')">
                <h5 class="mb-0">Filters</h5>
                <span class="chevron"></span>
            </div>
            <div id="filters" class="section-content">
                <div class="mb-3 site-input-container">
                    <label for="site" class="form-label">Specify Sites (Optional)</label>
                    <input type="text" class="form-control site-input" id="siteInput"
                        placeholder="Type to search for sites..." list="siteOptions">
                    <input type="hidden" id="site" name="site">
                    <datalist id="siteOptions">
                        {% for site in site_options %}
                        <option value="{{ site }}">
                            {% endfor %}
                    </datalist>
                    <div class="selected-sites" id="selectedSites"></div>
                </div>

                <div class="mb-3">
                    <label class="form-label">Tags (click to cycle: include → exclude → neutral)</label>
                    <div class="mb-2">
                        <small class="text-muted">
                            <span style="display:inline-block;width:12px;height:12px;background:#28a745;border-radius:50%;"></span> Included (whitelist)
                            &nbsp;&nbsp;
                            <span style="display:inline-block;width:12px;height:12px;background:#343a40;border-radius:50%;"></span> Excluded (blacklist)
                            &nbsp;&nbsp;
                            <span style="display:inline-block;width:12px;height:12px;background:#dc3545;border-radius:50%;"></span> Neutral
                        </small>
                    </div>
                    <div class="tag-cloud" id="tagCloud"></div>
                    <select multiple class="hidden-select" id="tags" name="tags">
                        <option value="gaming">Gaming</option>
                        <option value="coding">Coding</option>
                        <option value="photo">Photo</option>
                        <option value="music">Music</option>
                        <option value="blog">Blog</option>
                        <option value="finance">Finance</option>
                        <option value="freelance">Freelance</option>
                        <option value="dating">Dating</option>
                        <option value="tech">Tech</option>
                        <option value="forum">Forum</option>
                        <option value="porn">Porn</option>
                        <option value="erotic">Erotic</option>
                        <option value="webcam">Webcam</option>
                        <option value="video">Video</option>
                        <option value="movies">Movies</option>
                        <option value="hacking">Hacking</option>
                        <option value="art">Art</option>
                        <option value="discussion">Discussion</option>
                        <option value="sharing">Sharing</option>
                        <option value="writing">Writing</option>
                        <option value="wiki">Wiki</option>
                        <option value="business">Business</option>
                        <option value="shopping">Shopping</option>
                        <option value="sport">Sport</option>
                        <option value="books">Books</option>
                        <option value="news">News</option>
                        <option value="documents">Documents</option>
                        <option value="travel">Travel</option>
                        <option value="maps">Maps</option>
                        <option value="hobby">Hobby</option>
                        <option value="apps">Apps</option>
                        <option value="classified">Classified</option>
                        <option value="career">Career</option>
                        <option value="geosocial">Geosocial</option>
                        <option value="streaming">Streaming</option>
                        <option value="education">Education</option>
                        <option value="networking">Networking</option>
                        <option value="torrent">Torrent</option>
                        <option value="science">Science</option>
                        <option value="medicine">Medicine</option>
                        <option value="reading">Reading</option>
                        <option value="stock">Stock</option>
                        <option value="messaging">Messaging</option>
                        <option value="trading">Trading</option>
                        <option value="links">Links</option>
                        <option value="fashion">Fashion</option>
                        <option value="tasks">Tasks</option>
                        <option value="military">Military</option>
                        <option value="auto">Auto</option>
                        <option value="gambling">Gambling</option>
                        <option value="cybercriminal">Cybercriminal</option>
                        <option value="review">Review</option>
                        <option value="bookmarks">Bookmarks</option>
                        <option value="design">Design</option>
                        <option value="tor">Tor</option>
                        <option value="i2p">I2P</option>
                        <option value="q&a">Q&A</option>
                        <option value="crypto">Crypto</option>
                        <option value="ai">AI</option>
                        <!-- Country tags -->
                        <option value="ae" data-group="country">AE - United Arab Emirates</option>
                        <option value="ao" data-group="country">AO - Angola</option>
                        <option value="ar" data-group="country">AR - Argentina</option>
                        <option value="at" data-group="country">AT - Austria</option>
                        <option value="au" data-group="country">AU - Australia</option>
                        <option value="az" data-group="country">AZ - Azerbaijan</option>
                        <option value="bd" data-group="country">BD - Bangladesh</option>
                        <option value="be" data-group="country">BE - Belgium</option>
                        <option value="bg" data-group="country">BG - Bulgaria</option>
                        <option value="br" data-group="country">BR - Brazil</option>
                        <option value="by" data-group="country">BY - Belarus</option>
                        <option value="ca" data-group="country">CA - Canada</option>
                        <option value="ch" data-group="country">CH - Switzerland</option>
                        <option value="cl" data-group="country">CL - Chile</option>
                        <option value="cn" data-group="country">CN - China</option>
                        <option value="co" data-group="country">CO - Colombia</option>
                        <option value="cr" data-group="country">CR - Costa Rica</option>
                        <option value="cz" data-group="country">CZ - Czechia</option>
                        <option value="de" data-group="country">DE - Germany</option>
                        <option value="dk" data-group="country">DK - Denmark</option>
                        <option value="dz" data-group="country">DZ - Algeria</option>
                        <option value="ee" data-group="country">EE - Estonia</option>
                        <option value="eg" data-group="country">EG - Egypt</option>
                        <option value="es" data-group="country">ES - Spain</option>
                        <option value="eu" data-group="country">EU - European Union</option>
                        <option value="fi" data-group="country">FI - Finland</option>
                        <option value="fr" data-group="country">FR - France</option>
                        <option value="gb" data-group="country">GB - United Kingdom</option>
                        <option value="global" data-group="country">🌍 Global</option>
                        <option value="gr" data-group="country">GR - Greece</option>
                        <option value="hk" data-group="country">HK - Hong Kong</option>
                        <option value="hr" data-group="country">HR - Croatia</option>
                        <option value="hu" data-group="country">HU - Hungary</option>
                        <option value="id" data-group="country">ID - Indonesia</option>
                        <option value="ie" data-group="country">IE - Ireland</option>
                        <option value="il" data-group="country">IL - Israel</option>
                        <option value="in" data-group="country">IN - India</option>
                        <option value="ir" data-group="country">IR - Iran</option>
                        <option value="it" data-group="country">IT - Italy</option>
                        <option value="jp" data-group="country">JP - Japan</option>
                        <option value="kg" data-group="country">KG - Kyrgyzstan</option>
                        <option value="kr" data-group="country">KR - Korea</option>
                        <option value="kz" data-group="country">KZ - Kazakhstan</option>
                        <option value="la" data-group="country">LA - Laos</option>
                        <option value="lk" data-group="country">LK - Sri Lanka</option>
                        <option value="lt" data-group="country">LT - Lithuania</option>
                        <option value="ma" data-group="country">MA - Morocco</option>
                        <option value="md" data-group="country">MD - Moldova</option>
                        <option value="mg" data-group="country">MG - Madagascar</option>
                        <option value="mk" data-group="country">MK - North Macedonia</option>
                        <option value="mx" data-group="country">MX - Mexico</option>
                        <option value="ng" data-group="country">NG - Nigeria</option>
                        <option value="nl" data-group="country">NL - Netherlands</option>
                        <option value="no" data-group="country">NO - Norway</option>
                        <option value="ph" data-group="country">PH - Philippines</option>
                        <option value="pk" data-group="country">PK - Pakistan</option>
                        <option value="pl" data-group="country">PL - Poland</option>
                        <option value="pt" data-group="country">PT - Portugal</option>
                        <option value="re" data-group="country">RE - Réunion</option>
                        <option value="ro" data-group="country">RO - Romania</option>
                        <option value="rs" data-group="country">RS - Serbia</option>
                        <option value="ru" data-group="country">RU - Russia</option>
                        <option value="sa" data-group="country">SA - Saudi Arabia</option>
                        <option value="sd" data-group="country">SD - Sudan</option>
                        <option value="se" data-group="country">SE - Sweden</option>
                        <option value="sg" data-group="country">SG - Singapore</option>
                        <option value="sk" data-group="country">SK - Slovakia</option>
                        <option value="sv" data-group="country">SV - El Salvador</option>
                        <option value="th" data-group="country">TH - Thailand</option>
                        <option value="tn" data-group="country">TN - Tunisia</option>
                        <option value="tr" data-group="country">TR - Türkiye</option>
                        <option value="tw" data-group="country">TW - Taiwan</option>
                        <option value="ua" data-group="country">UA - Ukraine</option>
                        <option value="uk" data-group="country">UK - United Kingdom</option>
                        <option value="us" data-group="country">US - United States</option>
                        <option value="uz" data-group="country">UZ - Uzbekistan</option>
                        <option value="ve" data-group="country">VE - Venezuela</option>
                        <option value="vi" data-group="country">VI - Virgin Islands</option>
                        <option value="vn" data-group="country">VN - Viet Nam</option>
                        <option value="za" data-group="country">ZA - South Africa</option>
                    </select>
                    <select multiple class="hidden-select" id="excludedTags" name="excluded_tags">
                    </select>
                </div>
            </div>
        </div>

        <!-- Advanced Options Section -->
        <div class="mb-4">
            <div class="section-header" onclick="toggleSection('advanced')">
                <h5 class="mb-0">Advanced Options</h5>
                <span class="chevron"></span>
            </div>
            <div id="advanced" class="section-content">
                <div class="mb-3 form-check">
                    <input type="checkbox" class="form-check-input" id="permute" name="permute">
                    <label class="form-check-label" for="permute">Enable Username Permutations</label>
                </div>
                <div class="mb-3 form-check">
                    <input type="checkbox" class="form-check-input" id="disable_recursive_search"
                        name="disable_recursive_search">
                    <label class="form-check-label" for="disable_recursive_search">Disable Recursive Search</label>
                </div>
                <div class="mb-3 form-check">
                    <input type="checkbox" class="form-check-input" id="disable_extracting" name="disable_extracting">
                    <label class="form-check-label" for="disable_extracting">Disable Information Extraction</label>
                </div>
                <div class="mb-3 form-check">
                    <input type="checkbox" class="form-check-input" id="with_domains" name="with_domains">
                    <label class="form-check-label" for="with_domains">Check Domains</label>
                </div>
                <div class="mb-3">
                    <label for="proxy" class="form-label">Proxy URL</label>
                    <input type="text" class="form-control" id="proxy" name="proxy"
                        placeholder="e.g., 127.0.0.1:1080">
                </div>
                <div class="mb-3">
                    <label for="tor_proxy" class="form-label">TOR Proxy URL</label>
                    <input type="text" class="form-control" id="tor_proxy" name="tor_proxy"
                        placeholder="Default: 127.0.0.1:9050">
                </div>
                <div class="mb-3">
                    <label for="i2p_proxy" class="form-label">I2P Proxy URL</label>
                    <input type="text" class="form-control" id="i2p_proxy" name="i2p_proxy"
                        placeholder="Default: 127.0.0.1:4444">
                </div>
            </div>
        </div>

        <button type="submit" class="btn search-button" style="background-color: rgb(249, 207, 0); color: black;">
            Start Search
        </button>
    </form>
</div>

<script>
    function toggleSection(sectionId) {
        const content = document.getElementById(sectionId);
        const header = content.previousElementSibling;
        content.classList.toggle('show');
        header.querySelector('.chevron').classList.toggle('collapsed');
    }

    document.addEventListener('DOMContentLoaded', function () {
        // Tag cloud functionality with include/exclude (whitelist/blacklist) support
        const tagCloud = document.getElementById('tagCloud');
        const hiddenSelect = document.getElementById('tags');
        const excludedSelect = document.getElementById('excludedTags');
        const allTags = Array.from(hiddenSelect.options).map(opt => ({
            value: opt.value,
            label: opt.text,
            group: opt.dataset.group || 'category'
        }));

        function updateTagSelects() {
            // Clear and repopulate hidden selects based on tag states
            Array.from(hiddenSelect.options).forEach(opt => opt.selected = false);
            // Clear excluded select
            excludedSelect.innerHTML = '';

            document.querySelectorAll('#tagCloud .tag').forEach(tagEl => {
                const val = tagEl.dataset.value;
                if (tagEl.classList.contains('selected')) {
                    const option = Array.from(hiddenSelect.options).find(opt => opt.value === val);
                    if (option) option.selected = true;
                } else if (tagEl.classList.contains('excluded')) {
                    const opt = document.createElement('option');
                    opt.value = val;
                    opt.selected = true;
                    excludedSelect.appendChild(opt);
                }
            });
        }

        let lastGroup = '';
        allTags.forEach(tag => {
            if (tag.group !== lastGroup && tag.group === 'country') {
                const separator = document.createElement('div');
                separator.style.cssText = 'width:100%;margin:8px 0 4px;padding:4px 0;border-top:1px solid rgba(0,0,0,0.15);font-size:13px;color:#666;';
                separator.textContent = 'Countries';
                tagCloud.appendChild(separator);
            }
            lastGroup = tag.group;

            const tagElement = document.createElement('span');
            tagElement.className = 'tag';
            tagElement.textContent = tag.label;
            tagElement.dataset.value = tag.value;

            // Single click cycles: neutral -> included -> excluded -> neutral
            tagElement.addEventListener('click', function (e) {
                e.preventDefault();
                if (this.classList.contains('selected')) {
                    // included -> excluded
                    this.classList.remove('selected');
                    this.classList.add('excluded');
                } else if (this.classList.contains('excluded')) {
                    // excluded -> neutral
                    this.classList.remove('excluded');
                } else {
                    // neutral -> included
                    this.classList.add('selected');
                }
                updateTagSelects();
            });

            tagCloud.appendChild(tagElement);
        });

        // Site selection functionality
        const siteInput = document.getElementById('siteInput');
        const hiddenInput = document.getElementById('site');
        const selectedSitesContainer = document.getElementById('selectedSites');
        let selectedSites = new Set();

        function updateHiddenInput() {
            hiddenInput.value = Array.from(selectedSites).join(',');
        }

        function addSite(site) {
            if (site && !selectedSites.has(site)) {
                selectedSites.add(site);
                updateHiddenInput();
                const siteElement = document.createElement('span');
                siteElement.className = 'selected-site';
                siteElement.innerHTML = `${site}<span class="remove-site" data-site="${site}">&times;</span>`;
                selectedSitesContainer.appendChild(siteElement);
            }
        }

        function removeSite(site) {
            selectedSites.delete(site);
            updateHiddenInput();
            const siteElements = selectedSitesContainer.querySelectorAll('.selected-site');
            siteElements.forEach(el => {
                if (el.querySelector('.remove-site').dataset.site === site) {
                    el.remove();
                }
            });
        }

        siteInput.addEventListener('change', function (e) {
            const value = this.value.trim();
            if (value) {
                addSite(value);
                this.value = '';
            }
        });

        selectedSitesContainer.addEventListener('click', function (e) {
            if (e.target.classList.contains('remove-site')) {
                removeSite(e.target.dataset.site);
            }
        });

        siteInput.addEventListener('paste', function (e) {
            e.preventDefault();
            const paste = (e.clipboardData || window.clipboardData).getData('text');
            const sites = paste.split(',').map(site => site.trim()).filter(site => site);
            sites.forEach(addSite);
        });

        const form = document.querySelector('form');
        form.addEventListener('submit', function (e) {
            const selectedTags = Array.from(tagCloud.querySelectorAll('.tag.selected'));
            Array.from(hiddenSelect.options).forEach(opt => {
                opt.selected = selectedTags.some(tag => tag.dataset.value === opt.value);
            });
            updateHiddenInput();
        });
    });
</script>
{% endblock %}