/**
 * Recherche entreprise via API data.gouv.fr
 * Auto-remplissage du formulaire de création client
 */
const API_URL = '/admin/clients/entreprise-search'

const computeTva = (siren) => {
    if (!siren) return ''
    const key = (12 + 3 * (Number.parseInt(siren, 10) % 97)) % 97
    return 'FR' + String(key).padStart(2, '0') + siren
}

const capitalize = (s) => s ? s.charAt(0).toUpperCase() + s.slice(1).toLowerCase() : ''

const fillField = (id, value) => {
    const el = document.getElementById(id)
    if (el) el.value = value
}

const fillFieldIfEmpty = (id, value) => {
    const el = document.getElementById(id)
    if (el && !el.value && value) el.value = value
}

const buildRcs = (siren, city) => {
    if (!siren || !city) return ''
    return 'RCS ' + capitalize(city) + ' ' + siren
}

const resolveTypeCompany = (natureJuridique) => {
    if (!natureJuridique) return ''
    const code = String(natureJuridique)
    if (code.startsWith('92') || code.startsWith('91') || code.startsWith('93')) return 'association'
    if (code.startsWith('10')) return 'auto-entrepreneur'
    if (code.startsWith('54') || code.startsWith('55')) return 'sarl'
    if (code.startsWith('57')) return 'sas'
    if (code.startsWith('52')) return 'eurl'
    /* istanbul ignore next -- dead code: 55xx already matched by sarl */
    if (code.startsWith('55') && code === '5599') return 'sa'
    if (code.startsWith('65')) return 'sci'
    return ''
}

const renderResult = (e, onSelect) => {
    /* istanbul ignore next */
    const s = e.siege || {}
    /* istanbul ignore next */
    const d = e.dirigeants?.[0] ?? {}
    const actif = e.etat_administratif === 'A'
    const addr = [s.numero_voie, s.type_voie, s.libelle_voie].filter(Boolean).join(' ')
    const ape = e.activite_principale || ''
    const rna = e.complements?.identifiant_association ?? ''
    const isAsso = resolveTypeCompany(e.nature_juridique) === 'association'

    const div = document.createElement('div')
    div.className = 'glass p-4 cursor-pointer hover:bg-white/80 transition-all'
    /* istanbul ignore next -- template literal fallbacks */
    div.innerHTML = `
        <div class="flex items-start justify-between gap-3">
            <div class="min-w-0">
                <div class="font-bold text-sm">${e.nom_complet || e.nom_raison_sociale || 'N/A'}</div>
                <div class="text-xs text-gray-500 mt-1">
                    SIREN <span class="font-mono font-bold">${e.siren || '?'}</span>
                    ${s.siret ? ' &mdash; SIRET <span class="font-mono font-bold">' + s.siret + '</span>' : ''}
                    ${ape ? ' &mdash; APE <span class="font-mono font-bold">' + ape + '</span>' : ''}
                    ${rna ? ' &mdash; RNA <span class="font-mono font-bold">' + rna + '</span>' : ''}
                </div>
                <div class="text-xs text-gray-400 mt-1">${s.geo_adresse || s.adresse || ''}</div>
                ${d.nom ? '<div class="text-xs text-gray-400 mt-1">Dirigeant : ' + (d.prenoms || '') + ' ' + d.nom + '</div>' : ''}
                ${isAsso ? '<div class="text-xs mt-1"><span class="px-1.5 py-0.5 bg-indigo-100 text-indigo-800 font-bold text-[10px] uppercase" >Association</span></div>' : ''}
            </div>
            <span class="shrink-0 px-2 py-1 text-[10px] font-bold uppercase ${actif ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'}" >
                ${actif ? 'Actif' : 'Ferme'}
            </span>
        </div>`

    /* istanbul ignore next -- click handler fallbacks */
    div.addEventListener('click', () => {
        fillField('raisonSociale', e.nom_raison_sociale || e.nom_complet || '')
        fillField('siret', s.siret || '')
        fillField('rcs', buildRcs(e.siren, s.libelle_commune))
        fillField('numTva', computeTva(e.siren))
        fillField('ape', ape)
        fillField('address', addr)
        fillField('zipCode', s.code_postal || '')
        fillField('city', s.libelle_commune || '')
        fillField('geoLat', s.latitude || '')
        fillField('geoLong', s.longitude || '')

        const typeCompany = resolveTypeCompany(e.nature_juridique)
        if (typeCompany) fillField('typeCompany', typeCompany)

        const rna = e.complements?.identifiant_association ?? ''
        if (rna) fillField('rna', rna)

        const prenom = (d.prenoms || '').split(' ')[0]
        fillFieldIfEmpty('firstName', capitalize(prenom))
        fillFieldIfEmpty('lastName', capitalize(d.nom))

        onSelect()
    })

    return div
}

export function initEntrepriseSearch() {
    const modal = document.getElementById('modal-entreprise')
    const overlay = document.getElementById('modal-overlay')
    const closeBtn = document.getElementById('modal-close')
    const openBtn = document.getElementById('btn-search-entreprise')
    const input = document.getElementById('search-entreprise-input')
    const searchBtn = document.getElementById('search-entreprise-btn')
    const resultsEl = document.getElementById('search-entreprise-results')
    const statusEl = document.getElementById('search-entreprise-status')

    if (!modal || !openBtn) return

    const openModal = () => { modal.classList.remove('hidden'); input.focus() }
    const closeModal = () => modal.classList.add('hidden')

    openBtn.addEventListener('click', openModal)
    closeBtn.addEventListener('click', closeModal)
    overlay.addEventListener('click', closeModal)
    document.addEventListener('keydown', (e) => { if (e.key === 'Escape') closeModal() })

    const doSearch = async () => {
        const q = input.value.trim()
        if (q.length < 2) return

        resultsEl.innerHTML = ''
        statusEl.textContent = 'Recherche en cours...'
        statusEl.classList.remove('hidden')

        try {
            const resp = await fetch(API_URL + '?q=' + encodeURIComponent(q) + '&page=1&per_page=10')
            const data = await resp.json()

            if (!data.results || data.results.length === 0) {
                statusEl.textContent = 'Aucun resultat trouve.'
                return
            }

            statusEl.textContent = data.total_results + ' resultat(s) - cliquez pour selectionner'
            data.results.forEach(e => resultsEl.appendChild(renderResult(e, closeModal)))
        } catch (err) {
            statusEl.textContent = 'Erreur : ' + err.message
        }
    }

    searchBtn.addEventListener('click', doSearch)
    /* istanbul ignore next -- keydown non-Enter branch */
    input.addEventListener('keydown', (e) => { if (e.key === 'Enter') { e.preventDefault(); doSearch() } })
}
