from pathlib import Path
import json
import os

from pyucc.core import countings_impl as ci


def make_item_variant(key_map):
    # helper to create dict with varying keys
    return key_map


def test_map_pygount_json_item_variants():
    item1 = {
        "raw_total_lines": 10,
        "code": 7,
        "comment": 2,
        "blank": 1,
        "language": "Python",
        "filename": "a.py",
    }
    res1 = ci._map_pygount_json_item(item1)
    assert res1['physical_lines'] == 10
    assert res1['code_lines'] == 7
    assert res1['comment_lines'] == 2
    assert res1['blank_lines'] == 1
    assert res1['language'] == 'Python'
    assert res1['file'].endswith('a.py')

    # variant with different key names
    item2 = {
        "n_lines": 4,
        "n_code": 3,
        "n_comment": 1,
        "n_blank": 0,
        "lang": "C",
        "file": "b.c",
    }
    res2 = ci._map_pygount_json_item(item2)
    assert res2['physical_lines'] == 4
    assert res2['code_lines'] == 3
    assert res2['comment_lines'] == 1
    assert res2['blank_lines'] == 0
    assert res2['language'] == 'C'


def test_analyze_file_counts_fallback(tmp_path):
    p = tmp_path / "sample.txt"
    data = "line1\n\nline3\nline4\n\n"
    p.write_text(data)
    res = ci.analyze_file_counts(p)
    assert res['physical_lines'] == 5
    assert res['blank_lines'] == 2
    assert res['code_lines'] == 3


def test_analyze_paths_with_missing(tmp_path):
    files = [tmp_path / "nope.py", tmp_path / "also.py"]
    # none exist
    out = ci.analyze_paths(files)
    assert isinstance(out, list)
    assert len(out) == 2
    assert 'error' in out[0] or 'error' in out[1]
