<?php
// Savit API: Add a single research prompt manually
// Ported from api-add-prompt.php

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

use CAE\Lib\Database;

$project_id = $_POST['brand_id'] ?? null;
$prompt_text = trim($_POST['prompt_text'] ?? '');

if (!$project_id || empty($prompt_text)) {
    echo json_encode(['success' => false, 'error' => 'Project ID and Prompt Text are required']);
    exit();
}

try {
    $db = Database::getInstance();
    
    // Check for duplicates
    $dup = $db->fetch("SELECT id FROM brand_prompts WHERE brand_id = ? AND prompt_text = ?", [(int)$project_id, $prompt_text]);
    if ($dup) {
        echo json_encode(['success' => false, 'error' => 'This prompt already exists for this project']);
        exit();
    }

    $db->insert('brand_prompts', [
        'brand_id'    => (int)$project_id,
        'prompt_text' => $prompt_text,
        'ai_volume'   => 0
    ]);

    echo json_encode(['success' => true]);

} catch (Exception $e) {
    echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}
