// test_parallel_hash_large_dir.cpp - Verify parallel_hash handles >256 files
// Validates F3 fix: no truncation, correct combined hash for large directories

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "../dataset_hash/threading/parallel_hash.h"

// Create a test file with known content
static bool create_test_file(const char* path, int content_id) {
    int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd < 0) return false;
    
    // Write unique content based on content_id
    char buf[64];
    snprintf(buf, sizeof(buf), "Test file content %d\n", content_id);
    write(fd, buf, strlen(buf));
    close(fd);
    return true;
}

int main() {
    // Create a temporary directory
    char tmpdir[] = "/tmp/test_large_dir_XXXXXX";
    if (!mkdtemp(tmpdir)) {
        printf("FAIL: Could not create temp directory\n");
        return 1;
    }
    
    // Create 300 test files (more than old 256 limit)
    const int num_files = 300;
    for (int i = 0; i < num_files; i++) {
        char path[256];
        snprintf(path, sizeof(path), "%s/file_%04d.txt", tmpdir, i);
        if (!create_test_file(path, i)) {
            printf("FAIL: Could not create test file %d\n", i);
            return 1;
        }
    }
    
    printf("Created %d test files in %s\n", num_files, tmpdir);
    
    // Initialize parallel hasher
    ParallelHasher hasher;
    if (!parallel_hasher_init(&hasher, 4, 64*1024)) {
        printf("FAIL: Could not initialize parallel hasher\n");
        return 1;
    }
    
    // Hash the directory
    char combined_hash[65];
    int result = parallel_hash_directory(&hasher, tmpdir, combined_hash);
    
    if (result != 0) {
        printf("FAIL: parallel_hash_directory returned %d\n", result);
        parallel_hasher_cleanup(&hasher);
        return 1;
    }
    
    printf("Combined hash: %s\n", combined_hash);
    
    // Verify hash is valid (64 hex chars)
    if (strlen(combined_hash) != 64) {
        printf("FAIL: Hash length is %zu, expected 64\n", strlen(combined_hash));
        parallel_hasher_cleanup(&hasher);
        return 1;
    }
    
    for (int i = 0; i < 64; i++) {
        char c = combined_hash[i];
        if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) {
            printf("FAIL: Invalid hex char '%c' at position %d\n", c, i);
            parallel_hasher_cleanup(&hasher);
            return 1;
        }
    }
    
    // Cleanup
    parallel_hasher_cleanup(&hasher);
    
    // Remove test files
    for (int i = 0; i < num_files; i++) {
        char path[256];
        snprintf(path, sizeof(path), "%s/file_%04d.txt", tmpdir, i);
        unlink(path);
    }
    rmdir(tmpdir);
    
    printf("PASS: parallel_hash handles %d files without truncation (F3 fix verified)\n", num_files);
    return 0;
}
