import csv
import random
import sys
from pathlib import Path

from .generator import generate_records, shuffle_records, sort_records, sample_existing, sample_nonexistent
from .bench import run_once 

N = 10000
RUNS = 5
SEARCH_K = 100
SEARCH_MISSING_K = 10
DELETE_K = 50
sys.setrecursionlimit(15000)
BASE_DIR = Path(__file__).resolve().parent.parent
RESULT_PATH = BASE_DIR / "results.csv"

def run_experiment(records, mode):
    search_names = sample_existing(records, SEARCH_K) + sample_nonexistent(SEARCH_MISSING_K)
    delete_names = sample_existing(records, DELETE_K)

    all_rows = []

    for run_i in range(1, RUNS + 1):
        print(f"  [{mode}] run {run_i}/{RUNS} ...")
        run_results = run_once(records, search_names, delete_names)
        for label, t_insert, t_find, t_delete in run_results:
            all_rows.append([label, mode, 'insert', run_i, round(t_insert, 6)])
            all_rows.append([label, mode, 'find',   run_i, round(t_find,   6)])
            all_rows.append([label, mode, 'delete', run_i, round(t_delete, 6)])

    return all_rows


def main_experiment():
    random.seed(52)

    records_base = generate_records(N)
    records_shuffled = shuffle_records(records_base)
    records_sorted = sort_records(records_base)

    rows = []
    rows += run_experiment(records_shuffled, 'shuffled')
    rows += run_experiment(records_sorted,   'sorted')

    header = ['structure', 'mode', 'operation', 'run', 'time_sec']
    output_path = RESULT_PATH

    with open(output_path, 'w', newline='') as f:
        writer = csv.writer(f)
        writer.writerow(header)
        writer.writerows(rows)

    print(f"Done. Results saved to {output_path.name}")
    print(f"Total rows: {len(rows)}")

if __name__ == "__main__":
    main_experiment()
