"""Published-ad inventory, local edits and live reconciliation. 2026.11.10.5 MSK."""
import json
import re
import time
import xml.etree.ElementTree as ET
from collections import Counter
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime
from decimal import Decimal, InvalidOperation
from pathlib import Path
from urllib.parse import urlencode, urlsplit, urlunsplit

import httpx

from . import kufar


DEFAULT_PROFILE_URL = 'https://www.kufar.by/user/O1fdDwGUaN-_GZNqxfb3wng'


def schema(conn):
    conn.execute("CREATE TABLE IF NOT EXISTS kufar_inventory (ad_id TEXT PRIMARY KEY, payload TEXT NOT NULL, product_id TEXT NOT NULL DEFAULT '', version INTEGER NOT NULL DEFAULT 0, checked_at TEXT, public_status TEXT NOT NULL DEFAULT 'snapshot', public_payload TEXT, compared_at TEXT)")
    conn.execute("CREATE TABLE IF NOT EXISTS kufar_inventory_source (id INTEGER PRIMARY KEY CHECK(id=1), source_name TEXT NOT NULL, source_at TEXT NOT NULL, imported_at TEXT NOT NULL, source_hash TEXT NOT NULL)")
    conn.execute("CREATE TABLE IF NOT EXISTS kufar_live_scan (id INTEGER PRIMARY KEY CHECK(id=1), profile_url TEXT NOT NULL, scanned_at TEXT NOT NULL, total INTEGER NOT NULL, payload TEXT NOT NULL)")
    columns={row['name'] for row in conn.execute('PRAGMA table_info(kufar_inventory)')}
    if 'local_draft' not in columns:
        conn.execute('ALTER TABLE kufar_inventory ADD COLUMN local_draft TEXT')
    if 'local_removed' not in columns:
        conn.execute('ALTER TABLE kufar_inventory ADD COLUMN local_removed INTEGER NOT NULL DEFAULT 0')


def import_snapshot(conn, path, uid):
    schema(conn)
    if conn.execute("SELECT 1 FROM kufar_inventory_source WHERE id=1").fetchone():
        return
    path=Path(path)
    if not path.exists():
        return
    data=path.read_bytes();rows=json.loads(data)
    ids=set()
    for row in rows:
        ad_id=str(row.get('ad_id',''))
        if not ad_id.isdigit() or ad_id in ids or not row.get('body') or not row.get('title'):
            raise kufar.Error('Неполные или повторяющиеся объявления в исходном снимке.')
        ids.add(ad_id)
    for row in rows:
        conn.execute("INSERT INTO kufar_inventory(ad_id,payload,product_id) VALUES(?,?,?)", (str(row['ad_id']),json.dumps(row,ensure_ascii=False),row.get('code','')))
    # The snapshot is provenance, not a live account publication check.
    source_at=datetime.fromtimestamp(path.stat().st_mtime).astimezone().isoformat()
    conn.execute("INSERT INTO kufar_inventory_source VALUES(1,?,?,?,?)", ('Куфар — база объявлений.xlsx',source_at,kufar.now(),kufar.digest(data)))
    kufar.audit(conn,uid,'inventory-import',f'Загружено {len(rows)} объявлений из снимка; файл импорта не изменён')


def load(conn):
    schema(conn)
    source=conn.execute("SELECT * FROM kufar_inventory_source WHERE id=1").fetchone()
    result=[]
    for record in conn.execute("SELECT * FROM kufar_inventory ORDER BY ad_id DESC"):
        row=dict(record);payload=json.loads(row.pop('payload'))
        live=json.loads(row.pop('public_payload')) if row['public_payload'] else None
        draft_raw=row.pop('local_draft')
        draft=json.loads(draft_raw) if draft_raw else None
        remote=dict(title=(live or {}).get('title',payload['title']),body=(live or {}).get('body',payload['body']),
                    price=(live or {}).get('price',payload['price']),
                    photos=(live or {}).get('photos') or payload.get('hosted_photos') or payload.get('photos',[]))
        row.update(draft or remote)
        row.update(category=payload['category'],
                   included=payload.get('included',False),reason=payload.get('reason',''),url='https://www.kufar.by/item/'+row['ad_id'])
        row['local_changed']=bool(draft)
        result.append(row)
    counts=Counter(r['product_id'] for r in result if r['product_id'])
    for row in result:
        row['duplicate']=bool(row['product_id'] and counts[row['product_id']]>1)
    return dict(source) if source else None,result


def bind(conn,ad_id,version,product_id,uid):
    product_id=product_id.strip()
    if product_id:
        kufar.import_unid(product_id)
        first,kind,last=product_id.split('-')
        product_id=first+'-'+('БК' if kind.casefold()=='бк' else 'расп')+'-'+last
    cur=conn.execute("UPDATE kufar_inventory SET product_id=?,version=version+1,compared_at=NULL WHERE ad_id=? AND version=?", (product_id,ad_id,version))
    if cur.rowcount!=1:
        raise kufar.Error('Привязка уже изменена. Обновите страницу.')
    kufar.audit(conn,uid,'inventory-bind',ad_id+': привязка '+(product_id or 'очищена'))


def save_local(conn,ad_id,version,title,body,price,photos,uid):
    """Save feed-candidate fields without requiring a discount-product binding."""
    schema(conn)
    normalized=kufar.validate(dict(subject=title.strip(),body=body.strip(),
                                    price=str(price).strip().replace(',','.'),
                                    photos=list(dict.fromkeys(str(value).strip() for value in photos if str(value).strip()))))
    draft=dict(title=normalized['subject'],body=normalized['body'],price=normalized['price'],photos=normalized['photos'])
    cur=conn.execute('UPDATE kufar_inventory SET local_draft=?,version=version+1 WHERE ad_id=? AND version=?',
                     (json.dumps(draft,ensure_ascii=False),str(ad_id),version))
    if cur.rowcount!=1:
        raise kufar.Error('Карточка уже изменена. Обновите страницу.')
    kufar.audit(conn,uid,'inventory-edit',str(ad_id)+': локальная карточка сохранена')


def set_local_removed(conn,ad_id,version,removed,uid):
    """Include/exclude an inventory ad from the next complete feed candidate."""
    schema(conn)
    cur=conn.execute('UPDATE kufar_inventory SET local_removed=?,version=version+1 WHERE ad_id=? AND version=?',
                     (int(bool(removed)),str(ad_id),version))
    if cur.rowcount!=1:
        raise kufar.Error('Статус карточки уже изменён. Обновите страницу.')
    detail='локально исключено из следующего полного файла' if removed else 'возвращено в следующий полный файл'
    kufar.audit(conn,uid,'inventory-remove-local',str(ad_id)+': '+detail)
    return ('Товар снят локально: он исключён из следующего полного файла. После обновления профиля подтвердите, что объявления уже нет на Kufar.'
            if removed else 'Локальное снятие отменено: товар снова включён в следующий полный файл.')


def reconcile(rows,products):
    labels={'present':'Есть в уценке','missing':'Нет в последнем срезе','unknown':'Код не найден в уценке','no_code':'Не указан БК/расп'}
    for row in rows:
        product=products.get(row['product_id'])
        row['stock']='no_code' if not row['product_id'] else 'present' if product and product['is_active'] else 'missing' if product else 'unknown'
        row['stock_label']=labels[row['stock']]
        row['product']=product
    return rows


def normalize_price(value):
    """Return a two-decimal BYN value from Kufar's rendered price."""
    match=re.search(r'-?\d[\d\s\xa0]*(?:[.,]\d+)?',str(value or ''))
    raw=match.group(0).replace('\xa0','').replace(' ','') if match else ''
    if ',' in raw and '.' in raw:
        raw=raw.replace(',','')
    else:
        raw=raw.replace(',','.')
    try:
        price=Decimal(raw)
    except (InvalidOperation,ValueError):
        raise kufar.Error('Kufar вернул объявление без распознаваемой цены.')
    if not price.is_finite() or price < 0:
        raise kufar.Error('Kufar вернул некорректную цену объявления.')
    return format(price,'.2f')


def _profile_page(text):
    match=re.search(r'<script[^>]*id="__NEXT_DATA__"[^>]*>(.*?)</script>',text,re.S)
    if not match:
        raise kufar.Error('Не удалось прочитать список продавца на Kufar. Предыдущая сверка сохранена.')
    try:
        listing=json.loads(match[1])['props']['initialState']['listing']
        elements=listing['elements'];total=int(listing['total'])
    except (ValueError,KeyError,TypeError):
        raise kufar.Error('Структура списка продавца Kufar изменилась. Предыдущая сверка сохранена.')
    rows=[]
    for ad in elements:
        ad_id=str(ad.get('adId',''))
        if not ad_id.isdigit() or not ad.get('title'):
            raise kufar.Error('Kufar вернул неполную карточку объявления. Предыдущая сверка сохранена.')
        rows.append(dict(ad_id=ad_id,title=ad['title'].strip(),price=normalize_price(ad.get('price')),
                         category=ad.get('category',''),published_at=ad.get('date',''),
                         url=ad.get('adViewLink') or 'https://www.kufar.by/item/'+ad_id,
                         image=ad.get('image','')))
    next_token=next((p.get('token') for p in listing.get('pagination',[]) if p.get('label')=='next'),None)
    return rows,total,next_token


def scan_profile(profile_url=DEFAULT_PROFILE_URL,client=None):
    """Read every current seller page. No inventory or feed records are changed."""
    parsed=urlsplit((profile_url or '').strip())
    if parsed.scheme!='https' or parsed.hostname not in {'kufar.by','www.kufar.by'} or not re.fullmatch(r'/user/[A-Za-z0-9_-]+',parsed.path.rstrip('/')):
        raise kufar.Error('Укажите публичную ссылку на профиль продавца Kufar.')
    base=urlunsplit(('https','www.kufar.by',parsed.path.rstrip('/'),'',''))
    own=client is None
    session=client or httpx.Client(timeout=30,follow_redirects=True,headers={'User-Agent':'Mozilla/5.0'})
    url=base+'?'+urlencode({'sort':'lst.d'});rows=[];expected=None
    try:
        for _ in range(20):
            response=session.get(url)
            if response.status_code==429:
                raise kufar.Error('Kufar ограничил частоту сверки. Повторите позже; предыдущие данные сохранены.')
            response.raise_for_status()
            page,total,next_token=_profile_page(response.text)
            expected=total if expected is None else expected
            for row in page:
                if not any(old['ad_id']==row['ad_id'] for old in rows):
                    rows.append(row)
            if not next_token:
                break
            url=base+'?'+urlencode({'cmp':'1','cursor':next_token,'sort':'lst.d'})
        if expected is None or len(rows)!=expected:
            raise kufar.Error(f'Kufar сообщает {expected or 0} объявлений, прочитано {len(rows)}. Сверка не сохранена.')
        return base,rows
    finally:
        if own:
            session.close()


def store_live_scan(conn,profile_url,rows,uid):
    schema(conn)
    stamp=kufar.now()
    conn.execute("INSERT INTO kufar_live_scan(id,profile_url,scanned_at,total,payload) VALUES(1,?,?,?,?) ON CONFLICT(id) DO UPDATE SET profile_url=excluded.profile_url,scanned_at=excluded.scanned_at,total=excluded.total,payload=excluded.payload",
                 (profile_url,stamp,len(rows),json.dumps(rows,ensure_ascii=False)))
    state=kufar.snapshot(conn)
    feed_count=len(kufar.parse(state['baseline'])[1]) if state['baseline'] else 0
    manual_count=max(0,len(rows)-feed_count)
    conn.execute("UPDATE state SET manual_count=?,manual_checked_at=?,revision=revision+1 WHERE id=1",
                 (manual_count,stamp))
    conn.executemany("UPDATE kufar_inventory SET public_status='snapshot',version=version+1 WHERE ad_id=? AND public_status='confirmed_removed'",
                     [(row['ad_id'],) for row in rows])
    kufar.audit(conn,uid,'inventory-profile-scan',f'Прочитано {len(rows)} объявлений; база и XML не изменялись')
    return stamp


def load_live_scan(conn):
    schema(conn)
    record=conn.execute('SELECT * FROM kufar_live_scan WHERE id=1').fetchone()
    if not record:
        return None
    result=dict(record);result['rows']=json.loads(result.pop('payload'))
    return result


def infer_product_id(text):
    match=re.search(r'(?<!\d)(\d+)-(БК|расп)-(\d+)(?!\d)',text or '',re.I)
    if not match:
        return ''
    return match[1]+'-'+('БК' if match[2].casefold()=='бк' else 'расп')+'-'+match[3]


def compare_live(rows,live_scan):
    live_rows=live_scan['rows'] if live_scan else []
    live_by_id={r['ad_id']:r for r in live_rows};base_by_id={r['ad_id']:r for r in rows}
    for row in rows:
        row['live']=live_by_id.get(row['ad_id'])
        row['live_status']=('current' if row['live'] else 'confirmed_removed'
                            if live_scan and row['public_status']=='confirmed_removed'
                            else 'not_on_profile' if live_scan else 'not_checked')
        row['live_price']=row['live']['price'] if row['live'] else None
    new=[]
    for live in live_rows:
        if live['ad_id'] not in base_by_id:
            item=dict(live);item['product_id']=infer_product_id(item['title']);new.append(item)
    removed=[r for r in rows if r['live_status']=='not_on_profile']
    return new,removed


def confirm_removed(conn,ad_ids,uid):
    """Acknowledge ads absent from the latest full profile scan without deleting history."""
    live_scan=load_live_scan(conn)
    if not live_scan:
        raise kufar.Error('Сначала выполните полную сверку публичного профиля Kufar.')
    _,rows=load(conn)
    _,proposals=compare_live(rows,live_scan)
    allowed={row['ad_id'] for row in proposals}
    targets=list(dict.fromkeys(str(ad_id) for ad_id in ad_ids if str(ad_id) in allowed))
    if not targets:
        raise kufar.Error('Нет выбранных рекомендаций для подтверждения.')
    stamp=kufar.now()
    conn.executemany("UPDATE kufar_inventory SET public_status='confirmed_removed',local_removed=1,checked_at=?,version=version+1 WHERE ad_id=?",
                     [(stamp,ad_id) for ad_id in targets])
    kufar.audit(conn,uid,'inventory-confirm-removed',f'Подтверждено снятие: {len(targets)}')
    return len(targets)


def live_product_links(conn):
    """Map exact discount product ids to ads confirmed by the latest profile scan."""
    live_scan=load_live_scan(conn)
    if not live_scan:
        return dict(scan=None,links={},matched_ads=0)
    _,rows=load(conn)
    live_by_id={r['ad_id']:r for r in live_scan['rows']}
    links={};matched=0
    for row in rows:
        live=live_by_id.get(row['ad_id'])
        if not live or not row['product_id']:
            continue
        links.setdefault(row['product_id'],[]).append(dict(
            ad_id=row['ad_id'],title=live['title'],price=live['price'],url=live['url']))
        matched+=1
    return dict(scan=live_scan,links=links,matched_ads=matched)


def price_check(kufar_price,site_price):
    if kufar_price in (None,'') or site_price is None:
        return dict(status='unknown',delta=None)
    try:
        current=Decimal(str(kufar_price));reference=Decimal(str(site_price))
    except (InvalidOperation,ValueError):
        return dict(status='unknown',delta=None)
    delta=current-reference
    return dict(status='same' if delta==0 else 'different',delta=format(delta,'.2f'))


def check_public(ad_id,client=None):
    if not str(ad_id).isdigit():
        raise kufar.Error('Некорректный номер объявления.')
    url='https://www.kufar.by/item/'+str(ad_id)
    response=(client.get(url) if client else httpx.get(url,timeout=30,follow_redirects=True,headers={'User-Agent':'Mozilla/5.0'}))
    if response.status_code in {404,410}:
        return 'unavailable',None
    if response.status_code==429:
        raise kufar.Error('Kufar ограничил частоту проверок. Повторите позже; прошлые данные сохранены.')
    response.raise_for_status()
    match=re.search(r'<script[^>]*id="__NEXT_DATA__"[^>]*>(.*?)</script>',response.text,re.S)
    if not match:
        raise kufar.Error('Не удалось подтвердить публикацию по странице. Предыдущие данные сохранены.')
    try:
        ad=json.loads(match[1])['props']['initialState']['adView']['data']
    except (ValueError,KeyError,TypeError):
        raise kufar.Error('Структура страницы изменилась. Предыдущие данные сохранены.')
    if not ad or str(ad.get('adId'))!=str(ad_id):
        raise kufar.Error('Страница не подтверждает это объявление. Снятие не подтверждено.')
    price=ad.get('price','')
    calc=next((r for r in ad.get('calculator',[]) if r.get('currency')=='BYN'),None)
    if calc:
        from decimal import Decimal
        price=format(Decimal(str(calc['price']))/100,'.2f')
    return 'available',dict(title=ad.get('subject') or ad['title'],body=ad.get('body') or ad.get('description',''),price=price,photos=(ad.get('gallery') or {}).get('images') or (ad.get('images') or {}).get('gallery',[]))


def store_check(conn,ad_id,status,payload,uid):
    conn.execute("UPDATE kufar_inventory SET public_status=?,checked_at=?,public_payload=COALESCE(?,public_payload) WHERE ad_id=?", (status,kufar.now(),json.dumps(payload,ensure_ascii=False) if payload else None,ad_id))
    kufar.audit(conn,uid,'inventory-public-check',ad_id+': '+status)


def _feed_page(ad_id,url,client):
    response=client.get(url);response.raise_for_status()
    match=re.search(r'<script[^>]*id="__NEXT_DATA__"[^>]*>(.*?)</script>',response.text,re.S)
    if not match:
        raise kufar.Error('Не удалось прочитать категорию объявления №'+str(ad_id)+'.')
    try:
        ad=json.loads(match[1])['props']['initialState']['adView']['data']
        if str(ad.get('adId'))!=str(ad_id):
            raise KeyError('adId')
        return dict(ad_id=str(ad_id),category=str(ad['categoryId']),params=ad.get('adParams',{}),
                    type=ad.get('type') or 'sell',subject=ad.get('subject') or ad.get('title',''),body=ad.get('body') or '')
    except (ValueError,KeyError,TypeError) as exc:
        raise kufar.Error('Kufar вернул неполные данные категории объявления №'+str(ad_id)+'.') from exc


def _save_json(path,value):
    path.parent.mkdir(parents=True,exist_ok=True)
    temp=path.with_suffix(path.suffix+'.tmp')
    temp.write_text(json.dumps(value,ensure_ascii=False,indent=2),encoding='utf-8')
    temp.replace(path)


def build_full_candidate(conn,products,prepared_dir,out_dir,uid,client=None):
    """Build and validate one complete local feed candidate; never publish it."""
    schema(conn)
    live_scan=load_live_scan(conn)
    if not live_scan:
        raise kufar.Error('Сначала обновите полную сверку публичного профиля Kufar.')
    _,inventory=load(conn)
    compare_live(inventory,live_scan)
    active_ids={row['ad_id'] for row in live_scan['rows']}
    inventory_by_id={row['ad_id']:row for row in inventory}
    missing=[row for row in live_scan['rows'] if row['ad_id'] not in inventory_by_id]
    if missing:
        raise kufar.Error(f'В публичном профиле есть {len(missing)} объявлений, которых ещё нет в каталоге приложения. Сначала добавьте их в базу.')
    prepared_dir=Path(prepared_dir);pages_dir=prepared_dir/'pages';out_dir=Path(out_dir)
    pages={};to_fetch=[]
    for ad_id in active_ids:
        path=pages_dir/(ad_id+'.json')
        if path.exists():
            try:pages[ad_id]=json.loads(path.read_text(encoding='utf-8'))
            except (OSError,ValueError):to_fetch.append(ad_id)
        else:to_fetch.append(ad_id)
    own=client is None
    session=client or httpx.Client(timeout=35,follow_redirects=True,headers={'User-Agent':'Mozilla/5.0','Accept-Language':'ru'})
    try:
        def fetch(ad_id):
            live=next(row for row in live_scan['rows'] if row['ad_id']==ad_id)
            for attempt in range(4):
                try:
                    page=_feed_page(ad_id,live['url'],session)
                    time.sleep(2.5)
                    return ad_id,page
                except (httpx.HTTPError,kufar.Error) as exc:
                    if attempt==3:raise
                    limited=isinstance(exc,httpx.HTTPStatusError) and exc.response.status_code==429
                    time.sleep((30 if limited else 3)*(attempt+1))
        with ThreadPoolExecutor(max_workers=1) as pool:
            futures=[pool.submit(fetch,ad_id) for ad_id in to_fetch]
            for future in as_completed(futures):
                ad_id,page=future.result();pages[ad_id]=page;_save_json(pages_dir/(ad_id+'.json'),page)
    finally:
        if own:session.close()
    schema_path=prepared_dir/'parameter-schema.json'
    allowed=json.loads(schema_path.read_text(encoding='utf-8')) if schema_path.exists() else {}
    definitions=kufar.categories();root=ET.Element('uedb',{'revision':'5'});records=ET.SubElement(root,'records')
    blockers=[];warnings=[];mapping=[];authoritative=0;review=0;excluded=0
    for row in inventory:
        if row['ad_id'] not in active_ids:
            continue
        if row['local_removed']:
            excluded+=1;continue
        page=pages[row['ad_id']];category=str(page.get('category',''));definition=definitions.get(category)
        params={value.get('p'):value.get('v') for value in page.get('params',{}).values() if isinstance(value,dict) and value.get('p')}
        reason=''
        if not definition:
            reason='категория отсутствует в справочнике импорта'
        else:
            for field in definition['fields']:
                value=params.get(field['tag']);permitted={str(option[0]) for option in field['options']}
                if value is None or (permitted and str(value) not in permitted):
                    reason='не заполнено обязательное поле: '+field['label'];break
        photos=row['photos']
        try:
            draft=kufar.validate(dict(subject=row['title'],body=row['body'],price=row['price'],photos=photos))
        except kufar.Error as exc:
            reason=str(exc);draft=None
        if str(params.get('region'))!='7' or str(params.get('area')) not in {str(value) for value in range(22,31)}:
            reason=reason or 'не указан район Минска'
        if reason:
            blockers.append(dict(ad_id=row['ad_id'],title=row['title'],reason=reason));continue
        product=products.get(row['product_id']) if row['product_id'] else None
        price=draft['price']
        if product and product.get('is_active') and product.get('site_price') is not None:
            price=format(Decimal(str(product['site_price'])),'.2f');authoritative+=1
        else:
            review+=1;warnings.append(dict(ad_id=row['ad_id'],product_id=row['product_id'],reason='цена сохранена с Kufar: нет активной сопоставленной позиции'))
        record=ET.SubElement(records,'record')
        general=dict(unid='KF'+row['ad_id'],region='7',area=str(params['area']),remuneration_type=str(params.get('remuneration_type','1')),
                     subject=draft['subject'],body=draft['body'],category=category,type='sell',phone='+375445012104',
                     price=price,currency='BYN',contact_person='ООО "Триовист"')
        for tag in allowed.get(category,[]):
            value=params.get(tag)
            if value is not None and not isinstance(value,bool) and tag not in general:
                general[tag]=value
        for tag,value in general.items():
            for scalar in value if isinstance(value,list) else [value]:
                if scalar not in (None,''):
                    ET.SubElement(record,tag).text=str(scalar)
        gallery=ET.SubElement(record,'photos')
        for photo in photos:ET.SubElement(gallery,'photo',{'picture':photo})
        mapping.append(dict(unid='KF'+row['ad_id'],ad_id=row['ad_id'],product_id=row['product_id'],category=category,price=price))
    state=kufar.snapshot(conn)
    active_products={row['product_id'] for row in inventory if row['ad_id'] in active_ids and not row['local_removed'] and row['product_id']}
    if state['baseline']:
        _,baseline_records=kufar.parse(state['baseline'])
        for listing in state['rows']:
            if listing['removed'] or listing['product_id'] in active_products:
                continue
            node=ET.fromstring(listing['source_xml']) if listing['source_xml'] else baseline_records.get(listing['unid'])
            if node is None:
                blockers.append(dict(ad_id='',title=listing['unid'],reason='не найдена XML-карточка рабочего файла'));continue
            records.append(ET.fromstring(ET.tostring(node,encoding='utf-8')))
            mapping.append(dict(unid=listing['unid'],ad_id='',product_id=listing['product_id'],category=node.findtext('category',''),price=node.findtext('price','')))
    report=dict(created_at=kufar.now(),active_profile=len(active_ids),local_removed=excluded,records=len(mapping),
                authoritative_prices=authoritative,prices_for_review=review,category_blockers=len(blockers),
                blockers=blockers,warnings=warnings,mapping=mapping,published=False)
    out_dir.mkdir(parents=True,exist_ok=True);_save_json(out_dir/'report.json',report)
    if blockers:
        raise kufar.Error(f'Полный файл не собран: {len(blockers)} карточек требуют исправления категории или обязательных полей.')
    xml=ET.tostring(root,encoding='utf-8',xml_declaration=True)
    if len(kufar.parse(xml)[1])!=len(mapping) or len(mapping)>kufar.LIMIT:
        raise kufar.Error('Проверка целостности полного файла не пройдена.')
    (out_dir/'kufar-full-candidate.xml').write_bytes(xml)
    report['sha256']=kufar.digest(xml);_save_json(out_dir/'report.json',report)
    kufar.audit(conn,uid,'full-candidate',f"Собран полный кандидат: {len(mapping)}; актуальных цен {authoritative}; проверить цен {review}; локально снято {excluded}")
    return report
