# -*- coding: utf-8 -*-
"""SOI_TRANG_WEB.PY — doi chieu THAM SO tren trang guilde/dcabot voi MA NGUON EA.

Cau hoi can tra loi: trang huong dan dang THIEU nhung module va tham so nao
so voi ban EA dang phat hanh?

SO THEO NHAN, KHONG SO THEO TEN BIEN. Ly do: trong MQL5, o Inputs cua MT5
khach KHONG nhin thay ten bien (InpMaxLotSize) ma nhin thay phan chu thich
cuoi dong ("9. Lot toi da cho 1 lenh"). Trang web cung viet theo nhan do.
Lan do dau tien toi so theo ten bien va ra "thieu 336/336" — con so vo
nghia, vi trang khong he dinh dung ten bien.

Cach doc trang: cac bang tham so KHONG nam trong dcabot.php ma vao bang 12
lenh include() tu intro_sections/accordion/. Ba tep dang bi chu thich
(Hidden) nen khong ra trang — tep nay danh dau ro.

Chay:  python soi_trang_web.py
"""
import io
import os
import re
import sys
import unicodedata

GOC_EA = r'G:/server tin hieu/systemai/dca/ea'
TEP_INPUT = os.path.join(GOC_EA, 'include/config/DCA_Inputs.mqh')
THU_MUC_ACC = (r'G:/Xampp/htdocs/public/pages/site/toolintroduce'
               r'/intro_sections/accordion')
TRANG_DCA = r'G:/Xampp/htdocs/public/pages/site/guilde/dcabot.php'

# Nhom noi bo — chu du an chot 23/09/2026 KHONG dua vao tai lieu khach.
# Thieu chung tren trang la DUNG, khong phai loi.
NHOM_NOI_BO = ('XVII.', 'XVIII.')


def gon(s):
    """Chuan hoa de so khop noi tay: bo dau, bo so thu tu dau dong, bo dau cau."""
    s = re.sub(r'^\s*\d+[.)]\s*', '', s or '')
    s = unicodedata.normalize('NFD', s.lower())
    s = ''.join(c for c in s if unicodedata.category(c) != 'Mn')
    s = s.replace('d', 'd')
    s = re.sub(r'[^a-z0-9 ]+', ' ', s)
    return re.sub(r'\s+', ' ', s).strip()


def doc_input_ea():
    """Tra list (ten_bien, nhan, nhom) cho ban INPUT_DEFAULT dang phat hanh."""
    van = io.open(TEP_INPUT, encoding='utf-8', errors='replace').read()
    ra = []
    nhom = '(khong nhom)'
    sau_ifdef = 0

    for dong in van.split('\n'):
        d = dong.strip()
        if d.startswith('#ifdef') or d.startswith('#ifndef'):
            if not (d.startswith('#ifdef') and 'INPUT_DEFAULT' in d):
                sau_ifdef += 1
            continue
        if d.startswith('#endif'):
            if sau_ifdef > 0:
                sau_ifdef -= 1
            continue
        if sau_ifdef > 0:
            continue

        m_nhom = re.search(r'input\s+group\s+"([^"]+)"', d)
        if m_nhom:
            nhom = m_nhom.group(1).strip().strip('= ').strip()
            continue

        m = re.match(r'input\s+[A-Za-z_][A-Za-z0-9_]*\s+([A-Za-z_][A-Za-z0-9_]*)', d)
        if m:
            nhan = ''
            m_nhan = re.search(r'//\s*(.+)$', d)
            if m_nhan:
                nhan = m_nhan.group(1).strip()
            ra.append((m.group(1), nhan, nhom))
    return ra


def doc_trang():
    """Tra (van_ban_cac_tep_dang_bat, tap_ten_tep_dang_tat, so_dong_theo_tep)."""
    tat = set()
    van_trang = io.open(TRANG_DCA, encoding='utf-8', errors='replace').read()
    for m in re.finditer(r"//\s*include\(\$introSec\s*\.\s*'([^']+)'", van_trang):
        tat.add(m.group(1))

    van_bat, theo_tep = [], {}
    for ten_tep in sorted(os.listdir(THU_MUC_ACC)):
        if not ten_tep.endswith('.php'):
            continue
        van = io.open(os.path.join(THU_MUC_ACC, ten_tep),
                      encoding='utf-8', errors='replace').read()
        theo_tep[ten_tep] = van
        if ten_tep not in tat:
            van_bat.append(van)
    return gon(' '.join(van_bat)), tat, theo_tep


def main():
    ea = doc_input_ea()
    van_bat_gon, dang_tat, theo_tep = doc_trang()

    print('=' * 76)
    print('DOI CHIEU THEO NHAN: trang /guilde/dcabot  vs  ma nguon EA phat hanh')
    print('=' * 76)

    print('--- 12 bang tham so, tep nao dang TAT ---')
    for ten_tep in sorted(theo_tep):
        print('  %-24s %s' % (ten_tep,
                              'TAT (Hidden)' if ten_tep in dang_tat else 'bat'))
    print()

    khach = [(t, nh, g) for t, nh, g in ea if not g.startswith(NHOM_NOI_BO)]
    noi_bo = len(ea) - len(khach)

    co, thieu = [], []
    for ten, nhan, nhom in khach:
        n = gon(nhan)
        if n and len(n) > 6 and n in van_bat_gon:
            co.append((ten, nhan, nhom))
        else:
            thieu.append((ten, nhan, nhom))

    print('Input ban phat hanh        : %d  (trong do %d thuoc nhom noi bo, bo qua)'
          % (len(ea), noi_bo))
    print('Input danh cho khach       : %d' % len(khach))
    print('  -> CO mo ta tren trang   : %d' % len(co))
    print('  -> KHONG thay tren trang : %d' % len(thieu))
    print()

    theo_nhom = {}
    for ten, nhan, nhom in thieu:
        theo_nhom.setdefault(nhom, []).append((ten, nhan))

    print('--- THIEU, gom theo nhom trong ma (%d nhom) ---' % len(theo_nhom))
    for nhom in sorted(theo_nhom, key=lambda k: -len(theo_nhom[k])):
        ds = theo_nhom[nhom]
        print()
        print('  [%2d thieu] %s' % (len(ds), nhom))
        for ten, nhan in ds[:6]:
            print('        %-28s %s' % (ten, nhan[:46]))
        if len(ds) > 6:
            print('        ... con %d input nua' % (len(ds) - 6))


if __name__ == '__main__':
    sys.exit(main())
