#!/bin/bash
# Generate test data files for freneng tests
# This script creates the test files needed for freneng's test suite
# Test data is generated in the crate root at: freneng/test_data/

set -e

# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Script is in tests/, test_data is at crate root level
# Path: tests/ -> .. -> crate root -> test_data
TEST_DATA_DIR="$SCRIPT_DIR/../test_data"

# Create test_data directory structure
mkdir -p "$TEST_DATA_DIR"
mkdir -p "$TEST_DATA_DIR/Photos"
mkdir -p "$TEST_DATA_DIR/Photos/Vacation"
mkdir -p "$TEST_DATA_DIR/Photos/Vacation/2024"
mkdir -p "$TEST_DATA_DIR/Documents"
mkdir -p "$TEST_DATA_DIR/Documents/Projects"
mkdir -p "$TEST_DATA_DIR/Documents/Archive"
mkdir -p "$TEST_DATA_DIR/Media"
mkdir -p "$TEST_DATA_DIR/Media/Music"
mkdir -p "$TEST_DATA_DIR/Media/Videos"
mkdir -p "$TEST_DATA_DIR/Backups"
mkdir -p "$TEST_DATA_DIR/Backups/2024"
mkdir -p "$TEST_DATA_DIR/Backups/2024/January"
mkdir -p "$TEST_DATA_DIR/Backups/2024/February"
mkdir -p "$TEST_DATA_DIR/Logs"
mkdir -p "$TEST_DATA_DIR/Logs/Application"
mkdir -p "$TEST_DATA_DIR/Logs/System"
mkdir -p "$TEST_DATA_DIR/Temp"
mkdir -p "$TEST_DATA_DIR/Config"
mkdir -p "$TEST_DATA_DIR/Config/Fonts"
mkdir -p "$TEST_DATA_DIR/Config/Themes"

echo "Generating test data files for freneng in $TEST_DATA_DIR..."

# Helper function to create a minimal valid JPEG file
create_jpeg() {
    local filename="$1"
    # Create a minimal valid JPEG (just the header)
    printf '\xFF\xD8\xFF\xE0\x00\x10JFIF\x00\x01\x01\x01\x00H\x00H\x00\x00\xFF\xD9' > "$filename"
}

# Helper function to create a minimal valid PNG file
create_png() {
    local filename="$1"
    # Create a minimal valid PNG (just the header)
    printf '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\tpHYs\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a\x9c\x18\x00\x00\x00\nIDATx\x9cc\x00\x01\x00\x00\x05\x00\x01\r\n-\xdb\x00\x00\x00\x00IEND\xaeB`\x82' > "$filename"
}

# Helper function to create a minimal valid PDF file
create_pdf() {
    local filename="$1"
    # Create a minimal valid PDF
    echo "%PDF-1.4
1 0 obj
<< /Type /Catalog /Pages 2 0 R >>
endobj
2 0 obj
<< /Type /Pages /Kids [3 0 R] /Count 1 >>
endobj
3 0 obj
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>
endobj
xref
0 4
0000000000 65535 f 
0000000009 00000 n 
0000000058 00000 n 
0000000115 00000 n 
trailer
<< /Size 4 /Root 1 0 R >>
startxref
178
%%EOF" > "$filename"
}

# Root level files
create_jpeg "$TEST_DATA_DIR/photo_001.jpg"
create_jpeg "$TEST_DATA_DIR/photo_002.jpg"
echo "Document content" > "$TEST_DATA_DIR/document.txt"
echo "Readme content" > "$TEST_DATA_DIR/README.md"

# Photos directory structure
create_jpeg "$TEST_DATA_DIR/Photos/IMG_001.jpg"
create_jpeg "$TEST_DATA_DIR/Photos/IMG_002.jpg"
create_jpeg "$TEST_DATA_DIR/Photos/IMG_003.jpg"
create_png "$TEST_DATA_DIR/Photos/screenshot.png"
create_png "$TEST_DATA_DIR/Photos/thumbnail.png"

# Photos/Vacation
create_jpeg "$TEST_DATA_DIR/Photos/Vacation/beach_001.jpg"
create_jpeg "$TEST_DATA_DIR/Photos/Vacation/beach_002.jpg"
create_jpeg "$TEST_DATA_DIR/Photos/Vacation/mountain_001.jpg"
create_jpeg "$TEST_DATA_DIR/Photos/Vacation/sunset.jpg"

# Photos/Vacation/2024
create_jpeg "$TEST_DATA_DIR/Photos/Vacation/2024/summer_001.jpg"
create_jpeg "$TEST_DATA_DIR/Photos/Vacation/2024/summer_002.jpg"
create_jpeg "$TEST_DATA_DIR/Photos/Vacation/2024/winter_001.jpg"

# Documents
create_pdf "$TEST_DATA_DIR/Documents/report.pdf"
create_pdf "$TEST_DATA_DIR/Documents/manual.pdf"
echo "Project notes" > "$TEST_DATA_DIR/Documents/notes.txt"
echo "Meeting minutes" > "$TEST_DATA_DIR/Documents/meeting.txt"

# Documents/Projects
echo "Project A documentation" > "$TEST_DATA_DIR/Documents/Projects/project_a.txt"
echo "Project B documentation" > "$TEST_DATA_DIR/Documents/Projects/project_b.txt"
create_pdf "$TEST_DATA_DIR/Documents/Projects/spec.pdf"

# Documents/Archive
echo "Archived document 1" > "$TEST_DATA_DIR/Documents/Archive/doc1.txt"
echo "Archived document 2" > "$TEST_DATA_DIR/Documents/Archive/doc2.txt"
create_pdf "$TEST_DATA_DIR/Documents/Archive/old_report.pdf"

# Media/Music
echo "Audio metadata" > "$TEST_DATA_DIR/Media/Music/song1.mp3"
echo "Audio metadata" > "$TEST_DATA_DIR/Media/Music/song2.mp3"
echo "Audio metadata" > "$TEST_DATA_DIR/Media/Music/album_cover.jpg"

# Media/Videos
echo "Video metadata" > "$TEST_DATA_DIR/Media/Videos/video1.mp4"
echo "Video metadata" > "$TEST_DATA_DIR/Media/Videos/video2.mp4"
create_png "$TEST_DATA_DIR/Media/Videos/thumbnail.png"

# Backups
echo "Backup data" > "$TEST_DATA_DIR/Backups/backup_001.dat"
echo "Backup data" > "$TEST_DATA_DIR/Backups/backup_002.dat"

# Backups/2024
echo "Backup data 2024" > "$TEST_DATA_DIR/Backups/2024/backup_2024_01.dat"
echo "Backup data 2024" > "$TEST_DATA_DIR/Backups/2024/backup_2024_02.dat"

# Backups/2024/January
echo "January backup" > "$TEST_DATA_DIR/Backups/2024/January/jan_backup_01.dat"
echo "January backup" > "$TEST_DATA_DIR/Backups/2024/January/jan_backup_02.dat"

# Backups/2024/February
echo "February backup" > "$TEST_DATA_DIR/Backups/2024/February/feb_backup_01.dat"
echo "February backup" > "$TEST_DATA_DIR/Backups/2024/February/feb_backup_02.dat"

# Logs
echo "Application log entry" > "$TEST_DATA_DIR/Logs/app.log"
echo "System log entry" > "$TEST_DATA_DIR/Logs/system.log"

# Logs/Application
echo "App log 1" > "$TEST_DATA_DIR/Logs/Application/app1.log"
echo "App log 2" > "$TEST_DATA_DIR/Logs/Application/app2.log"
echo "Error log" > "$TEST_DATA_DIR/Logs/Application/error.log"

# Logs/System
echo "System log 1" > "$TEST_DATA_DIR/Logs/System/sys1.log"
echo "System log 2" > "$TEST_DATA_DIR/Logs/System/sys2.log"
echo "Kernel log" > "$TEST_DATA_DIR/Logs/System/kernel.log"

# Temp
echo "Temporary file" > "$TEST_DATA_DIR/Temp/temp1.tmp"
echo "Temporary file" > "$TEST_DATA_DIR/Temp/temp2.tmp"
echo "Cache data" > "$TEST_DATA_DIR/Temp/cache.tmp"

# Config/Fonts
echo "Font data" > "$TEST_DATA_DIR/Config/Fonts/font1.ttf"
echo "Font data" > "$TEST_DATA_DIR/Config/Fonts/font2.ttf"
echo "Font data" > "$TEST_DATA_DIR/Config/Fonts/font3.otf"

# Config/Themes
echo "Theme config" > "$TEST_DATA_DIR/Config/Themes/theme1.json"
echo "Theme config" > "$TEST_DATA_DIR/Config/Themes/theme2.json"
echo "Theme config" > "$TEST_DATA_DIR/Config/Themes/dark.json"

echo ""
echo "Test data generation complete!"
echo "Files created in: $TEST_DATA_DIR"
echo ""
echo "Directory structure created:"
echo "  - Photos/ (with Vacation/2024 subdirectories)"
echo "  - Documents/ (with Projects and Archive subdirectories)"
echo "  - Media/ (with Music and Videos subdirectories)"
echo "  - Backups/ (with 2024/January and 2024/February subdirectories)"
echo "  - Logs/ (with Application and System subdirectories)"
echo "  - Temp/"
echo "  - Config/ (with Fonts and Themes subdirectories)"
echo ""
echo "Usage:"
echo "  Run this script before running tests: ./generate_test_data.sh"
echo "  Or add 'test_data' to .gitignore to exclude generated files from the repository."
