<?php
/**
 * Debug: بررسی داده‌های داشبورد املاک
 * حذف بعد از استفاده
 */
if ( $_GET['key'] !== 'dbg2024' ) { http_response_code(403); exit('دسترسی غیرمجاز'); }

define('ABSPATH', dirname(__FILE__) . '/../../');
require_once dirname(__FILE__) . '/../../wp-load.php';
global $wpdb;
$pre = $wpdb->prefix;

header('Content-Type: text/html; charset=utf-8');
?><!DOCTYPE html><html dir="rtl"><head><meta charset="utf-8">
<style>body{font-family:Tahoma;padding:20px} table{border-collapse:collapse;width:100%} td,th{border:1px solid #ddd;padding:6px 10px} th{background:#f0f0f0}</style>
</head><body>
<h2>🔍 Debug داشبورد املاک</h2>

<?php
/* تعداد پارسل */
$cnt = $wpdb->get_var("SELECT COUNT(*) FROM {$pre}gis_parcels");
echo "<p>📦 تعداد کل پارسل: <strong>{$cnt}</strong></p>";

/* تعداد با neighborhood_id */
$with_nh = $wpdb->get_var("SELECT COUNT(*) FROM {$pre}gis_parcels WHERE neighborhood_id IS NOT NULL");
echo "<p>🏘️ پارسل با محله: <strong>{$with_nh}</strong></p>";

/* تعداد بدون neighborhood_id */
$without_nh = $wpdb->get_var("SELECT COUNT(*) FROM {$pre}gis_parcels WHERE neighborhood_id IS NULL");
echo "<p>❓ پارسل بدون محله: <strong>{$without_nh}</strong></p>";

/* تعداد ساختمان */
$bld = $wpdb->get_var("SELECT COUNT(*) FROM {$pre}gis_parcel_building");
echo "<p>🏗️ رکورد ساختمان: <strong>{$bld}</strong></p>";
$bld_has = $wpdb->get_var("SELECT COUNT(*) FROM {$pre}gis_parcel_building WHERE has_building=1");
echo "<p>🏗️ دارای ساختمان=1: <strong>{$bld_has}</strong></p>";

/* تعداد محلات */
$nh_cnt = $wpdb->get_var("SELECT COUNT(*) FROM {$pre}gis_neighborhoods");
echo "<p>🗺️ تعداد محلات: <strong>{$nh_cnt}</strong></p>";

/* کوئری dashboard بدون فیلتر */
echo "<h3>کوئری داشبورد (بدون فیلتر)</h3>";
$rows = $wpdb->get_results("
    SELECT
        p.neighborhood_id,
        n.name AS neighborhood_name,
        p.district_id,
        d.name AS district_name,
        COUNT(DISTINCT p.id) AS total_parcels,
        COUNT(DISTINCT CASE WHEN pb.has_building=1 THEN p.id END) AS with_building,
        AVG(pl.area_sqm) AS avg_area
    FROM {$pre}gis_parcels p
    LEFT JOIN {$pre}gis_neighborhoods n ON n.id = p.neighborhood_id
    LEFT JOIN {$pre}gis_districts d     ON d.id = p.district_id
    LEFT JOIN {$pre}gis_parcel_land pl  ON pl.parcel_id = p.id
    LEFT JOIN {$pre}gis_parcel_building pb ON pb.parcel_id = p.id
    WHERE p.status != 'archived'
    GROUP BY p.neighborhood_id, p.district_id, n.name, d.name
    ORDER BY total_parcels DESC
    LIMIT 20
", ARRAY_A);

if ($rows) {
    echo "<table><tr><th>neighborhood_id</th><th>محله</th><th>منطقه</th><th>تعداد</th><th>با ساختمان</th><th>میانگین زمین</th></tr>";
    foreach ($rows as $r) {
        echo "<tr><td>{$r['neighborhood_id']}</td><td>{$r['neighborhood_name']}</td><td>{$r['district_name']}</td><td>{$r['total_parcels']}</td><td>{$r['with_building']}</td><td>". round($r['avg_area']) ."</td></tr>";
    }
    echo "</table>";
} else {
    echo "<p style='color:red'>❌ داده‌ای یافت نشد — " . $wpdb->last_error . "</p>";
}

/* ۵ پارسل اول */
echo "<h3>۵ پارسل اول</h3>";
$ps = $wpdb->get_results("SELECT id, neighborhood_id, district_id, status, lat, lng FROM {$pre}gis_parcels LIMIT 5", ARRAY_A);
if ($ps) {
    echo "<table><tr><th>id</th><th>neighborhood_id</th><th>district_id</th><th>status</th><th>lat</th><th>lng</th></tr>";
    foreach ($ps as $p) {
        echo "<tr><td>{$p['id']}</td><td>{$p['neighborhood_id']}</td><td>{$p['district_id']}</td><td>{$p['status']}</td><td>{$p['lat']}</td><td>{$p['lng']}</td></tr>";
    }
    echo "</table>";
} else {
    echo "<p>جدول gis_parcels خالی است</p>";
}
?>
</body></html>
