<?php
/**
 * MJPOS -> MJ ERP Hub: Suppliers Directory Endpoint
 * Drop at: api/erp/suppliers-list.php inside MJPOS project root
 *
 * GET /api/erp/suppliers-list.php
 * Returns ALL active suppliers (full directory, not just non-zero balances -
 * that's what credit-book.php is for; this is the master supplier list).
 *
 * Response:
 * { "status": "success", "data": [
 *     { "id": 19, "supplier_name": "M.R.M.ASKAR", "contact_person": null, "phone": "0772329121",
 *       "email": null, "address": "162/6 AWISSAWELLA ROAD, KOTIYAKUMBURA", "current_balance": 111320.63,
 *       "status": "active" }
 * ]}
 */

require_once __DIR__ . '/erp_auth.php';
require_once __DIR__ . '/../../mjpos/config/config.php';

erp_require_auth();

try {
    // Confirmed against MJPOS schema: `suppliers` table
    $stmt = erp_pdo()->query(
        "SELECT id, supplier_name, contact_person, phone, email, address, current_balance, status
         FROM suppliers
         WHERE status = 'active'
         ORDER BY supplier_name"
    );
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

    erp_json_success($rows);

} catch (Exception $e) {
    erp_json_error('Query failed: ' . $e->getMessage(), 500);
}
