"""Small UI-facing choices, independent namespace, and current-proof packaging."""
import json
from pathlib import Path
import zipfile
import pytest
from pycforge_sdk import SDK


def test_array_cards_are_small_and_offline_readable(sdk,tmp_path):
    result=sdk.templates();assert result.ok,result.to_dict()
    cards=result.data['templates']
    assert {'numpy-array','array-starter'} <= {c['id'] for c in cards}
    assert all(set(c)=={'id','title','description','declaration_schema',
                        'identity','exports','customization_available'} for c in cards)
    assert all(set(c['identity'])=={'package_id','namespace','version'} for c in cards)
    assert all(c['customization_available'] is True for c in cards)
    card=sdk.describe('ArrayFunction');assert card.ok,card.to_dict()
    assert [p['name'] for p in card.data['method']['parameters']]==['operation_id','export','operation','summary']
    operation_ids = [op['id'] for op in card.data['array_operations']]
    workbench = sdk.workbench().data['workbench']
    array = next(p for p in workbench['profiles'] if 'array' in p['declaration_schema'])
    field = next(f for f in array['function_fields'] if f['name'] == 'operation')
    assert operation_ids == [op['value'] for op in field['options']]
    assert {'zeros', 'concatenate', 'sqrt'} <= set(operation_ids)
    offline=SDK(engine_python=tmp_path/'missing-python',cache=tmp_path/'cache')
    assert offline.connect().status=='incomplete'
    assert offline.templates().data['templates']==cards
    assert offline.describe('ArrayFunction').data['method']==card.data['method']


@pytest.mark.parametrize('template',['numpy-array','array-starter'])
def test_array_projects_verify_and_build_deterministically(sdk,tmp_path,template):
    folder=tmp_path/template
    created=sdk.create_project(folder,author='Independent array contract fixture',template=template)
    assert created.ok,created.to_dict()
    assert sdk.scan(folder).ok
    checked=sdk.verify(folder,compiler='gcc');assert checked.ok,checked.to_dict()
    package=tmp_path/'array.pcfplugin';second=tmp_path/'second.pcfplugin'
    built=sdk.build(folder,package);assert built.ok,built.to_dict()
    assert sdk.build(folder,second).ok and package.read_bytes()==second.read_bytes()
    with zipfile.ZipFile(package) as archive:
        manifest=json.loads(archive.read('manifest.json'))
        declared = sdk.inspect(folder).data['inspection']['extension']
        assert manifest['profile'] == declared['profile']
        assert manifest['extension_api']=='0.4'
        assert set(archive.namelist())=={'manifest.json','conversion.json','documentation.json'}
    assert sdk.register(package,store=tmp_path/'registry.json').ok
    previous=package.read_bytes()
    project=folder/'project.json';data=json.loads(project.read_text())
    data['declaration']['functions'][0]['summary']='Changed after verification'
    project.write_text(json.dumps(data))
    refused=sdk.build(folder,package)
    assert not refused.ok and refused.diagnostics[0]['code']=='SDK_STALE_EVIDENCE'
    assert package.read_bytes()==previous


@pytest.mark.parametrize('field,value',[('ownership','borrowed'),('native_symbol','malloc'),('rank',2)])
def test_array_field_rejection_is_structured(sdk,tmp_path,field,value):
    folder=tmp_path/'array'
    assert sdk.create_project(folder,author='Boundary fixture',template='numpy-array').ok
    project=folder/'project.json';data=json.loads(project.read_text())
    data['declaration']['functions'][0][field]=value
    project.write_text(json.dumps(data))
    result=sdk.scan(folder)
    assert not result.ok
    assert result.diagnostics or result.data.get('scan',{}).get('issues')
