from pathlib import Path
import json, re, sys
ROOT=Path(__file__).resolve().parents[2]
checks=[]
def require(path, pattern, label, regex=False):
    text=(ROOT/path).read_text(encoding='utf-8-sig')
    ok=bool(re.search(pattern,text,re.M|re.S)) if regex else pattern in text
    checks.append((ok,label))

require(Path('MASSystem/Application/Facade/MASApplicationWindowControls.ControlFactory.Data.vb'), 'Public Function AddListView', 'AddListView factory')
require(Path('MASSystem/Application/Facade/MASApplicationWindowControls.ControlFactory.Data.vb'), 'Public Function AddTreeView', 'AddTreeView factory')
require(Path('Component/Navigation/List/ListView/Core/MASListView.Api.vb'), 'Public Sub ClearItems()', 'ListView ClearItems')
require(Path('Component/Navigation/List/ListView/Core/MASListView.Api.vb'), 'If selectionChanged Then\n                RaiseEvent SelectedIndexChanged', 'ClearItems conditional selection event')
require(Path('Component/Navigation/List/ListView/Core/MASListViewItem.vb'), 'Return _kind = MASListViewItemKind.Normal AndAlso _enabled', 'List item selectable policy')
require(Path('Component/Navigation/List/ListView/Core/MASListViewItem.vb'), 'If columnIndex <= 0 Then', 'List item main-cell policy')
require(Path('Component/Navigation/List/ListView/Core/MASListViewColumn.vb'), 'Private _widthDip As Single = 120.0F', 'Column default width')
require(Path('Component/Navigation/List/ListView/Core/MASListViewColumn.vb'), 'Private _minWidthDip As Single = 60.0F', 'Column default minimum width')
require(Path('Component/Navigation/List/ListView/Selection/MASListView.Selection.vb'), 'Public Property SelectedIndex As Integer', 'ListView selection API')
require(Path('Component/Navigation/List/ListView/Core/MASListView.Api.vb'), 'If direction = MASListViewSortDirection.None Then\n                columnIndex = -1', 'Sort none normalization')
require(Path('Component/Navigation/Tree/Core/MASTreeNode.vb'), 'Private _isExpanded As Boolean = True', 'Tree node default expanded')
require(Path('Component/Navigation/Tree/Core/MASTreeNode.vb'), 'If Object.ReferenceEquals(node, Me) Then Return', 'Tree self-parent prevention')
require(Path('Component/Navigation/Tree/Core/MASTreeNode.vb'), 'If IsNodeInAncestorChain(node) Then Return', 'Tree cycle prevention')
require(Path('Component/Navigation/Tree/Core/MASTreeView.Api.vb'), 'Not ContainsNodeInTree(normalized)', 'Tree foreign selection normalization')
require(Path('Component/Navigation/Tree/Core/MASTreeView.Api.vb'), 'Public Sub SetIconResolver(resolver As IMASIconResolver,', 'Tree resolver ownership API')
require(Path('docs/reference/api/README.md'), 'list-and-tree-views.md', 'API reference link')

classification=json.loads((ROOT/'eng/tests/Nexamas.UI.PublicApiClassificationBaseline.json').read_text(encoding='utf-8-sig'))['classifications']
by_name={x['name']:x['category'] for x in classification}
for name in ('MASListView','MASListViewItem','MASListViewColumn','MASTreeView','MASTreeNode','MASTreeNodeCollection'):
    checks.append((by_name.get(name)=='Advanced', f'{name} classified Advanced'))

failed=[label for ok,label in checks if not ok]
for ok,label in checks:
    print(('PASS' if ok else 'FAIL')+': '+label)
print(f'Assertions: {len(checks)-len(failed)} passed, {len(failed)} failed')
if failed: sys.exit(1)
