#!/usr/bin/env python3
"""
Ruleaza ANAF API LOCAL (de pe calculatorul tau, nu de pe server).
ANAF blocheaza IP-urile de hosting — de aceea trebuie rulat local.

Utilizare:
  python3 anaf_local.py --limit 50000 --output contacte_anaf.csv

Dupa export:
  1. Incarca fisierul CSV in platforma via butonul "Import CSV"
  2. Apasa "Emailuri web" pentru a scana site-urile gasite

Sau:
  python3 anaf_local.py --cui 401536,1589850,11447921
"""

import asyncio
import csv
import argparse
import httpx
from datetime import datetime
from pathlib import Path

ANAF_URL   = 'https://webservicesp.anaf.ro/api/PlatitorTvaRest/v9/tva'
BATCH_SIZE = 100
DELAY      = 1.1


def today():
    return datetime.now().strftime('%Y-%m-%d')


def clean_phone(raw: str) -> str:
    import re
    digits = re.sub(r'\D', '', raw or '')
    if not digits:
        return ''
    if digits.startswith('40') and len(digits) == 11:
        return '+' + digits
    if digits.startswith('0') and len(digits) >= 9:
        return '+4' + digits
    return digits if len(digits) >= 9 else ''


async def fetch_batch(client: httpx.AsyncClient, cuis: list[str]) -> list[dict]:
    payload = [{'cui': int(c), 'data': today()} for c in cuis if c.isdigit()]
    if not payload:
        return []
    try:
        r = await client.post(
            ANAF_URL,
            json=payload,
            headers={
                'Content-Type': 'application/json',
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
                'Origin': 'https://www.anaf.ro',
                'Referer': 'https://www.anaf.ro/',
            },
            timeout=30,
        )
        if r.status_code != 200:
            print(f'  HTTP {r.status_code} — skip')
            return []
        data = r.json()
        results = []
        for item in data.get('found', []):
            dg = item.get('date_generale', {})
            ad = item.get('adresa_sediu_social', {})
            cui = str(dg.get('cui', ''))
            tel = clean_phone(dg.get('telefon', ''))
            results.append({
                'cui':        cui,
                'nume':       (dg.get('denumire') or '').strip(),
                'adresa':     (dg.get('adresa') or '').strip(),
                'judet':      (ad.get('sdenumire_Judet') or '').strip(),
                'localitate': (ad.get('sdenumire_Localitate') or '').strip(),
                'cod_caen':   str(dg.get('cod_CAEN') or '').strip(),
                'nr_reg_com': (dg.get('nrRegCom') or '').strip(),
                'telefon':    tel,
                'stare':      (dg.get('stare_inregistrare') or '').strip(),
            })
        return results
    except Exception as exc:
        print(f'  Eroare: {exc}')
        return []


async def run(cuis: list[str], output: str):
    total = len(cuis)
    batches = [cuis[i:i+BATCH_SIZE] for i in range(0, total, BATCH_SIZE)]
    print(f'Interoghez {total} CUI-uri ({len(batches)} batch-uri) → {output}')

    salvate = 0
    path = Path(output)

    with open(path, 'w', newline='', encoding='utf-8-sig') as f:
        writer = csv.DictWriter(f, fieldnames=[
            'cui', 'nume', 'adresa', 'judet', 'localitate',
            'cod_caen', 'nr_reg_com', 'telefon', 'stare',
        ])
        writer.writeheader()

        async with httpx.AsyncClient() as client:
            for i, batch in enumerate(batches):
                results = await fetch_batch(client, batch)
                for r in results:
                    writer.writerow(r)
                    if r['telefon']:
                        salvate += 1
                        print(f'  + {r["nume"][:40]} | {r["telefon"]} | {r["judet"]}')

                processed = min((i + 1) * BATCH_SIZE, total)
                print(f'  [{processed}/{total}] {salvate} telefoane gasite')

                if i < len(batches) - 1:
                    await asyncio.sleep(DELAY)

    print(f'\nGata! {salvate}/{total} firme cu telefon → {path}')
    return salvate


def load_cuis_from_anaf_csv(csv_path: str, limit: int) -> list[str]:
    """Citeste CUI-urile din fisierul CSV ANAF descarcat."""
    import zipfile, io
    p = Path(csv_path)
    cuis = []

    if p.suffix == '.zip':
        with zipfile.ZipFile(p) as zf:
            name = next(f for f in zf.namelist() if f.endswith(('.csv', '.txt')))
            with zf.open(name) as raw:
                import io as _io
                text = _io.TextIOWrapper(raw, encoding='windows-1250', errors='replace')
                reader = csv.DictReader(text)
                for row in reader:
                    cui = (row.get('CUI') or row.get('cui') or '').strip()
                    if cui.isdigit():
                        cuis.append(cui)
                    if limit and len(cuis) >= limit:
                        break
    else:
        with open(p, encoding='utf-8-sig') as f:
            reader = csv.DictReader(f)
            for row in reader:
                cui = (row.get('CUI') or row.get('cui') or '').strip()
                if cui.isdigit():
                    cuis.append(cui)
                if limit and len(cuis) >= limit:
                    break
    return cuis


def main():
    parser = argparse.ArgumentParser(description='ANAF API - colectare telefoane firme RO')
    parser.add_argument('--cui',    help='CUI-uri separate prin virgula (ex: 401536,1589850)')
    parser.add_argument('--anaf',   help='Calea catre fisierul ZIP ANAF descarcat')
    parser.add_argument('--limit',  type=int, default=10000, help='Nr maxim CUI-uri')
    parser.add_argument('--output', default='contacte_anaf.csv', help='Fisier CSV output')
    args = parser.parse_args()

    if args.cui:
        cuis = [c.strip() for c in args.cui.split(',')]
    elif args.anaf:
        print(f'Citesc CUI-uri din {args.anaf}...')
        cuis = load_cuis_from_anaf_csv(args.anaf, args.limit)
        print(f'  {len(cuis)} CUI-uri gasite')
    else:
        # Demo cu 10 firme cunoscute
        cuis = ['401536', '1589850', '11447921', '2205882', '433082',
                '14199222', '24932975', '24736210', '16038516', '12760557']
        print(f'Demo cu {len(cuis)} CUI-uri...')

    asyncio.run(run(cuis[:args.limit], args.output))


if __name__ == '__main__':
    main()