#!/bin/bash

# Arguments: script_dir working_dir data_dir train_count validate_count margin validate_iterations
SCRIPT_DIR=${1:-"script"}
WORKING_DIR=${2:-"."}
DATA_DIR=${3:-"datas"}
TRAIN_COUNT=${4:-100}
VAL_COUNT=${5:-20}
MARGIN=${6:-0.5}
VAL_ITERATIONS=${7:-5}

# 작업 디렉토리가 없으면 생성
if [ ! -d "$WORKING_DIR" ]; then
    echo "Creating working directory: $WORKING_DIR"
    mkdir -p "$WORKING_DIR"
fi

# factors.json이 없으면 생성
if [ ! -f "$WORKING_DIR/factors.json" ]; then
    echo "Factors file not found. Running factorization..."
    bun run "$SCRIPT_DIR/extract.ts" "$WORKING_DIR" "$DATA_DIR"
fi

echo "Starting training..."
python3 "$SCRIPT_DIR/train.py" "$SCRIPT_DIR" "$WORKING_DIR" "$DATA_DIR" "$TRAIN_COUNT" "$VAL_COUNT" "$MARGIN" "$VAL_ITERATIONS"
