// test_storage_init_new_dir.cpp - Verify storage_init works on non-existent directories
// Validates F1 fix: storage_init should succeed when queue_dir doesn't exist yet

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <sys/stat.h>
#include "../queue_index/storage/index_storage.h"

int main() {
    // Create a temporary base directory
    char tmpdir[] = "/tmp/test_init_XXXXXX";
    if (!mkdtemp(tmpdir)) {
        printf("FAIL: Could not create temp directory\n");
        return 1;
    }
    
    // Create a path for a non-existent queue directory
    char queue_dir[256];
    snprintf(queue_dir, sizeof(queue_dir), "%s/new_queue_dir", tmpdir);
    
    // Verify the directory doesn't exist
    struct stat st;
    if (stat(queue_dir, &st) == 0) {
        printf("FAIL: Queue directory already exists\n");
        return 1;
    }
    
    // Try to init storage - this should succeed (F1 fix)
    IndexStorage storage;
    bool result = storage_init(&storage, queue_dir);
    
    if (!result) {
        printf("FAIL: storage_init failed on non-existent directory\n");
        return 1;
    }
    
    // Verify the index_path ends with expected suffix (macOS may add /private prefix)
    char expected_suffix[256];
    snprintf(expected_suffix, sizeof(expected_suffix), "%s/new_queue_dir/index.bin", tmpdir);
    
    // On macOS, canonicalized paths may have /private prefix
    const char* actual_path = storage.index_path;
    const char* expected_path = expected_suffix;
    
    // Check if paths match (accounting for /private prefix on macOS)
    if (strstr(actual_path, expected_path) == nullptr &&
        strcmp(actual_path + (strncmp(actual_path, "/private", 8) == 0 ? 8 : 0), 
               expected_path + (strncmp(expected_path, "/private", 8) == 0 ? 8 : 0)) != 0) {
        printf("FAIL: index_path mismatch\n");
        printf("  Expected suffix: %s\n", expected_suffix);
        printf("  Got:           %s\n", storage.index_path);
        return 1;
    }
    
    // Now try to open - this should create the directory and file
    result = storage_open(&storage);
    if (!result) {
        printf("FAIL: storage_open failed\n");
        return 1;
    }
    
    // Verify the directory now exists
    if (stat(queue_dir, &st) != 0 || !S_ISDIR(st.st_mode)) {
        printf("FAIL: Queue directory was not created\n");
        return 1;
    }
    
    // Verify the index file exists
    if (stat(storage.index_path, &st) != 0) {
        printf("FAIL: Index file was not created\n");
        return 1;
    }
    
    // Cleanup
    storage_close(&storage);
    storage_cleanup(&storage);
    unlink(storage.index_path);
    rmdir(queue_dir);
    rmdir(tmpdir);
    
    printf("PASS: storage_init works on non-existent directories (F1 fix verified)\n");
    return 0;
}
