#!/usr/bin/env python3
import re, pathlib, sys
root=pathlib.Path(__file__).resolve().parents[1]
php=(root/'app/enterprise_supply_chain_262.php').read_text(encoding='utf-8')
sql=(root/'database/migrations/244_core_2_6_2_strategic_live_pharmacy_sterile_or_supply.sql').read_text(encoding='utf-8')
pt=set(re.findall(r'CREATE TABLE IF NOT EXISTS\s+([a-zA-Z0-9_]+)',php,re.I))
st=set(re.findall(r'CREATE TABLE IF NOT EXISTS\s+([a-zA-Z0-9_]+)',sql,re.I))
expected={'enterprise_supply_catalog_items','enterprise_supply_locations','enterprise_supply_stock','enterprise_supply_lots','enterprise_sterile_set_definitions','enterprise_sterile_set_components','enterprise_sterile_set_instances','enterprise_case_supply_plans','enterprise_case_supply_plan_items','enterprise_supply_movements','enterprise_supply_alerts','enterprise_supply_orders','enterprise_supply_order_items','enterprise_patient_guide_answers'}
checks={'php_tables_expected':expected<=pt,'sql_tables_expected':expected<=st,'php_sql_tables_equal':pt==st,'table_count_14':len(pt)==14 and len(st)==14}
failed=[]
for k,v in checks.items():print(('PASS' if v else 'FAIL')+'\t'+k);failed += ([] if v else [k])
print(f'PHP_TABLES={len(pt)} SQL_TABLES={len(st)} FAILED={len(failed)}')
sys.exit(1 if failed else 0)
