<?php
// lib/generate_data_sync.php
// Run generate_data.sh synchronously (no-append) and return the generated JSON/description.
require __DIR__ . '/../auth.php';
mvlog_require_login();

$MVLOG_ROOT = dirname(__DIR__);
header('Content-Type: application/json; charset=utf-8');

$job = $_POST['job'] ?? '';
$max_frames = isset($_POST['max_frames']) ? intval($_POST['max_frames']) : 16;

if (!preg_match('/^[0-9]{8}[-_][A-Za-z0-9._-]+$/', $job)) {
    http_response_code(400);
    echo json_encode(['status'=>'error','message'=>'invalid job']);
    exit;
}

$jobdir = $MVLOG_ROOT . '/in-dir/' . $job;
if (!is_dir($jobdir)) {
    http_response_code(404);
    echo json_encode(['status'=>'error','message'=>'job not found']);
    exit;
}

// Read existing data.txt for Title and Location (case-insensitive aliases)
$data_file = $jobdir . '/data.txt';
$title = '';
$location = '';
if (is_file($data_file)) {
    $lines = file($data_file, FILE_IGNORE_NEW_LINES);
    $i = 0;
    while ($i < count($lines)) {
        $raw = $lines[$i];
        $line = trim($raw);
        if ($line === '' || str_starts_with($line, '#')) { $i++; continue; }
        if (!str_contains($line, ':')) { $i++; continue; }
        [$key, $value] = array_map('trim', explode(':', $line, 2));
        $low = strtolower($key);
        if ($value === '|' && in_array($low, ['description','desc','synopsis'], true)) {
            // Skip indented block
            $i++;
            while ($i < count($lines)) {
                $next = $lines[$i];
                if (trim($next) !== '' && $next[0] !== ' ' && $next[0] !== "\t") break;
                $i++;
            }
            continue;
        }
        if (in_array($low, ['title','name'], true) && $title === '') $title = $value;
        if (in_array($low, ['place','location','where'], true) && $location === '') $location = $value;
        $i++;
    }
}

// Run generator synchronously but in no-append mode so it doesn't modify data.txt directly.
$script = $MVLOG_ROOT . '/bin/generate_data.sh';
if (!is_file($script)) {
    http_response_code(500);
    echo json_encode(['status' => 'error', 'message' => 'generate_data.sh not found']);
    exit;
}

// Build command and execute. Use no-append flag as last argument.
$cmd = escapeshellcmd($script) . ' ' . escapeshellarg($jobdir) . ' ' . escapeshellarg($title) . ' ' . escapeshellarg($location) . ' ' . escapeshellarg((string)$max_frames) . ' ' . escapeshellarg('no-append') . ' 2>&1';

// Allow long-running
@set_time_limit(0);

exec($cmd, $out_lines, $rc);
$log = implode("\n", $out_lines);

// If gemini_generated.json exists, parse and return
$gfile = $jobdir . '/gemini_generated.json';
if (is_file($gfile)) {
    $raw = @file_get_contents($gfile);
    $json = @json_decode($raw, true);
    $description = '';
    $teaser = '';
    $tags = [];
    if (is_array($json)) {
        $description = isset($json['description']) ? (string)$json['description'] : '';
        $teaser = isset($json['teaser']) ? (string)$json['teaser'] : '';
        $tags = isset($json['tags']) && is_array($json['tags']) ? $json['tags'] : [];
    }
    echo json_encode([
        'status' => 'ok',
        'description' => $description,
        'teaser' => $teaser,
        'tags' => $tags,
        'raw_json' => $json,
        'log' => $log,
        'rc' => $rc,
    ], JSON_UNESCAPED_UNICODE);
    exit;
}

// No gemini file — return error and include log
http_response_code(500);
echo json_encode(['status'=>'error','message'=>'no gemini output','log'=>$log,'rc'=>$rc], JSON_UNESCAPED_UNICODE);
exit;
