<?php
declare(strict_types=1);

require_once __DIR__ . '/_bootstrap.php';

golder_sales_require_post();
$master = golder_master_pdo();
$user = golder_portal_api_require_login($master);
$userId = (int)($user['id'] ?? 0);

$input = golder_portal_api_payload();
$subject = trim((string)($input['subject'] ?? 'Bize ulas'));
$message = trim((string)($input['message'] ?? ''));
if ($message === '') {
    golder_json_error(400, 'INVALID_REQUEST', 'message is required.');
}

$res = golder_create_support_thread(
    $master,
    $userId,
    $subject,
    $message,
    trim((string)($user['full_name'] ?? $user['email'] ?? 'customer'))
);
if (empty($res['ok'])) {
    golder_json_error(400, 'INVALID_REQUEST', (string)($res['message'] ?? 'Support thread could not be created.'));
}

golder_json_response([
    'status' => 'ok',
    'thread_id' => (string)($res['thread_id'] ?? ''),
]);


