<?php
declare(strict_types=1);
require_once __DIR__ . '/../app/bootstrap.php';
use App\Core\Database;

$ref=trim((string)($_GET['ref']??''));
$member=$ref!==''?Database::safeFetchOne('SELECT am.*,u.name,u.phone,u.email FROM affiliate_members am LEFT JOIN users u ON u.id=am.user_id WHERE am.referral_code=? AND am.status="active" LIMIT 1',[$ref]):null;
if(!$member){http_response_code(404);$pageTitle='Member Store Not Found';include ROOT_PATH.'/app/Layouts/frontend_header.php';echo '<section class="container py-5"><div class="alert alert-warning">This affiliate store is unavailable.</div></section>';include ROOT_PATH.'/app/Layouts/frontend_footer.php';exit;}
$_SESSION['affiliate_referral_code']=$ref;
Database::safeExecute('INSERT INTO affiliate_clicks (referral_code,landing_url,ip_address,user_agent,created_at) VALUES (?,?,?,?,NOW())',[$ref,substr((string)($_SERVER['REQUEST_URI']??''),0,255),substr((string)($_SERVER['REMOTE_ADDR']??''),0,60),substr((string)($_SERVER['HTTP_USER_AGENT']??''),0,255)]);
$products=Database::safeFetchAll('SELECT p.*,v.shop_name FROM affiliate_products ap JOIN products p ON p.id=ap.product_id LEFT JOIN vendors v ON v.id=p.vendor_id WHERE ap.affiliate_member_id=? AND p.status="published" ORDER BY ap.sort_order,ap.id DESC',[$member['id']]);
$social=json_decode((string)($member['social_json']??'{}'),true)?:[];
$pageTitle=($member['landing_title']?:$member['name']).' | '.BRAND_NAME;
include ROOT_PATH.'/app/Layouts/frontend_header.php';
?>
<section class="container py-5"><div class="landing-hero p-5 position-relative" <?php if(!empty($member['landing_banner'])): ?>style="background-image:linear-gradient(90deg,rgba(255,255,255,.94),rgba(255,255,255,.75)),url('<?= e(product_image(['image'=>$member['landing_banner']])) ?>');background-size:cover;background-position:center"<?php endif; ?>><div class="row align-items-center g-4"><div class="col-md-8"><span class="rank-badge"><?= e($member['rank_code']?:'IBO') ?></span><h1 class="fw-bold mt-3"><?= e($member['landing_title']?:$member['name']) ?></h1><p><?= nl2br(e($member['landing_bio']??'')) ?></p><p class="mb-0">Referral Code: <strong><?= e($member['referral_code']) ?></strong></p></div><?php if(!empty($member['landing_image'])): ?><div class="col-md-4 text-center"><img class="img-fluid rounded-circle border shadow" style="width:190px;height:190px;object-fit:cover" src="<?= e(product_image(['image'=>$member['landing_image']])) ?>" alt="<?= e($member['name']) ?>"></div><?php endif; ?></div></div>
<div class="d-flex justify-content-between align-items-center mt-5 mb-3"><h3 class="mb-0">Selected Products</h3><span class="text-muted"><?= count($products) ?> products</span></div><div class="row g-4"><?php if(!$products): ?><div class="col-12"><div class="alert alert-info">No products have been selected for this store yet.</div></div><?php endif; ?><?php foreach($products as $p): ?><div class="col-sm-6 col-lg-3"><div class="product-card h-100"><img class="w-100" src="<?= e(product_image($p)) ?>" alt="<?= e($p['name']) ?>"><div class="p-3"><small class="text-muted"><?= e($p['shop_name']??'') ?></small><h6><?= e($p['name']) ?></h6><div class="price mb-2"><?= money($p['sale_price']?:$p['regular_price']) ?></div><a class="btn btn-sm btn-primary" href="<?= url('product/'.$p['slug'].'?ref='.rawurlencode($member['referral_code'])) ?>">View Product</a></div></div></div><?php endforeach; ?></div>
<?php if(!empty($member['contact_info'])||array_filter($social)): ?><div class="card border-0 shadow-sm mt-5"><div class="card-body"><h4>Contact</h4><p><?= nl2br(e($member['contact_info']??'')) ?></p><div class="d-flex gap-2 flex-wrap"><?php foreach($social as $network=>$link): if(!$link)continue; ?><a class="btn btn-outline-primary btn-sm" target="_blank" rel="noopener" href="<?= e($link) ?>"><?= e(ucfirst($network)) ?></a><?php endforeach; ?></div></div></div><?php endif; ?></section>
<?php include ROOT_PATH.'/app/Layouts/frontend_footer.php'; ?>
