<?php
// Candidate report only. This script never mutates production.
$c=include '/workspace/wwwroot/tnxlgstyj/project/application/config/prod/tnxlgstyj/database.php';
$p=new PDO('mysql:host='.$c['hostname'].';dbname='.$c['database'].';charset=utf8mb4',$c['username'],$c['password'],[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC]);
function n($p,$q,$a){$s=$p->prepare($q);$s->execute($a);return(int)$s->fetchColumn();}
$out=[];
foreach($p->query('SELECT h.id,h.uuid,h.name,h.model_num,h.status,h.is_show,c.group_type FROM tn_equipment_hardware h JOIN tn_equipment_hardware_cate c ON c.uuid=h.cate_uuid WHERE h.is_show=1 ORDER BY h.id') as$h){$a=[$h['id'],$h['uuid'],$h['uuid'],$h['uuid'],$h['uuid'],$h['uuid'],$h['uuid']];$refs=n($p,'SELECT (SELECT COUNT(*) FROM tn_equipment_result WHERE equipment_id=?)+(SELECT COUNT(*) FROM tn_device_lactate WHERE equipment_uuid=?)+(SELECT COUNT(*) FROM tn_equipment_eliga WHERE device_uuid=?)+(SELECT COUNT(*) FROM tn_equipment_miccogate WHERE equipment_uuid=?)+(SELECT COUNT(*) FROM tn_equipment_vald WHERE device_uuid=?)+(SELECT COUNT(*) FROM tn_equipment_concept2 WHERE equipment_uuid=?)+(SELECT COUNT(*) FROM tn_equipment_wattbike WHERE equipment_uuid=?)',$a);if((int)$h['group_type']===2)$refs+=n($p,'SELECT COUNT(*) FROM tn_gymware_reps',[]);if((int)$h['group_type']===14)$refs+=n($p,'SELECT COUNT(*) FROM tn_enginery_data',[]);if($refs===0)$out[]=$h+['all_data_refs'=>0,'proposed_action'=>'soft-disable is_show=0 after separate approval'];}
echo json_encode(['mode'=>'report-only','count'=>count($out),'candidates'=>$out],JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES),PHP_EOL;
