'use strict'
const { httpRequest } = require('./helpers')

const SUPABASE_URL = 'https://rddrjzptzhypltuzmere.supabase.co'
const SUPABASE_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InJkZHJqenB0emh5cGx0dXptZXJlIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzA4MTY4NTYsImV4cCI6MjA4NjM5Mjg1Nn0.EluFlKBze8BYM6AFx88G7kt21EvR18EI3uw1zgCXVzs'

function wordsToIlike (str) {
  const words = str.split(/\s+/).filter(w => w.length >= 2)
  if (!words.length) return ''
  return '*' + words.map(w => encodeURIComponent(w)).join('*') + '*'
}

async function searchAddresses (term, limit = 8) {
  const clean = term.trim()
  if (clean.length < 3) return []

  const numMatch = clean.match(/^\s*(\d+)\s*(.*)/)
  const headers = { apikey: SUPABASE_KEY, Authorization: 'Bearer ' + SUPABASE_KEY }
  const select = 'adresse_formatee,numero_municipal,numero_unite,code_postal,odonyme_recompose_normal,nom_municipalite,latitude,longitude,identifiant_unique_adresse'
  const base = `${SUPABASE_URL}/rest/v1/addresses?select=${select}&limit=${limit}`

  let results = []

  if (numMatch) {
    const num = numMatch[1]
    const street = numMatch[2].trim()
    let url = `${base}&numero_municipal=eq.${num}`
    if (street) url += `&odonyme_recompose_normal=ilike.${wordsToIlike(street)}`
    url += '&order=nom_municipalite'
    const res = await httpRequest(url, '', { headers })
    results = Array.isArray(res.data) ? res.data : []

    if (!results.length && num.length >= 2) {
      let url2 = `${base}&numero_municipal=like.${num}*`
      if (street) url2 += `&odonyme_recompose_normal=ilike.${wordsToIlike(street)}`
      url2 += '&order=nom_municipalite'
      const res2 = await httpRequest(url2, '', { headers })
      results = Array.isArray(res2.data) ? res2.data : []
    }
  } else {
    const pattern = wordsToIlike(clean)
    if (!pattern) return []
    const url = `${base}&odonyme_recompose_normal=ilike.${pattern}&order=nom_municipalite`
    const res = await httpRequest(url, '', { headers })
    results = Array.isArray(res.data) ? res.data : []
  }

  return results.map(a => ({ ...a, fiber_available: false }))
}

module.exports = { searchAddresses, wordsToIlike }
