<?php
require_once __DIR__.'/includes/functions.php';
$country = country_by_slug($_GET['country'] ?? '');
if(!$country){ http_response_code(404); $pageTitle='Country not found'; include __DIR__.'/includes/header.php'; echo '<section class="mj-section"><div class="container"><div class="alert alert-warning rounded-4">Country not found.</div></div></section>'; include __DIR__.'/includes/footer.php'; exit; }
$state = state_by_slug((int)$country['id'], $_GET['state'] ?? '');
if(!$state){ http_response_code(404); $pageTitle='State not found'; include __DIR__.'/includes/header.php'; echo '<section class="mj-section"><div class="container"><div class="alert alert-warning rounded-4">State / region not found.</div></div></section>'; include __DIR__.'/includes/footer.php'; exit; }
$city = city_by_slug((int)$country['id'], $_GET['city'] ?? '', (int)$state['id']);
if(!$city){ http_response_code(404); $pageTitle='City not found'; include __DIR__.'/includes/header.php'; echo '<section class="mj-section"><div class="container"><div class="alert alert-warning rounded-4">City not found.</div></div></section>'; include __DIR__.'/includes/footer.php'; exit; }

$countryId = (int)$country['id'];
$stateId = (int)$state['id'];
$cityId = (int)$city['id'];
$pageTitle = $city['name'].' '.$country['name'].' Marketplace';
$timezone = $city['timezone'] ?: ($state['timezone'] ?: $country['timezone_default']);
$listings=mj_public_fetch_listings(['country_id'=>$countryId, 'state_id'=>$stateId, 'city_id'=>$cityId], 12);
$companies=mj_public_fetch_companies(['country_id'=>$countryId, 'state_id'=>$stateId, 'city_id'=>$cityId], 12);
$stats = [
  'listings' => safe_count_public('SELECT COUNT(*) c FROM mj_listings WHERE country_id=? AND city_id=? AND '.mj_public_listing_status_sql('mj_listings'),[$countryId,$cityId]),
  'companies' => safe_count_public('SELECT COUNT(*) c FROM mj_companies WHERE country_id=? AND city_id=? AND '.mj_public_company_status_sql('mj_companies'),[$countryId,$cityId]),
  'verified' => safe_count_public('SELECT COUNT(*) c FROM mj_companies WHERE country_id=? AND city_id=? AND '.mj_public_company_status_sql('mj_companies').' AND is_verified=1',[$countryId,$cityId]),
];
try { $dt = new DateTime('now', new DateTimeZone($timezone)); $localNow = $dt->format('d M Y, h:i A').' '.$timezone; } catch(Throwable $e) { $localNow=''; }
include __DIR__.'/includes/header.php';
?>
<section class="mj-home-hero mj-country-home-hero">
  <div class="container position-relative">
    <div class="row align-items-center g-5">
      <div class="col-lg-7" data-aos="fade-right">
        <div class="d-flex flex-wrap gap-2 mb-3"><span class="mj-home-pill is-local"><i class="bi bi-geo-alt-fill me-1"></i><?=e($city['name'])?></span><span class="mj-home-pill"><i class="bi bi-map me-1"></i><?=e($state['name'])?></span><span class="mj-home-pill"><i class="bi bi-clock-history me-1"></i><?=e($timezone)?></span></div>
        <h1><?=e($city['name'])?> Marketplace</h1>
        <p class="mj-home-lead">Browse local listings, services and verified companies in <?=e($city['name'])?>, <?=e($country['name'])?>.</p>
        <form class="mj-home-search" action="<?=base_url('browse.php')?>" method="get">
          <input type="hidden" name="country_id" value="<?=e($countryId)?>"><input type="hidden" name="state_id" value="<?=e($stateId)?>"><input type="hidden" name="city_id" value="<?=e($cityId)?>">
          <div class="row g-2 align-items-center"><div class="col-lg-8"><div class="mj-home-input"><i class="bi bi-search"></i><input class="form-control" name="q" placeholder="Search in <?=e($city['name'])?>"></div></div><div class="col-lg-4 d-grid"><button class="btn btn-primary"><i class="bi bi-search me-1"></i>Search</button></div></div>
          <div class="mj-home-quick mt-3"><span>Local time:</span><a href="#"><?=e($localNow ?: $timezone)?></a><a href="<?=e(base_url($country['slug'].'/'.$state['slug'].'/'))?>">Back to <?=e($state['name'])?></a></div>
        </form>
        <div class="mj-home-stats mj-country-stats mt-4"><div><b><?=e(number_format((int)$stats['listings']))?></b><span>Listings</span></div><div><b><?=e(number_format((int)$stats['companies']))?></b><span>Companies</span></div><div><b><?=e(number_format((int)$stats['verified']))?></b><span>Verified</span></div></div>
      </div>
      <div class="col-lg-5" data-aos="fade-left"><div class="mj-home-hero-board"><div class="mj-home-window"><div class="mj-window-top"><span></span><span></span><span></span><b><?=e($city['name'])?></b></div><img src="<?=asset('images/marketplace-hero.svg')?>" alt="<?=e($city['name'])?> marketplace"><div class="mj-home-check"><i class="bi bi-check-lg"></i></div></div><div class="mj-home-floating-card card-a"><i class="bi bi-receipt-cutoff"></i><b>Quotes</b><small>Compare replies</small></div><div class="mj-home-floating-card card-b"><i class="bi bi-chat-dots"></i><b>Chat</b><small>Ask before buying</small></div><div class="mj-home-floating-card card-c"><i class="bi bi-patch-check-fill"></i><b>Verified</b><small>Local companies</small></div></div></div>
    </div>
  </div>
</section>
<section class="mj-section mj-home-category-section"><div class="container"><div class="mj-section-title mj-home-title"><div><span class="mj-badge mb-2"><i class="bi bi-tags"></i>Latest</span><h2>Listings in <?=e($city['name'])?></h2><p class="text-muted mb-0">Newest active listings in this city.</p></div><a class="btn btn-outline-primary rounded-pill" href="<?=base_url('browse.php?country_id='.$countryId.'&city_id='.$cityId)?>">Browse all</a></div><?php if(!$listings): ?><div class="alert alert-info rounded-4">No active listings in this city yet.</div><?php else: ?><div class="row g-4"><?php foreach($listings as $r): ?><div class="col-md-6 col-xl-3"><?php include __DIR__.'/partials/listing-card.php'; ?></div><?php endforeach; ?></div><?php endif; ?></div></section>
<section class="mj-section bg-white"><div class="container"><div class="mj-section-title mj-home-title"><div><span class="mj-badge good mb-2"><i class="bi bi-building-check"></i>Companies</span><h2>Companies in <?=e($city['name'])?></h2><p class="text-muted mb-0">Trusted company profiles in this city.</p></div><a class="btn btn-outline-primary rounded-pill" href="<?=base_url('companies.php?country_id='.$countryId.'&city_id='.$cityId)?>">View companies</a></div><?php if(!$companies): ?><div class="alert alert-info rounded-4">No active companies in this city yet.</div><?php else: ?><div class="row g-4"><?php foreach($companies as $r): ?><div class="col-md-6 col-xl-3"><?php include __DIR__.'/partials/company-card.php'; ?></div><?php endforeach; ?></div><?php endif; ?></div></section>
<?php include __DIR__.'/includes/footer.php'; ?>
