"""Architecture tests — module boundary enforcement."""
import importlib
import pkgutil

import pytest


def test_no_direct_journal_creation_outside_posting_engine():
    """ADR-010: JournalEntry creation must go through PostingEngine."""
    from app.services.posting_engine import PostingEngine

    assert hasattr(PostingEngine, "post_voucher")
    assert hasattr(PostingEngine, "reverse_voucher")


def test_all_models_have_tenant_id():
    from app.core.database import Base
    import app.models  # noqa: F401

    skip = {"alembic_version"}
    for table in Base.metadata.tables.values():
        if table.name in skip:
            continue
        assert "tenant_id" in table.columns, f"{table.name} missing tenant_id"


def test_permissions_defined():
    from app.permissions.definitions import ALL_PERMISSIONS

    assert "accounting.view" in ALL_PERMISSIONS
    assert "purchase_accounting.post" in ALL_PERMISSIONS
    assert "assets.depreciate" in ALL_PERMISSIONS
    assert "payroll.calculate" in ALL_PERMISSIONS
    assert "reports.view" in ALL_PERMISSIONS
    assert "audit.view" in ALL_PERMISSIONS


def test_events_defined():
    from app.events.types import AccountingEventType

    assert AccountingEventType.GOODS_RECEIVED.value == "goods.received"
    assert AccountingEventType.ASSET_DEPRECIATED.value == "asset.depreciated"
    assert AccountingEventType.PAYROLL_POSTED.value == "payroll.posted"
    assert AccountingEventType.BALANCE_SHEET_GENERATED.value == "balance_sheet.generated"
    assert AccountingEventType.AUDIT_RECORD_CREATED.value == "audit_record.created"
