<?php
require_once __DIR__.'/includes/functions.php';
require_once __DIR__.'/includes/account-v281-ui.php';
mj_v281_ensure_schema();
$u=require_user(); $userId=(int)$u['id']; mj_require_user_feature('trust_verification', $userId, 'Trust & Verification is not enabled for your account.'); $pageTitle='Trust & Verification';
$tabs=['center'=>'Trust Center','verification'=>'Verification','ratings'=>'Ratings','safety'=>'Safety','reports'=>'Reports','devices'=>'Devices','privacy'=>'Privacy','blocked'=>'Blocked'];
$tab=(string)($_GET['tab']??'center'); if(!isset($tabs[$tab])){$tab='center';}
try{
  if($_SERVER['REQUEST_METHOD']==='POST'){
    verify_csrf(); $action=(string)($_POST['action']??'');
    if($action==='verification_request'){
      $type=trim((string)($_POST['verification_type']??'company'));
      $note=trim((string)($_POST['note']??''));
      q('INSERT INTO mj_verification_requests(user_id,verification_type,note,status,created_at) VALUES(?,?,?,"pending",NOW())',[$userId,substr($type,0,80),$note]);
      mj_v281_log_activity($userId,'verification_requested','Requested '.$type.' verification.');
      flash('success','Verification request submitted for admin review.'); redirect('trust-verification.php?tab=verification');
    }
  }
}catch(Throwable $e){ flash('error',$e->getMessage()); }
$completion=mj_v281_completion_score($u);
$requests=table_exists('mj_verification_requests')?all_rows('SELECT * FROM mj_verification_requests WHERE user_id=? ORDER BY id DESC LIMIT 20',[$userId]):[];
$devices=table_exists('mj_user_devices')?all_rows('SELECT * FROM mj_user_devices WHERE user_id=? ORDER BY last_seen_at DESC LIMIT 15',[$userId]):[];
$blocked=table_exists('mj_blocked_users')?all_rows('SELECT * FROM mj_blocked_users WHERE user_id=? ORDER BY id DESC LIMIT 20',[$userId]):[];
$privacy=table_exists('mj_user_privacy_requests')?all_rows('SELECT * FROM mj_user_privacy_requests WHERE user_id=? ORDER BY id DESC LIMIT 10',[$userId]):[];
include __DIR__.'/includes/header.php';
?>
<div class="container py-5"><div class="sidebar-layout"><?php include __DIR__.'/includes/account-sidebar.php'; ?><section>
  <div class="mj-page-hero rounded-4 mb-4"><div class="container-fluid"><span class="mj-badge mb-2"><i class="bi bi-shield-check"></i> Trust & Verification</span><h1 class="fw-bold mb-1">Trust & Verification</h1><p class="text-muted mb-0">Verification badges, trust score, safety tools, ratings, device security, report center and privacy controls.</p></div></div>
  <?php mj_v281_tabs($tabs,$tab,'trust-verification.php'); ?>

  <?php if($tab==='center'): ?>
    <div class="row g-4 mb-4"><div class="col-md-4"><div class="stat"><b><?=e((int)($u['trust_score']??0))?>%</b><br><span class="muted">Trust Score</span><div class="mj-progress-soft mt-2"><span style="width:<?=e(min(100,max(0,(int)($u['trust_score']??0))))?>%"></span></div></div></div><div class="col-md-4"><div class="stat"><b><?=e($completion['score'])?>%</b><br><span class="muted">Profile Complete</span><div class="mj-progress-soft mt-2"><span style="width:<?=e($completion['score'])?>%"></span></div></div></div><div class="col-md-4"><div class="stat"><b><?=e(count($requests))?></b><br><span class="muted">Verification Requests</span></div></div></div>
    <?php mj_v281_feature_grid([['bi-patch-check','Trust Badge Levels','Basic, Verified, Premium and Business Verified.'],['bi-envelope-check','Email Verification','Confirm email and unlock verified badge steps.'],['bi-phone','Mobile Verification','Confirm mobile or WhatsApp number.'],['bi-building-check','Company Verification','Admin approval for business registration and company proof.'],['bi-award','Top Seller Badge','Based on response speed, activity, rating and no complaints.'],['bi-shield-lock','Safe Seller Badge','Verified, good rating and clean report history.']]); ?>

  <?php elseif($tab==='verification'): ?>
    <form method="post" class="card card-body border-0 rounded-4 shadow-sm mb-4"><?=csrf_input()?><input type="hidden" name="action" value="verification_request"><h3 class="h5 fw-bold">Request Verification</h3><div class="row g-3"><div class="col-md-4"><label class="label">Verification Type</label><select class="form-select" name="verification_type"><option value="email">Email</option><option value="mobile">Mobile / WhatsApp</option><option value="company">Company</option><option value="address">Address</option><option value="document">Document</option><option value="social">Social / Website</option></select></div><div class="col-md-8"><label class="label">Note for Admin</label><input class="form-control" name="note" placeholder="Tell admin what you want verified"></div></div><button class="btn btn-primary rounded-pill mt-3">Submit Verification Request</button></form>
    <div class="card card-body border-0 rounded-4 shadow-sm"><h3 class="h5 fw-bold">Verification History</h3><?php if(!$requests): mj_v281_empty('No verification requests','Submit email, mobile, company, address, document or social verification request.','bi-patch-check'); else: ?><div class="table-responsive"><table class="table"><thead><tr><th>Date</th><th>Type</th><th>Status</th><th>Note</th><th>Expires</th></tr></thead><tbody><?php foreach($requests as $r): ?><tr><td><?=e($r['created_at']??'')?></td><td><?=e($r['verification_type']??'')?></td><td><?=mj_v281_badge((string)($r['status']??'pending'))?></td><td><?=e($r['note']??'')?></td><td><?=e($r['expires_at']??'')?></td></tr><?php endforeach; ?></tbody></table></div><?php endif; ?></div>

  <?php elseif($tab==='ratings'): ?>
    <?php mj_v281_feature_grid([['bi-star-half','Seller Rating Breakdown','Response speed, accuracy, service quality and completed orders.'],['bi-person-star','Buyer Rating','Sellers can rate buyers after completed order.'],['bi-chat-square-heart','Review Management','Review history and admin moderation-ready flow.'],['bi-lightning-charge','Fast Responder Badge','Badge based on enquiry reply speed.'],['bi-trophy','Top Seller Badge','Award active sellers with strong ratings.'],['bi-graph-up-arrow','Trust Impact','Ratings improve trust and marketplace ranking.']]); ?>

  <?php elseif($tab==='safety'): ?>
    <?php mj_v281_feature_grid([['bi-exclamation-triangle','Scam / Fraud Report','Simple safety report flow to admin.'],['bi-slash-circle','Blocked Keywords Alert','Warn before posting risky/spam content.'],['bi-person-x','Block User','Block unwanted buyers/sellers.'],['bi-shield-exclamation','Suspicious Activity Alert','Too many messages/clicks from one user can trigger admin alert.'],['bi-lock','Hide Phone Until Login','Protect seller contact details.'],['bi-envelope-slash','Hide Email Always','Use marketplace enquiry instead of showing email.']]); ?>

  <?php elseif($tab==='reports'): ?>
    <div class="mj-mini-card"><h3 class="h5 fw-bold">Report Center</h3><p class="text-muted">Submitted listing, user, scam, fraud and abuse reports are prepared for admin review tracking.</p><a class="btn btn-primary rounded-pill" href="<?=base_url('report.php')?>">Report Listing / User</a></div>

  <?php elseif($tab==='devices'): ?>
    <div class="card card-body border-0 rounded-4 shadow-sm"><h3 class="h5 fw-bold">Device Management</h3><?php if(!$devices): mj_v281_empty('No login devices found','Login history, IP address, browser and force logout flow will appear here.','bi-phone'); else: ?><div class="table-responsive"><table class="table"><thead><tr><th>IP</th><th>Device</th><th>Created</th><th>Last Seen</th></tr></thead><tbody><?php foreach($devices as $d): ?><tr><td><?=e($d['ip_address']??'')?></td><td><?=e($d['user_agent']??'')?></td><td><?=e($d['created_at']??'')?></td><td><?=e($d['last_seen_at']??'')?></td></tr><?php endforeach; ?></tbody></table></div><?php endif; ?></div>
    <div class="mt-4"><?php mj_v281_feature_grid([['bi-box-arrow-right','Logout Old Sessions','Force logout all devices except current session.'],['bi-bell','Login Alert','Notify when account logs in from new device.'],['bi-key','2FA Ready','Google Authenticator, email OTP and mobile OTP ready.']]); ?></div>

  <?php elseif($tab==='privacy'): ?>
    <div class="d-flex justify-content-between align-items-center mb-3"><h3 class="h5 fw-bold mb-0">Privacy Requests</h3><a class="btn btn-outline-primary rounded-pill" href="<?=base_url('account-center.php?tab=privacy')?>">Open Privacy Center</a></div>
    <?php if(!$privacy): mj_v281_empty('No privacy request','Data download, deactivate account and delete account requests appear here.','bi-shield-lock'); else: ?><div class="table-responsive card card-body border-0 rounded-4 shadow-sm"><table class="table"><thead><tr><th>Date</th><th>Type</th><th>Status</th><th>Notes</th></tr></thead><tbody><?php foreach($privacy as $p): ?><tr><td><?=e($p['created_at']??'')?></td><td><?=e($p['request_type']??'')?></td><td><?=mj_v281_badge((string)($p['status']??'pending'))?></td><td><?=e($p['notes']??'')?></td></tr><?php endforeach; ?></tbody></table></div><?php endif; ?>

  <?php elseif($tab==='blocked'): ?>
    <?php if(!$blocked): mj_v281_empty('No blocked contacts','Blocked users or contacts will show here. Add them from Account Center > Blocked Users.','bi-slash-circle'); else: ?><div class="table-responsive card card-body border-0 rounded-4 shadow-sm"><table class="table"><thead><tr><th>Contact</th><th>Reason</th><th>Date</th></tr></thead><tbody><?php foreach($blocked as $b): ?><tr><td><?=e($b['blocked_email']??$b['blocked_user_id']??'')?></td><td><?=e($b['reason']??'')?></td><td><?=e($b['created_at']??'')?></td></tr><?php endforeach; ?></tbody></table></div><?php endif; ?>
  <?php endif; ?>
</section></div></div>
<?php include __DIR__.'/includes/footer.php'; ?>
