#!/bin/bash

set -eo pipefail

## VIASH START
## VIASH END

# Source centralized test helpers
source "$meta_resources_dir/test_helpers.sh"

# Initialize test environment
setup_test_env

log "Starting tests for bedtools_multiinter"

####################################################################################################

log "Creating test data..."

# Create test BED files with overlapping intervals
cat > "$meta_temp_dir/file1.bed" << 'EOF'
chr1	100	200	feature1	100	+
chr1	300	400	feature2	200	-
chr2	150	250	feature3	150	+
EOF

cat > "$meta_temp_dir/file2.bed" << 'EOF'
chr1	150	250	feature4	100	+
chr1	350	450	feature5	300	-
chr2	100	200	feature6	250	+
EOF

cat > "$meta_temp_dir/file3.bed" << 'EOF'
chr1	180	280	feature7	50	+
chr1	320	420	feature8	150	+
chr2	120	220	feature9	200	-
EOF

# Create genome file for empty regions testing
cat > "$meta_temp_dir/genome.txt" << 'EOF'
chr1	1000
chr2	800
EOF

####################################################################################################

log "TEST 1: Basic multiinter functionality"
"$meta_executable" \
  --input "$meta_temp_dir/file1.bed" \
  --input "$meta_temp_dir/file2.bed" \
  --input "$meta_temp_dir/file3.bed" \
  --output "$meta_temp_dir/test1_output.bed"

check_file_exists "$meta_temp_dir/test1_output.bed" "basic multiinter output"
check_file_not_empty "$meta_temp_dir/test1_output.bed" "basic multiinter result"

# Verify that output contains intersection data from multiple files
# The output should have columns: chr, start, end, plus one column per input file (3 files = 3+ columns)
num_columns=$(head -1 "$meta_temp_dir/test1_output.bed" | awk '{print NF}')
if [ "$num_columns" -lt 6 ]; then  # chr, start, end + at least 3 file columns
  log "ERROR: Output should have at least 6 columns (chr, start, end + 3 file columns), found $num_columns"
  head -3 "$meta_temp_dir/test1_output.bed"
  exit 1
fi
log "✓ Output has correct number of columns ($num_columns) for 3 input files"

####################################################################################################

log "TEST 2: With header (simple test)"
"$meta_executable" \
  --input "$meta_temp_dir/file1.bed" \
  --input "$meta_temp_dir/file2.bed" \
  --input "$meta_temp_dir/file3.bed" \
  --header \
  --output "$meta_temp_dir/test2_output.bed"

check_file_exists "$meta_temp_dir/test2_output.bed" "multiinter output with header"
# bedtools multiinter uses 'chr' not 'chrom' in the header
check_file_contains "$meta_temp_dir/test2_output.bed" "chr" "header line"

####################################################################################################

log "TEST 2b: Multiple names with header - test parameter passing"
"$meta_executable" \
  --input "$meta_temp_dir/file1.bed" \
  --input "$meta_temp_dir/file2.bed" \
  --input "$meta_temp_dir/file3.bed" \
  --names "Sample_A" "Sample_B" "Sample_C" \
  --header \
  --output "$meta_temp_dir/test2b_output.bed"

check_file_exists "$meta_temp_dir/test2b_output.bed" "multiinter output with custom names"
check_file_not_empty "$meta_temp_dir/test2b_output.bed" "custom names result"

# Note: bedtools multiinter in this version doesn't actually put custom names in the header
# but we test that the parameter is accepted without error and produces output
log "✓ Component accepts names parameter and produces output (names may not appear in header in this bedtools version)"

####################################################################################################

log "TEST 3: Empty regions with genome file"
"$meta_executable" \
  --input "$meta_temp_dir/file1.bed" \
  --input "$meta_temp_dir/file2.bed" \
  --input "$meta_temp_dir/file3.bed" \
  --genome "$meta_temp_dir/genome.txt" \
  --empty \
  --output "$meta_temp_dir/test3_output.bed"

check_file_exists "$meta_temp_dir/test3_output.bed" "multiinter output with empty regions"
check_file_not_empty "$meta_temp_dir/test3_output.bed" "empty regions result"

####################################################################################################

log "TEST 4: Custom filler text"
"$meta_executable" \
  --input "$meta_temp_dir/file1.bed" \
  --input "$meta_temp_dir/file2.bed" \
  --input "$meta_temp_dir/file3.bed" \
  --filler "N/A" \
  --output "$meta_temp_dir/test4_output.bed"

check_file_exists "$meta_temp_dir/test4_output.bed" "multiinter output with custom filler"

####################################################################################################

log "TEST 5: Clustering algorithm"
"$meta_executable" \
  --input "$meta_temp_dir/file1.bed" \
  --input "$meta_temp_dir/file2.bed" \
  --input "$meta_temp_dir/file3.bed" \
  --cluster \
  --output "$meta_temp_dir/test5_output.bed"

check_file_exists "$meta_temp_dir/test5_output.bed" "multiinter output with clustering"

####################################################################################################

log "TEST 6: Two input files only - verify multiple inputs work with different counts"
"$meta_executable" \
  --input "$meta_temp_dir/file1.bed" \
  --input "$meta_temp_dir/file2.bed" \
  --names "Dataset1" "Dataset2" \
  --header \
  --output "$meta_temp_dir/test6_output.bed"

check_file_exists "$meta_temp_dir/test6_output.bed" "multiinter output with 2 files"
check_file_not_empty "$meta_temp_dir/test6_output.bed" "two-file result"

# Verify output has correct columns for 2 files (chr, start, end + additional columns)
num_columns_2files=$(head -1 "$meta_temp_dir/test6_output.bed" | awk '{print NF}')
if [ "$num_columns_2files" -lt 5 ]; then
  log "ERROR: Output for 2 files should have at least 5 columns, found $num_columns_2files"
  head -1 "$meta_temp_dir/test6_output.bed"
  exit 1
fi
log "✓ Two-file input works correctly with $num_columns_2files columns"

####################################################################################################

log "✓ All tests completed successfully!"
