"""Published-ad inventory, local edits and live reconciliation. 2026.11.10.8 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')
    if 'archived' not in columns:
        conn.execute('ALTER TABLE kufar_inventory ADD COLUMN archived INTEGER NOT NULL DEFAULT 0')
    if 'archived_at' not in columns:
        conn.execute('ALTER TABLE kufar_inventory ADD COLUMN archived_at TEXT')


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 import_archive(conn, rows, uid):
    """Append historical ads from a seller export as archive records.

    Rows are pre-normalised dicts (ad_id, title, body, price, published_at,
    end_date, code). Existing cards are never modified, so bindings and local
    edits of the current catalogue survive the import untouched.
    """
    schema(conn)
    added=skipped=0
    for row in rows:
        ad_id=str(row.get('ad_id',''))
        if not ad_id.isdigit() or not (row.get('title') or '').strip():
            raise kufar.Error('В выгрузке есть объявление без номера или названия.')
        if conn.execute('SELECT 1 FROM kufar_inventory WHERE ad_id=?',(ad_id,)).fetchone():
            skipped+=1;continue
        try:
            price=normalize_price(row.get('price'))
        except kufar.Error:
            price=(row.get('price') or '').strip()
        payload=dict(ad_id=ad_id,title=row['title'].strip(),body=row.get('body','').strip(),
                     price=price,category='',photos=[],hosted_photos=[],
                     included=False,reason='',region=row.get('region',''),
                     published_at=row.get('published_at','') or '',archive_source=row.get('source',''))
        conn.execute("INSERT INTO kufar_inventory(ad_id,payload,product_id,archived,archived_at,public_status) VALUES(?,?,?,?,?,?)",
                     (ad_id,json.dumps(payload,ensure_ascii=False),row.get('code','') or '',1,
                      row.get('end_date') or None,'confirmed_removed'))
        added+=1
    kufar.audit(conn,uid,'inventory-archive-import',f'Добавлено в архив {added}; уже было в базе {skipped}')
    return dict(added=added,skipped=skipped)


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'))
        row['archived']=bool(row.get('archived'))
        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.get('title','')),body=(live or {}).get('body',payload.get('body','')),
                    price=(live or {}).get('price',payload.get('price','')),
                    photos=(live or {}).get('photos') or payload.get('hosted_photos') or payload.get('photos',[]))
        row.update(draft or remote)
        row.update(category=payload.get('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'] and not r['archived'])
    for row in result:
        row['duplicate']=bool(not row['archived'] and 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('Привязка уже изменена. Обновите страницу.')
    linked=conn.execute('UPDATE listing SET product_id=?,version=version+1 WHERE unid=?',
                        (product_id,'KF'+str(ad_id)))
    if linked.rowcount:
        conn.execute('UPDATE state SET revision=revision+1 WHERE id=1')
    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('Карточка уже изменена. Обновите страницу.')
    feed_draft=dict(subject=normalized['subject'],body=normalized['body'],price=normalized['price'],photos=normalized['photos'])
    linked=conn.execute('UPDATE listing SET draft=?,version=version+1 WHERE unid=?',
                        (json.dumps(feed_draft,ensure_ascii=False),'KF'+str(ad_id)))
    if linked.rowcount:
        conn.execute('UPDATE state SET revision=revision+1 WHERE id=1')
    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('Статус карточки уже изменён. Обновите страницу.')
    linked=conn.execute('UPDATE listing SET removed=?,version=version+1 WHERE unid=?',
                        (int(bool(removed)),'KF'+str(ad_id)))
    if linked.rowcount:
        conn.execute('UPDATE state SET revision=revision+1 WHERE id=1')
    detail='локально исключено из следующего полного файла' if removed else 'возвращено в следующий полный файл'
    kufar.audit(conn,uid,'inventory-remove-local',str(ad_id)+': '+detail)
    return ('Товар снят локально: он исключён из следующего полного файла. После обновления профиля подтвердите, что объявления уже нет на Kufar.'
            if removed else 'Локальное снятие отменено: товар снова включён в следующий полный файл.')


def archive_card(conn,ad_id,version,uid,archive):
    """Move a catalogue card to/from the archive with feed mirroring.

    Archive hides the card from the working file immediately; the record is
    dropped from the FTP file at the next publish. Restoring brings back the
    last published record so the card reappears in the file. Inventory
    history is never deleted.
    """
    schema(conn)
    current=conn.execute('SELECT product_id FROM kufar_inventory WHERE ad_id=?',(str(ad_id),)).fetchone()
    if not current:
        raise kufar.Error('Карточка не найдена в каталоге.')
    cur=conn.execute('UPDATE kufar_inventory SET archived=?,archived_at=?,local_removed=?,version=version+1 WHERE ad_id=? AND version=?',
                     (1 if archive else 0,kufar.now() if archive else None,1 if archive else 0,str(ad_id),version))
    if cur.rowcount!=1:
        raise kufar.Error('Карточка уже изменена. Обновите страницу.')
    unid='KF'+str(ad_id)
    changed=False
    listing=conn.execute('SELECT removed FROM listing WHERE unid=?',(unid,)).fetchone()
    if archive:
        if listing and not listing['removed']:
            conn.execute('UPDATE listing SET removed=1,draft=NULL,version=version+1 WHERE unid=?',(unid,))
            changed=True
        detail='перемещено в архив'
    else:
        if listing and listing['removed']:
            conn.execute('UPDATE listing SET removed=0,version=version+1 WHERE unid=?',(unid,))
            changed=True
        elif not listing:
            record=conn.execute('SELECT record_xml FROM archive WHERE unid=? ORDER BY at DESC LIMIT 1',(unid,)).fetchone()
            if record:
                conn.execute('INSERT INTO listing(unid,product_id,source_xml) VALUES(?,?,?)',
                             (unid,current['product_id'] or '',record['record_xml']))
                changed=True
        detail='возвращено из архива'
    if changed:
        conn.execute('UPDATE state SET revision=revision+1 WHERE id=1')
    kufar.audit(conn,uid,'inventory-archive-card',str(ad_id)+': '+detail)
    return dict(changed=changed,detail=detail)


def feed_stock_review(feed_rows,records,products):
    """Compare the working feed file with the current discount stock slice.

    Read-only preview of what the actualize step would change: prices for
    bound products, cards whose product left the active stock, unbound cards.
    """
    price_changes=[];out_of_stock=[];unbound=0;actual=0
    for row in feed_rows:
        if not row['product_id']:
            unbound+=1;continue
        draft=json.loads(row['draft']) if row['draft'] else kufar.fields(records[row['unid']])
        product=products.get(row['product_id'])
        if not product or not product.get('is_active') or product.get('site_price') is None:
            out_of_stock.append(dict(unid=row['unid'],product_id=row['product_id'],
                                     subject=draft['subject'][:90],removed=bool(row['removed'])))
            continue
        price=format(Decimal(str(product['site_price'])),'.2f')
        if draft['price']!=price:
            price_changes.append(dict(unid=row['unid'],product_id=row['product_id'],
                                      subject=draft['subject'][:90],old=draft['price'],new=price))
        else:
            actual+=1
    return dict(price_changes=price_changes,out_of_stock=out_of_stock,unbound=unbound,actual=actual)


def reconcile(rows,products):
    labels={'present':'Есть в уценке','missing':'Нет в последнем срезе','unknown':'Код не найден в уценке','no_code':'Не указан БК/расп','archived':'Архив — снят с продажи'}
    for row in rows:
        if row.get('archived'):
            row['stock']='archived'
            row['product']=products.get(row['product_id']) if row['product_id'] else None
        else:
            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['product']=product
        row['stock_label']=labels[row['stock']]
    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'])
        if row['live']:
            row['live_status']='current'
        elif row.get('archived') or (live_scan and row['public_status']=='confirmed_removed'):
            row['live_status']='confirmed_removed'
        elif live_scan:
            row['live_status']='not_on_profile'
        else:
            row['live_status']='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 accept_new_ads(conn,ad_ids,uid):
    """Store profile-scan proposals as catalogue cards; the feed is not touched.

    An empty ad_ids list means every current proposal. Rows already present in
    the base are skipped, so repeated acceptance stays harmless.
    """
    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']:row for row in proposals}
    wanted=list(dict.fromkeys(str(ad_id).strip() for ad_id in ad_ids if str(ad_id).strip())) if ad_ids else list(allowed)
    targets=[ad_id for ad_id in wanted if ad_id in allowed]
    if not targets:
        raise kufar.Error('Нет новых объявлений для добавления. Обновите сверку профиля.')
    stamp=kufar.now()
    added=0
    for ad_id in targets:
        if conn.execute('SELECT 1 FROM kufar_inventory WHERE ad_id=?',(ad_id,)).fetchone():
            continue
        item=allowed[ad_id]
        payload=dict(ad_id=ad_id,title=item['title'],body='',price=item.get('price',''),
                     category=str(item.get('category','') or ''),
                     photos=[item['image']] if item.get('image') else [],
                     hosted_photos=[],included=False,reason='',
                     published_at=item.get('published_at',''),region='',
                     profile_url=live_scan['profile_url'])
        conn.execute("INSERT INTO kufar_inventory(ad_id,payload,product_id,checked_at,public_status) VALUES(?,?,?,?,'snapshot')",
                     (ad_id,json.dumps(payload,ensure_ascii=False),infer_product_id(item['title']),stamp))
        added+=1
    kufar.audit(conn,uid,'inventory-accept-new',f'Добавлено в каталог из сверки профиля: {added}')
    return added


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=[];stock_excluded=[];mapping=[];authoritative=0;review=0;excluded=0
    for row in inventory:
        if row['ad_id'] not in active_ids or row['archived']:
            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 row['product_id'] and (not product or not product.get('is_active') or product.get('site_price') is None):
            stock_excluded.append(dict(ad_id=row['ad_id'],product_id=row['product_id'],title=row['title'],
                                       reason='нет в текущем активном остатке уценки'))
            continue
        if product:
            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 not row['archived'] 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),
                stock_excluded=len(stock_excluded),stock_excluded_rows=stock_excluded,
                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}; нет в остатке {len(stock_excluded)}; локально снято {excluded}")
    return report
