<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

header('Content-Type: application/json');

require_once __DIR__ . '/../../core/db_connect.php';

$landlord = $_GET['landlord'] ?? 'ALL';
$source   = $_GET['source'] ?? 'ALL';
$search   = trim($_GET['search'] ?? '');

$where = [];
$params = [];

if ($landlord !== 'ALL') {
    $where[] = "(p.landlord_id = ?)";
    $params[] = (int)$landlord;
}

// SEARCH across serial, property_number, tenant fullname
if ($search !== '') {
    $where[] = "(m.serial LIKE ? 
                 OR m.serial_number LIKE ?
                 OR p.property_number LIKE ?
                 OR tu.fullname LIKE ?)";
    $params[] = "%$search%";
    $params[] = "%$search%";
    $params[] = "%$search%";
    $params[] = "%$search%";
}

$whereSQL = $where ? "WHERE " . implode(" AND ", $where) : "";

/* ============================================================
   IAMMETER (smart_meters)
   ============================================================ */
$sql_iammeter = "
SELECT 
    'IAMMETER' AS source,
    m.serial,
    p.property_number AS property_name,
    lu.fullname AS landlord_name,
    tu.fullname AS tenant_name,
    m.last_synced_at AS last_sync
FROM smart_meters m
LEFT JOIN properties p ON m.property_id = p.id
LEFT JOIN rbe_users lu ON p.landlord_id = lu.id
LEFT JOIN tenants t ON m.tenant_id = t.id
LEFT JOIN rbe_users tu ON t.user_id = tu.id
{$whereSQL}
";

/* ============================================================
   FLECTO (flecto_meters)
   ============================================================ */
$sql_flecto = "
SELECT 
    'FLECTO' AS source,
    m.serial_number AS serial,
    p.property_number AS property_name,
    lu.fullname AS landlord_name,
    tu.fullname AS tenant_name,
    m.last_synced_at AS last_sync
FROM flecto_meters m
LEFT JOIN properties p ON m.property_id = p.id
LEFT JOIN rbe_users lu ON p.landlord_id = lu.id
LEFT JOIN tenants t ON m.tenant_id = t.id
LEFT JOIN rbe_users tu ON t.user_id = tu.id
{$whereSQL}
";

/* ============================================================
   WATER (water_meters)
   ============================================================ */
$sql_water = "
SELECT 
    'WATER' AS source,
    m.serial_number AS serial,
    p.property_number AS property_name,
    lu.fullname AS landlord_name,
    tu.fullname AS tenant_name,
    NULL AS last_sync   -- WATER meters don’t have last_synced_at
FROM water_meters m
LEFT JOIN properties p ON m.property_id = p.id
LEFT JOIN rbe_users lu ON p.landlord_id = lu.id
LEFT JOIN tenants t ON m.tenant_id = t.id
LEFT JOIN rbe_users tu ON t.user_id = tu.id
{$whereSQL}
";

$results = [];

// IAMMETER
if ($source === 'IAMMETER' || $source === 'ALL') {
    $stmt = $conn_rbe->prepare($sql_iammeter);
    $stmt->execute($params);
    $results = array_merge($results, $stmt->fetchAll(PDO::FETCH_ASSOC));
}

// FLECTO
if ($source === 'FLECTO' || $source === 'ALL') {
    $stmt = $conn_rbe->prepare($sql_flecto);
    $stmt->execute($params);
    $results = array_merge($results, $stmt->fetchAll(PDO::FETCH_ASSOC));
}

// WATER
if ($source === 'WATER' || $source === 'ALL') {
    $stmt = $conn_rbe->prepare($sql_water);
    $stmt->execute($params);
    $results = array_merge($results, $stmt->fetchAll(PDO::FETCH_ASSOC));
}

echo json_encode($results, JSON_UNESCAPED_SLASHES);
