<?php
require __DIR__.'/oracle.php';$check($lane==='C'&&$business==='','Only approved C business');$gate=json_decode(file_get_contents(__DIR__.'/private-fixture-gate.json'),true);$check($gate['passed']===true,'Complete fixture gate first');$check(!file_exists(__DIR__.'/private-fixture-insert.json'),'Only one INSERT');$write=json_decode(file_get_contents(__DIR__.'/private-fixture-write.json'),true);$natural=json_decode(file_get_contents(__DIR__.'/private-natural-before.json'),true);$check($actor===$actorNow(),'Actor unchanged');$check($natural===$q('SELECT * FROM '.$p.'agent_conversation_log ORDER BY ID'),'Full natural table unchanged');$check((int)$q('SELECT COUNT(*) AS CNT FROM '.$p.'agent_conversation_log WHERE USER_NAME=?',[$write['user_name']])[0]['cnt']===0,'Marker remains absent');
$db->beginTransaction();try{$keys=array_keys($write);$sql='INSERT INTO '.$p.'agent_conversation_log ('.implode(',',array_map('strtoupper',$keys)).') VALUES ('.implode(',',array_fill(0,count($keys),'?')).')';$s=$db->prepare($sql);$s->execute(array_values($write));$check($s->rowCount()===1,'Exactly one inserted');$rows=$q('SELECT * FROM '.$p.'agent_conversation_log WHERE USER_NAME=?',[$write['user_name']]);$check(count($rows)===1,'Unique own row');$row=$rows[0];$check((int)$row['id']>0,'Generated PK');foreach($write as $key=>$value){$actual=$row[$key];if(is_int($value))$check((int)$actual===$value,'Numeric persisted '.$key);elseif($key==='create_time')$check($actual===$value.'.000000'||$actual===$value,'Datetime persisted');else $check($actual===$value,'Persisted '.$key);}$check($natural===$q('SELECT * FROM '.$p.'agent_conversation_log WHERE ID<>? ORDER BY ID',[$row['id']]),'Natural PK exclusion exact');foreach(['agent_feedback_log','agent_suggestion_click_log']as $table)$check((int)$q('SELECT COUNT(*) AS CNT FROM '.$p.$table.' WHERE CONVERSATION_LOG_ID=?',[$row['id']])[0]['cnt']===0,'No existing references');$check($actor===$actorNow(),'Actor unchanged');$db->commit();$save('private-fixture-insert.json',['id'=>$row['id'],'row'=>$row,'natural_excluding_own_sha256'=>$hash($natural),'actor_sha256'=>$hash($actor),'inserted'=>1,'retained'=>true]);echo json_encode(['lane'=>'C','inserted'=>1,'id'=>$row['id'],'retained'=>true,'natural_unchanged'=>true,'actor_unchanged'=>true,'full_row_sha256'=>$hash($row)]),"\n";
}catch(Throwable $e){if($db->inTransaction())$db->rollBack();$save('private-fixture-insert-error.json',['class'=>get_class($e),'message'=>$e->getMessage()]);throw $e;}
