"""Authored in-memory SQLite record/vector link audit; vectors are manual and no database service is contacted."""
import sqlite3,json,hashlib
def sha(s):return hashlib.sha256(s.encode('utf-8')).hexdigest()
def demo():
    c=sqlite3.connect(':memory:');c.row_factory=sqlite3.Row
    c.executescript('CREATE TABLE source(id TEXT PRIMARY KEY,body TEXT NOT NULL);CREATE TABLE point(id TEXT PRIMARY KEY,source_id TEXT,source_sha TEXT,space TEXT,x REAL,y REAL);')
    docs={'D1':'زمان آماده سازی دو روز است.','D2':'نوبت فقط پس از تایید ثبت می شود.'}
    c.executemany('INSERT INTO source VALUES (?,?)',list(docs.items()))
    points=[('V1','D1',sha('متن قدیمی'), 'toy-v1',1.,0.),('V2','D1',sha(docs['D1']),'toy-v1',.9,.1),('V3','D-missing',sha('منبع ناموجود'),'toy-v1',.95,.05),('V4','D2',sha(docs['D2']),'toy-v1',0.,1.)]
    c.executemany('INSERT INTO point VALUES (?,?,?,?,?,?)',points)
    raw=[];accepted=[];rejected=[]
    for r in c.execute('SELECT p.*,s.body FROM point p LEFT JOIN source s ON s.id=p.source_id'):
        d=dict(r);d['distance_squared']=(d['x']-1)**2+d['y']**2;raw.append(d)
        reason='missing_source' if d['body'] is None else 'source_content_hash_changed' if sha(d['body'])!=d['source_sha'] else 'space_mismatch' if d['space']!='toy-v1' else None
        if reason:rejected.append({'point_id':d['id'],'reason':reason})
        else:accepted.append(d)
    raw.sort(key=lambda r:(r['distance_squared'],r['id']));accepted.sort(key=lambda r:(r['distance_squared'],r['id']))
    assert raw[0]['id']=='V1' and accepted[0]['id']=='V2'
    assert {r['reason'] for r in rejected}=={'missing_source','source_content_hash_changed'}
    before=c.execute('SELECT count(*) FROM point').fetchone()[0]
    c.execute('INSERT INTO point VALUES (?,?,?,?,?,?) ON CONFLICT(id) DO UPDATE SET source_id=excluded.source_id,source_sha=excluded.source_sha,space=excluded.space,x=excluded.x,y=excluded.y',points[1])
    after=c.execute('SELECT count(*) FROM point').fetchone()[0];assert before==after==4
    result={'original_source_records':docs,'source_hashes':{k:sha(v) for k,v in docs.items()},'query_vector':[1,0],'query_space':'toy-v1','all_vectors_manually_authored_not_embeddings':True,'ranked_raw_points':raw,'rejected_records':rejected,'ranked_source_link_valid_points':accepted,'same_id_repeat_upsert_counts':[before,after],'schema_intentionally_allows_bad_import_links_for_audit':True,'hash_integrity_not_semantic_or_validity_date_proof':True,'real_customer_data_or_qdrant_service_used':False}
    c.close();return result
if __name__=='__main__':print(json.dumps(demo(),ensure_ascii=False,indent=2))
