<?php
/**
 * API: عرض هيكل جدول inspection_requests_new
 * GET /api/supervisor/endpoints/get_table_structure.php
 */

header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');

// إعدادات الاتصال بقاعدة البيانات
$host = "localhost";
$dbname = "digitals_autotester_db";
$username = "digitals";
$password = "bassam.bassam";

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // جلب هيكل الجدول
    $query = "DESCRIBE inspection_requests_new";
    $stmt = $pdo->prepare($query);
    $stmt->execute();
    $columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    // جلب عينة من البيانات
    $sampleQuery = "SELECT * FROM inspection_requests_new LIMIT 1";
    $sampleStmt = $pdo->prepare($sampleQuery);
    $sampleStmt->execute();
    $sampleData = $sampleStmt->fetch(PDO::FETCH_ASSOC);
    
    echo json_encode([
        'success' => true,
        'columns' => $columns,
        'sample_data' => $sampleData,
        'column_names' => array_column($columns, 'Field')
    ], JSON_UNESCAPED_UNICODE);
    
} catch(PDOException $e) {
    echo json_encode([
        'success' => false,
        'message' => 'Database error: ' . $e->getMessage()
    ], JSON_UNESCAPED_UNICODE);
}
?>