# testcheck - Regression test formatted output library
# Provide PASS/FAIL counts, section headers, summary reports
import t, c
import stdio
from w32.win32console import SetConsoleOutputCP, SetConsoleCP

CP_UTF8: t.CDefine = 65001

_pass_count: t.CInt = 0
_fail_count: t.CInt = 0
_total_pass: t.CInt = 0
_total_fail: t.CInt = 0


def begin(name: str):
    """Start the test suite, print the header, and reset the current suite counter (total count remains the same)"""
    global _pass_count, _fail_count
    _pass_count = 0
    _fail_count = 0
    SetConsoleOutputCP(CP_UTF8)
    SetConsoleCP(CP_UTF8)
    stdio.printf("=== %s ===\n\n", name)


def section(name: str):
    """Print the section header"""
    stdio.printf("--- %s ---\n", name)


def ok(msg: str):
    """Print PASS and count"""
    global _pass_count
    _pass_count += 1
    stdio.printf("PASS: %s\n", msg)


def fail(msg: str):
    """Print FAIL and count"""
    global _fail_count
    _fail_count += 1
    stdio.printf("FAIL: %s\n", msg)


def check(cond: t.CInt, ok_msg: str, fail_msg: str):
    """Conditional check, automatically select PASS/FAIL"""
    if cond:
        ok(ok_msg)
    else:
        fail(fail_msg)


def info(msg: str):
    """Print information message (not counted in the PASS/FAIL messages)"""
    stdio.printf("%s\n", msg)


def end() -> t.CInt:
    """Print the current suite summary, accumulate to total, return the current suite fail count"""
    global _total_pass, _total_fail
    _total_pass += _pass_count
    _total_fail += _fail_count
    stdio.printf("\n--- Summary ---\n")
    stdio.printf("PASS: %d, FAIL: %d\n", _pass_count, _fail_count)
    return _fail_count


def summary() -> t.CInt:
    """Print the total summary of all suites, return the total fail count"""
    stdio.printf("\n=== Total Summary ===\n")
    stdio.printf("Total PASS: %d, Total FAIL: %d\n", _total_pass, _total_fail)
    return _total_fail
