from pathlib import Path
from pyucc.config import profiles as profiles_cfg


def test_profiles_crud(tmp_path):
    pfile = tmp_path / "profiles.json"
    profiles = [
        {"name": "p1", "paths": ["C:/p1"], "languages": ["Python"], "ignore": []}
    ]
    profiles_cfg.save_profiles(profiles, path=pfile)
    loaded = profiles_cfg.load_profiles(path=pfile)
    assert isinstance(loaded, list) and loaded[0]['name'] == 'p1'

    found = profiles_cfg.find_profile('p1', path=pfile)
    assert found is not None and found['paths'][0] == 'C:/p1'

    # update
    profiles_cfg.add_or_update_profile({"name": "p1", "paths": ["D:/p1"], "languages": [], "ignore": []}, path=pfile)
    found2 = profiles_cfg.find_profile('p1', path=pfile)
    assert found2['paths'][0] == 'D:/p1'

    # add new
    profiles_cfg.add_or_update_profile({"name": "p2", "paths": ["E:/p2"], "languages": [], "ignore": []}, path=pfile)
    allp = profiles_cfg.load_profiles(path=pfile)
    assert any(p.get('name') == 'p2' for p in allp)

    # delete
    profiles_cfg.delete_profile('p1', path=pfile)
    after = profiles_cfg.load_profiles(path=pfile)
    assert not any(p.get('name') == 'p1' for p in after)
