"""Published snapshot remains separate from the import feed; 2026.11.04.5."""
import json
import xml.etree.ElementTree as ET

import httpx
import pytest

from timesheet import kufar as k,kufarstock as s


class FeedRemote:
    """In-memory FTP stand-in that keeps the last published feed."""
    data=b'';uploads=0
    def __init__(self,config):pass
    def __enter__(self):return self
    def __exit__(self,*args):pass
    def read(self,path):return type(self).data
    def publish(self,path,data,baseline):type(self).data=data;type(self).uploads+=1


BASE_FEED=(b'<?xml version="1.0"?><uedb><records>'
           b'<record><unid>old-1</unid><subject>Old title</subject><body>Long enough description for an existing ad.</body>'
           b'<price>15.00</price><photos><photo picture="https://example.com/one.jpg"/></photos></record>'
           b'</records></uedb>')


@pytest.fixture
def inventory(tmp_path):
    rows=[dict(ad_id=str(i),title='Product '+str(i),body='Actual goods and defect description.',price='99.00',code=code,category='Ноутбуки',included=(i==1),reason='',hosted_photos=['https://example.com/photo.jpg']) for i,code in [(1,'123-расп-456'),(2,'123-расп-456'),(3,''),(4,'777-БК-999')]]
    path=tmp_path/'snapshot.json';path.write_text(json.dumps(rows),encoding='utf-8')
    with k.connect(tmp_path) as c:s.import_snapshot(c,path,1)
    return tmp_path,path


def test_all_ads_imported_without_modifying_feed(inventory):
    root,path=inventory
    with k.connect(root) as c:
        source,rows=s.load(c)
        assert len(rows)==4 and sum(r['duplicate'] for r in rows)==2
        assert sum(r['included'] for r in rows)==1
        assert k.snapshot(c)['baseline'] is None and not k.snapshot(c)['rows']


def test_binding_survives_reopening_snapshot(inventory):
    root,path=inventory
    with k.connect(root) as c:
        s.bind(c,'3',0,'888-РАСП-999',1)
        s.import_snapshot(c,path,1)
        rows=s.load(c)[1]
        assert next(r for r in rows if r['ad_id']=='3')['product_id']=='888-расп-999'
        with pytest.raises(k.Error):s.bind(c,'3',0,'111-БК-222',1)


def test_unbound_inventory_card_can_be_edited_and_removed_locally(inventory):
    root,_=inventory
    with k.connect(root) as c:
        row=next(r for r in s.load(c)[1] if r['ad_id']=='3')
        assert not row['product_id']
        s.save_local(c,'3',row['version'],'Edited title','Edited description long enough.','123,45',
                     ['https://example.com/main.jpg','https://example.com/second.jpg'],1)
        row=next(r for r in s.load(c)[1] if r['ad_id']=='3')
        assert row['title']=='Edited title' and row['price']=='123.45'
        assert row['photos'][0].endswith('main.jpg') and not row['product_id']
        message=s.set_local_removed(c,'3',row['version'],True,1)
        row=next(r for r in s.load(c)[1] if r['ad_id']=='3')
        assert row['local_removed']==1 and 'исключён' in message


def test_full_candidate_uses_local_removal_and_current_product_price(inventory,tmp_path):
    root,_=inventory
    prepared=tmp_path/'prepared';pages=prepared/'pages';pages.mkdir(parents=True)
    (prepared/'parameter-schema.json').write_text(json.dumps({'15020':['condition','large_appliance_type','shop_guarantee']}),encoding='utf-8')
    page=dict(category='15020',params={
        'region':dict(p='region',v=7),'area':dict(p='area',v='22'),
        'condition':dict(p='condition',v='1'),
        'type':dict(p='large_appliance_type',v='20'),
        'guarantee':dict(p='shop_guarantee',v='1 месяц'),
    })
    for ad_id in ['1','3']:
        (pages/(ad_id+'.json')).write_text(json.dumps(dict(page,ad_id=ad_id)),encoding='utf-8')
    live=[dict(ad_id='1',title='Product 1',price='90.00',url='https://www.kufar.by/item/1'),
          dict(ad_id='3',title='Product 3',price='80.00',url='https://www.kufar.by/item/3')]
    with k.connect(root) as c:
        s.store_live_scan(c,s.DEFAULT_PROFILE_URL,live,1)
        row=next(r for r in s.load(c)[1] if r['ad_id']=='3')
        s.set_local_removed(c,'3',row['version'],True,1)
        report=s.build_full_candidate(c,{'123-расп-456':dict(is_active=True,site_price='77.25')},prepared,tmp_path/'candidate',1)
    assert report['records']==1 and report['authoritative_prices']==1 and report['local_removed']==1
    _,records=k.parse((tmp_path/'candidate'/'kufar-full-candidate.xml').read_bytes())
    assert records['KF1'].findtext('price')=='77.25'


def test_exact_full_code_only(inventory):
    root,path=inventory
    with k.connect(root) as c:rows=s.load(c)[1]
    products={'123-расп-456':dict(is_active=True),'777-БК-999':dict(is_active=False),'888-расп-999':dict(is_active=True)}
    rows=s.reconcile(rows,products)
    assert [r['stock'] for r in sorted(rows,key=lambda r:r['ad_id'])]==['present','present','no_code','missing']
    products={'123':dict(is_active=True)}
    assert s.reconcile(rows,products)[-1]['stock']=='unknown'


class Client:
    def __init__(self,status=200,ad=None,text=None):self.status=status;self.ad=ad;self.text=text
    def get(self,url):
        text=self.text or '<script id="__NEXT_DATA__" type="application/json">'+json.dumps({'props':{'initialState':{'adView':{'data':self.ad}}}})+'</script>'
        return httpx.Response(self.status,text=text,request=httpx.Request('GET',url))


def profile_html(elements,total,next_token=None):
    pagination=[dict(label='self',num=1,token=None)]
    if next_token:pagination.append(dict(label='next',num=2,token=next_token))
    data={'props':{'initialState':{'listing':{'elements':elements,'total':total,'pagination':pagination}}}}
    return '<script id="__NEXT_DATA__" type="application/json">'+json.dumps(data)+'</script>'


class ProfileClient:
    def __init__(self,pages):self.pages=list(pages);self.calls=[]
    def get(self,url):
        self.calls.append(url);text=self.pages.pop(0)
        return httpx.Response(200,text=text,request=httpx.Request('GET',url))


def test_profile_scan_reads_all_pages_and_preserves_auto_link():
    one=dict(adId='10',title='First',price='1 234.50 р.',category='Техника',date='2026-11-10T00:00:00Z',adViewLink='https://www.kufar.by/item/10',image='a.jpg')
    two=dict(adId='20',title='20-БК-30 Second',price='99 р.',category='Авто',date='2026-11-09T00:00:00Z',adViewLink='https://auto.kufar.by/vi/20',image='b.jpg')
    client=ProfileClient([profile_html([one],2,'next-token'),profile_html([two],2)])
    url,rows=s.scan_profile('https://www.kufar.by/user/Valid_Profile',client)
    assert url.endswith('/user/Valid_Profile') and [r['ad_id'] for r in rows]==['10','20']
    assert rows[0]['price']=='1234.50' and rows[1]['url'].startswith('https://auto.')
    assert 'cursor=next-token' in client.calls[1]


def test_live_comparison_only_proposes_and_checks_price(inventory):
    root,_=inventory
    live=dict(rows=[dict(ad_id='1',title='Product 1',price='90.00'),dict(ad_id='5',title='555-БК-666 New',price='100.00')])
    with k.connect(root) as c:
        rows=s.load(c)[1];new,removed=s.compare_live(rows,live)
        assert [r['ad_id'] for r in new]==['5'] and new[0]['product_id']=='555-БК-666'
        assert {r['ad_id'] for r in removed}=={'2','3','4'}
        assert c.execute('SELECT count(*) FROM kufar_inventory').fetchone()[0]==4
    assert s.price_check('90.00','99.00')==dict(status='different',delta='-9.00')
    assert s.price_check('99.00','99.00')['status']=='same'


def test_confirmed_removed_ads_leave_proposals_and_reappear_if_live(inventory):
    root,_=inventory
    live=[dict(ad_id='1',title='Current',price='90.00',url='https://www.kufar.by/item/1')]
    with k.connect(root) as c:
        s.store_live_scan(c,s.DEFAULT_PROFILE_URL,live,1)
        assert len(s.compare_live(s.load(c)[1],s.load_live_scan(c))[1])==3
        assert s.confirm_removed(c,['2','3','4'],1)==3
        assert not s.compare_live(s.load(c)[1],s.load_live_scan(c))[1]
        s.store_live_scan(c,s.DEFAULT_PROFILE_URL,live+[dict(ad_id='2',title='Back',price='80.00',url='https://www.kufar.by/item/2')],1)
        row=next(r for r in s.load(c)[1] if r['ad_id']=='2')
        assert row['public_status']=='snapshot'


def test_live_product_links_only_include_current_exact_bindings(inventory):
    root,_=inventory
    live=[dict(ad_id='1',title='Current',price='99.00',url='https://www.kufar.by/item/1'),
          dict(ad_id='4',title='Current 4',price='88.00',url='https://www.kufar.by/item/4')]
    with k.connect(root) as c:
        s.store_live_scan(c,s.DEFAULT_PROFILE_URL,live,1)
        state=s.live_product_links(c)
        assert k.snapshot(c)['manual_count']==2
    assert state['matched_ads']==2
    assert {a['ad_id'] for a in state['links']['123-расп-456']}=={'1'}
    assert {a['ad_id'] for a in state['links']['777-БК-999']}=={'4'}


def test_public_page_confirms_exact_ad_and_preserves_kopecks():
    client=Client(ad=dict(adId='1',subject='Real title',body='Real description',calculator=[dict(currency='BYN',price='193765')],images=dict(gallery=['https://example.com/a.jpg'])))
    status,payload=s.check_public('1',client)
    assert status=='available' and payload['price']=='1937.65'
    assert payload['body']=='Real description'


def archive_rows():
    return [dict(ad_id='900',title='Old dryer',body='История продажи 999-БК-111, описано состояние.',price='1695.58 BYR',region='Минск (город)',published_at='2026-10-22T23:52:14',end_date='2026-10-23T08:27:20',code='999-БК-111',source='21_vek.xlsx'),
            dict(ad_id='901',title='Old kettle without code',body='Продано давно, код в описании не указан.',price='30,5 р.',region='Минск (город)',published_at='2026-09-01T10:00:00',end_date='2026-09-20T10:00:00',code='',source='21_vek.xlsx')]


def test_feed_stock_review_flags_prices_and_stock(inventory):
    records={'KFA':ET.fromstring('<record><unid>KFA</unid><subject>Card A</subject><body>desc</body><price>10.00</price><photos/></record>'),
             'KFB':ET.fromstring('<record><unid>KFB</unid><subject>Card B</subject><body>desc</body><price>5.00</price><photos/></record>')}
    rows=[dict(unid='KFA',product_id='123-расп-456',draft=json.dumps(dict(subject='Card A',body='desc',price='10.00',photos=[])),removed=0),
          dict(unid='KFB',product_id='gone-1',draft=None,removed=0),
          dict(unid='KFC',product_id='',draft=None,removed=0)]
    products={'123-расп-456':dict(is_active=True,site_price='12.50'),'gone-1':dict(is_active=False,site_price=None)}
    report=s.feed_stock_review(rows,records,products)
    assert report['price_changes']==[dict(unid='KFA',product_id='123-расп-456',subject='Card A',old='10.00',new='12.50')]
    assert [r['unid'] for r in report['out_of_stock']]==['KFB']
    assert report['unbound']==1 and report['actual']==0
    # When the price already matches, the card counts as actual.
    rows[0]['draft']=json.dumps(dict(subject='Card A',body='desc',price='12.50',photos=[]))
    assert s.feed_stock_review(rows,records,products)['actual']==1


def seed_inventory_card(conn,ad_id,product_id):
    conn.execute("INSERT INTO kufar_inventory(ad_id,payload,product_id) VALUES(?,?,?)",
                 (ad_id,json.dumps(dict(ad_id=ad_id,title='Card '+ad_id,body='desc',price='1.00',category='',
                                        photos=[],hosted_photos=[],included=False,reason='')),product_id))


def test_archive_card_moves_card_out_of_feed_and_back(inventory):
    root,_=inventory
    record=ET.fromstring('<record><unid>KF7</unid><subject>Card 7</subject><body>desc</body><price>9.00</price><photos/></record>')
    with k.connect(root) as c:
        FeedRemote.data=BASE_FEED
        c.execute("UPDATE state SET config=?,manual_count=0",(json.dumps({"feed_path":"kufar.xml"}),))
        k.reconcile(c,1,FeedRemote)
        seed_inventory_card(c,'7','123-расп-9')
        assert k.snapshot(c)['rows'][0]['unid']=='old-1'
        assert s.archive_card(c,'7',0,1,True)==dict(changed=False,detail='перемещено в архив')
        row=next(r for r in s.load(c)[1] if r['ad_id']=='7')
        assert row['archived'] and row['local_removed']
        # A card present in the working file is mirrored and published.
        c.execute("INSERT INTO listing(unid,product_id,source_xml) VALUES('KF7','123-расп-9',?)",(ET.tostring(record,encoding='utf-8'),))
        version=next(r['version'] for r in s.load(c)[1] if r['ad_id']=='7')
        assert s.archive_card(c,'7',version,1,True)['changed'] is True
        k.publish(c,k.snapshot(c)['revision'],1,FeedRemote)
        assert set(k.parse(FeedRemote.data)[1])=={'old-1'}
        assert not any(r['unid']=='KF7' for r in k.snapshot(c)['rows'])
        assert c.execute("SELECT count(*) FROM archive WHERE unid='KF7'").fetchone()[0]==1
        # Restoring brings the last published record back into the file.
        version=next(r['version'] for r in s.load(c)[1] if r['ad_id']=='7')
        assert s.archive_card(c,'7',version,1,False)['changed'] is True
        k.publish(c,k.snapshot(c)['revision'],1,FeedRemote)
        assert set(k.parse(FeedRemote.data)[1])=={'old-1','KF7'}
        row=next(r for r in s.load(c)[1] if r['ad_id']=='7')
        assert not row['archived'] and not row['local_removed']


def test_refresh_archives_catalogue_cards_without_stock(inventory,tmp_path):
    root,_=inventory
    with k.connect(root) as c:
        FeedRemote.data=BASE_FEED
        c.execute("UPDATE state SET config=?,manual_count=0",(json.dumps({"feed_path":"kufar.xml"}),))
        k.reconcile(c,1,FeedRemote)
        FeedRemote.data=k.snapshot(c)['baseline']
        seed_inventory_card(c,'9','555-расп-1')
        c.execute("UPDATE listing SET product_id='123-расп-456' WHERE unid='old-1'")
        c.execute("INSERT INTO listing(unid,product_id,source_xml) VALUES('KF9','555-расп-1',?)",
                  (ET.tostring(ET.fromstring('<record><unid>KF9</unid><subject>Card 9</subject><body>desc</body><price>3.00</price><photos/></record>'),encoding='utf-8'),))
        result=k.refresh_prices_and_stock(c,k.snapshot(c)['revision'],
            {'123-расп-456':dict(is_active=True,site_price='77.25'),'555-расп-1':dict(is_active=True,site_price='3.00')},1,FeedRemote)
        assert result['published'] and result['archived']==0
        result=k.refresh_prices_and_stock(c,k.snapshot(c)['revision'],
            {'123-расп-456':dict(is_active=True,site_price='77.25')},1,FeedRemote)
        assert result['removed']==1 and result['archived']==1
        assert 'KF9' not in k.parse(FeedRemote.data)[1]
        row=next(r for r in s.load(c)[1] if r['ad_id']=='9')
        assert row['archived'] and row['archived_at']


def test_accept_new_ads_stores_proposals_without_touching_feed(inventory):
    root,_=inventory
    live=[dict(ad_id='1',title='Product 1',price='90.00',url='https://www.kufar.by/item/1'),
          dict(ad_id='5',title='555-БК-666 New',price='100.00',url='https://www.kufar.by/item/5'),
          dict(ad_id='6',title='Unrelated kettle',price='12.00',url='https://www.kufar.by/item/6')]
    with k.connect(root) as c:
        s.bind(c,'3',0,'888-РАСП-999',1)
        s.store_live_scan(c,s.DEFAULT_PROFILE_URL,live,1)
        assert s.accept_new_ads(c,['5'],1)==1
        rows={r['ad_id']:r for r in s.load(c)[1]}
        added=rows['5']
        assert added['title']=='555-БК-666 New' and added['product_id']=='555-БК-666'
        assert not added['archived'] and rows['3']['product_id']=='888-расп-999'
        assert k.snapshot(c)['baseline'] is None and not k.snapshot(c)['rows']
        # A second pass has nothing new; existing card is not duplicated.
        with pytest.raises(k.Error):s.accept_new_ads(c,['5'],1)
        assert s.accept_new_ads(c,[],1)==1
        assert len(s.load(c)[1])==6


def test_accept_new_requires_profile_scan(inventory):
    root,_=inventory
    with k.connect(root) as c:
        with pytest.raises(k.Error):s.accept_new_ads(c,[],1)


def test_archive_import_appends_and_never_touches_existing(inventory):
    root,_=inventory
    with k.connect(root) as c:
        s.bind(c,'3',0,'888-РАСП-999',1)
        result=s.import_archive(c,archive_rows()+[dict(ad_id='1',title='Duplicate of existing',end_date='',code='')],1)
        assert result==dict(added=2,skipped=1)
        rows={r['ad_id']:r for r in s.load(c)[1]}
        old=rows['900']
        assert old['archived'] and old['product_id']=='999-БК-111' and old['price']=='1695.58'
        assert rows['901']['archived'] and not rows['901']['product_id']
        assert not rows['1']['archived'] and rows['3']['product_id']=='888-расп-999'
        assert s.import_archive(c,archive_rows(),1)==dict(added=0,skipped=2)


def test_archived_rows_are_labelled_and_excluded_from_duplicates(inventory):
    root,_=inventory
    with k.connect(root) as c:
        duplicates_before=sum(r['duplicate'] for r in s.load(c)[1])
        s.import_archive(c,archive_rows(),1)
        rows={r['ad_id']:r for r in s.load(c)[1]}
        labelled=s.reconcile(list(rows.values()),{'999-БК-111':dict(is_active=True)})
        assert next(r for r in labelled if r['ad_id']=='900')['stock']=='archived'
        assert next(r for r in labelled if r['ad_id']=='901')['stock_label']=='Архив — снят с продажи'
        assert not rows['900']['duplicate']
        # Archived codes must not add duplicates to the active catalogue.
        assert sum(r['duplicate'] for r in rows.values())==duplicates_before


def test_archived_ads_never_become_removal_proposals(inventory):
    root,_=inventory
    live=[dict(ad_id='1',title='Product 1',price='90.00',url='https://www.kufar.by/item/1')]
    with k.connect(root) as c:
        s.import_archive(c,archive_rows(),1)
        s.store_live_scan(c,s.DEFAULT_PROFILE_URL,live,1)
        rows=s.load(c)[1]
        new,removed=s.compare_live(rows,s.load_live_scan(c))
        assert not any(r['ad_id'] in {'900','901'} for r in removed)
        assert next(r for r in rows if r['ad_id']=='900')['live_status']=='confirmed_removed'


def test_full_candidate_skips_archived_even_when_seen_in_profile(inventory,tmp_path):
    root,_=inventory
    prepared=tmp_path/'prepared';pages=prepared/'pages';pages.mkdir(parents=True)
    (prepared/'parameter-schema.json').write_text(json.dumps({'15020':['condition','large_appliance_type']}),encoding='utf-8')
    page=dict(category='15020',params={'region':dict(p='region',v=7),'area':dict(p='area',v='22'),'condition':dict(p='condition',v='1'),'type':dict(p='large_appliance_type',v='20')})
    for ad_id in ['1','900']:
        (pages/(ad_id+'.json')).write_text(json.dumps(dict(page,ad_id=ad_id)),encoding='utf-8')
    live=[dict(ad_id='1',title='Product 1',price='90.00',url='https://www.kufar.by/item/1'),
          dict(ad_id='900',title='Old dryer',price='50.00',url='https://www.kufar.by/item/900')]
    with k.connect(root) as c:
        rows=archive_rows()
        rows[0]['code']=''
        s.import_archive(c,rows,1)
        s.store_live_scan(c,s.DEFAULT_PROFILE_URL,live,1)
        report=s.build_full_candidate(c,{'123-расп-456':dict(is_active=True,site_price='77.25')},prepared,tmp_path/'candidate',1)
    assert report['records']==1
    _,records=k.parse((tmp_path/'candidate'/'kufar-full-candidate.xml').read_bytes())
    assert set(records)=={'KF1'}


def test_archive_import_rejects_rows_without_title(inventory):
    root,_=inventory
    with k.connect(root) as c:
        with pytest.raises(k.Error):s.import_archive(c,[dict(ad_id='902',title='',end_date='2026-01-01',code='')],1)


@pytest.mark.parametrize('client',[Client(status=429),Client(ad=dict(adId='other')),Client(text='<html>captcha</html>')])
def test_unknown_or_throttled_page_not_marked_removed(client):
    with pytest.raises(k.Error):s.check_public('1',client)


def test_unavailable_keeps_last_description(inventory):
    root,path=inventory
    assert s.check_public('1',Client(status=404))==('unavailable',None)
    with k.connect(root) as c:
        s.store_check(c,'1','available',dict(title='Live title',body='Last known live description',price='12',photos=[]),1)
        s.store_check(c,'1','unavailable',None,1)
        row=next(r for r in s.load(c)[1] if r['ad_id']=='1')
        assert row['public_status']=='unavailable' and row['body']=='Last known live description'
