import "dotenv/config"
import { prisma } from "../src/server/db"

const API_KEY = process.env.SMTP2GO_API_KEY
if (!API_KEY) throw new Error('Missing SMTP2GO_API_KEY')

const BASE = 'https://api.smtp2go.com/v3'

async function main(){
  const res = await fetch(`${BASE}/subaccounts/search`, {
    method: 'POST',
    headers: { 'content-type': 'application/json' },
    body: JSON.stringify({ api_key: API_KEY, fuzzy_search: true, states: 'all', sort_direction: 'asc', page_size: 50 }),
  })
  const json = await res.json().catch(() => ({}))
  const items = Array.isArray(json?.data?.subaccounts) ? json.data.subaccounts : []
  console.log('remote count', items.length)
  const ids = items.map((it: any)=>it.id).filter(Boolean)
  if (ids.length===0) return
  const locals = await prisma.smtp2goSubaccount.findMany({ where: { subaccountId: { in: ids } }, include: { tenant: true } })
  console.log('local matches', locals.length)
  console.dir(locals, { depth: 2 })
}

main().catch(e=>{console.error(e); process.exit(1)})
