<?php // fetch_report_data.php
declare(strict_types=1);


$_REQUEST['tenant'] = 'clientcore';
$master_id='mr_682596f90f5d40.97081585';
require_once '../config.php';

// Use tenant connection for report data
/** @var mysqli $db */
$db = $con;

/**
 * Fetch and prepare all report data for a given master_report_id.
 * Uses the global $db connection from config.
 * @param string $master_id
 * @return array
 */
function getReportData(string $master_id): array {
    return [
        'sectionA' => getSectionA($master_id),
        'sectionB' => getSectionB($master_id),
        'sectionC' => getSectionC($master_id),
        'sectionD' => getSectionD($master_id),
        'sectionE' => getSectionE($master_id),
        'sectionF' => getSectionF($master_id),
        'sectionG' => getSectionG($master_id),
        'sectionH' => getSectionH($master_id),
        'sectionI' => getSectionI($master_id),
        'sectionJ' => getSectionJ($master_id),
        'sectionK' => getSectionK($master_id),
    ];
}

/**
 * Section A: Details of the Client
 * @param string $master_id
 * @return array{client_name:string, client_address:string}
 */
function getSectionA(string $master_id) {
    global $con;  // or $db, whichever your template uses

    $sql = '
      SELECT
        cd.clientname      AS client_name,
        CONCAT_WS("\n",
          cd.address1,
          cd.address2,
          cd.town,
          cd.county,
          cd.postcode
        )                  AS client_address
      FROM elec_report er
      JOIN clientdetails cd ON er.clientnumber = cd.clientnumber
      WHERE er.master_report_id = ?
      LIMIT 1
    ';
    $stmt = $con->prepare($sql);
    if (!$stmt) {
        throw new RuntimeException('MySQL prepare error (Section A): '.$con->error);
    }
    $stmt->bind_param('s', $master_id);
    $stmt->execute();
    $stmt->bind_result($clientName, $clientAddress);
    $stmt->fetch();
    $stmt->close();

    return [
        'client_name'    => $clientName    ?: '',
        'client_address' => $clientAddress ?: ''
    ];
}

/**
 * Section B: Reason and Dates
 * @param string $master_id
 * @return array{report_reason:string, inspection_dates:string}
 */
function getSectionB(string $master_id): array {
    global $db;
    $sql = 'SELECT reportreason AS report_reason,
               inspectiondates AS inspection_dates
            FROM elec_report
            WHERE master_report_id = ?
            LIMIT 1';

    $stmt = $db->prepare($sql);
    if (!$stmt) {
        throw new RuntimeException('MySQL prepare error (Section B): ' . $db->error);
    }
    $stmt->bind_param('s', $master_id);
    $stmt->execute();
    $stmt->bind_result($reason, $dates);
    $stmt->fetch();
    $stmt->close();

    return [
        'report_reason'    => $reason ?: '',
        'inspection_dates' => $dates ?: ''
    ];
}

// Stubs for Sections C through K
function getSectionC(string $master_id): array { return []; }
function getSectionD(string $master_id): array { return []; }
function getSectionE(string $master_id): array { return []; }
function getSectionF(string $master_id): array { return []; }
function getSectionG(string $master_id): array { return []; }
function getSectionH(string $master_id): array { return []; }
function getSectionI(string $master_id): array { return []; }
function getSectionJ(string $master_id): array { return []; }
function getSectionK(string $master_id): array { return []; }