#!/bin/bash

set -e
set -x

log() {
    echo "$(date "+%Y-%m-%d %H:%M:%S") $1" | tee -a test.log
}

# Create test directory structure
log "==> Creating test structure"
TEST_DIR="$HOME/test-archive"
MOCK_UPSTREAM="/srv/mock-upstream"  # Mock the remote archive
mkdir -p "$TEST_DIR"
mkdir -p "$TEST_DIR/repos"
mkdir -p "$TEST_DIR/packages"
mkdir -p "$MOCK_UPSTREAM/core/os/x86_64"
mkdir -p "$MOCK_UPSTREAM/extra/os/x86_64"

# Create some test packages in mock upstream
log "==> Creating test packages"
echo "test package 1" > "$MOCK_UPSTREAM/core/os/x86_64/test-1.0-1-x86_64.pkg.tar.zst"
echo "test package 2" > "$MOCK_UPSTREAM/extra/os/x86_64/test-2.0-1-x86_64.pkg.tar.zst"

# Update test config to use local path instead of rsync
log "==> Writing test config"
cat > /tmp/archive.conf << EOF
ARCHIVE_RSYNC="$MOCK_UPSTREAM/"
ARCHIVE_DIR="$TEST_DIR"
ARCHIVE_USER=root
ARCHIVE_GROUP=root
PKGREGEX='\.pkg\.tar(\.(gz|bz2|xz|zst|lrz|lzo|Z|lz4|lz))?'
PKGSIGREGEX="\$PKGREGEX\.sig"
UMASK=022
ARCHIVE_REPO=1
REPO_DAYLY=1
REPO_PACKAGES=1
REPO_PACKAGES_INDEX=1
REPO_PACKAGES_FULL_SEARCH=0
REPO_RSYNC_TIMEOUT=60
ARCHIVE_ISO=0
EOF

export ARCHIVE_CONFIG=/tmp/archive.conf

# Wrap archive.sh to add logging
run_archive() {
    log "  -> Starting archive.sh"
    log "  -> Current directory: $(pwd)"
    log "  -> Contents of MOCK_UPSTREAM:"
    ls -la "$MOCK_UPSTREAM"
    log "  -> Contents of TEST_DIR:"
    ls -la "$TEST_DIR"
    
    # Run archive.sh with debug output
    DEBUG=1 ./archive.sh 2>&1 | while IFS= read -r line; do
        log "    $line"
    done
    
    local status=${PIPESTATUS[0]}
    log "  -> archive.sh finished with status $status"
    return $status
}

# Test Suite
run_tests() {
    log "==> Starting test suite"
    log "==> Using test directory: $TEST_DIR"
    log "==> Using config: $ARCHIVE_CONFIG"
    log "==> Contents of config:"
    cat "$ARCHIVE_CONFIG"

    log "==> Test 1: Basic archive sync"
    run_archive || {
        log "ERROR: Basic archive sync failed"
        exit 1
    }
    
    log "==> Test 2: Verify archive structure"
    test -d "$TEST_DIR/repos" || {
        log "ERROR: repos directory missing"
        exit 1
    }
    test -d "$TEST_DIR/packages" || {
        log "ERROR: packages directory missing"
        exit 1
    }
    
    log "==> Test 3: Test archive-cleaner"
    ./archive-cleaner --repo "$TEST_DIR/repos/last" --archive "$TEST_DIR" --keep-years 1 || {
        log "ERROR: archive-cleaner failed"
        exit 1
    }
    
    log "==> Test 4: Test incremental sync"
    echo "new package" > "$MOCK_UPSTREAM/core/os/x86_64/test-2.0-1-x86_64.pkg.tar.zst"
    run_archive || {
        log "ERROR: Incremental sync failed"
        exit 1
    }
    
    log "==> Test 5: Test hardlinking"
    find "$TEST_DIR" -type f -links +1 || {
        log "ERROR: No hardlinks found"
        exit 1
    }

    log "==> Final archive structure:"
    find "$TEST_DIR" -type f -exec ls -l {} \;
}

# Run tests
run_tests

log "==> All tests passed!"
