#!/usr/bin/env python3
"""
Сопоставление Excel-выгрузки с данными API.
Обогащает Excel-записи file_ids и has_analysis из API.
Результат: data/matched_records.json + отчёт о расхождениях.

Использование:
    python3 match.py
"""

import json
import os
from collections import defaultdict


def load(path: str) -> list:
    with open(path, encoding='utf-8') as f:
        return json.load(f)


def normalize_id(val) -> str:
    return str(val).strip()


def main():
    excel_path = 'data/excel_records.json'
    api_path   = 'data/api_records.json'

    if not os.path.exists(excel_path):
        print(f'❌ Не найден {excel_path} — запусти parse_excel.py')
        return
    if not os.path.exists(api_path):
        print(f'❌ Не найден {api_path} — запусти fetch_api.py')
        return

    excel_records = load(excel_path)
    api_records   = load(api_path)

    # Индекс API по element_id
    api_by_id = {normalize_id(r['element_id']): r for r in api_records}

    print(f'Excel записей: {len(excel_records)}')
    print(f'API записей:   {len(api_records)}')
    print()

    matched    = []
    only_excel = []   # есть в Excel, нет в API (вне периода или удалены)
    only_api   = []   # есть в API, нет в Excel

    excel_ids = set()

    for ex in excel_records:
        eid = normalize_id(ex['element_id'])
        excel_ids.add(eid)
        api = api_by_id.get(eid)

        if api:
            # Обогащаем Excel-запись данными из API
            merged = {**ex}
            merged['file_ids']      = api['file_ids']
            merged['file_count']    = api['file_count']
            merged['has_analysis']  = api['has_analysis']
            merged['analysis_text'] = api.get('analysis_text', '')
            merged['source']        = 'matched'

            # Фиксируем расхождения
            diffs = []
            if ex['contractor'] and api['contractor'] and ex['contractor'] != api['contractor']:
                diffs.append(f'contractor: excel={ex["contractor"]!r} api={api["contractor"]!r}')
            # Нормализуем статус перед сравнением
            ex_status  = ex['status'].replace('Статус не установлен', 'Не установлен').strip()
            api_status = api['status'].strip()
            if ex_status and api_status and ex_status != api_status:
                diffs.append(f'status: excel={ex["status"]!r} api={api["status"]!r}')
            if ex['amount'] and api['amount'] and ex['amount'].replace(' ','') != api['amount'].replace(' ',''):
                diffs.append(f'amount: excel={ex["amount"]!r} api={api["amount"]!r}')

            merged['diffs'] = diffs
            matched.append(merged)
        else:
            only_excel.append(ex)

    for api in api_records:
        eid = normalize_id(api['element_id'])
        if eid not in excel_ids:
            only_api.append(api)

    # --- Отчёт ---
    print(f'✅ Совпало:           {len(matched)}')
    print(f'⚠️  Только в Excel:   {len(only_excel)}')
    print(f'⚠️  Только в API:     {len(only_api)}')
    print()

    with_diffs = [r for r in matched if r['diffs']]
    if with_diffs:
        print(f'🔍 Расхождения в данных ({len(with_diffs)} записей):')
        for r in with_diffs:
            print(f'  [{r["element_id"]}] {r["name"][:50]}')
            for d in r['diffs']:
                print(f'    ↳ {d}')
        print()

    if only_excel:
        print('📋 Только в Excel (нет в API-периоде):')
        for r in only_excel[:10]:
            print(f'  [{r["element_id"]}] {r["date_create"]} | {r["contractor"]} | {r["name"][:40]}')
        print()

    if only_api:
        print('📋 Только в API (нет в Excel):')
        for r in only_api[:10]:
            print(f'  [{r["element_id"]}] {r["date_create"]} | {r["contractor"]} | {r["name"][:40]}')
        print()

    # Статистика по matched
    if matched:
        print('📊 Matched-записи:')
        print(f'  С файлами:       {sum(1 for r in matched if r["file_count"] > 0)}')
        print(f'  С анализом:      {sum(1 for r in matched if r["has_analysis"])}')
        print(f'  С реф. договора: {sum(1 for r in matched if r["contract_refs"])}')
        print(f'  Без типа:        {sum(1 for r in matched if not r["doc_types"])}')

        from collections import Counter
        ct = Counter(t for r in matched for t in r['doc_types'])
        print('\n  Типы документов:')
        for t, n in ct.most_common():
            print(f'    {n:3}x  {t}')

    # Сохраняем результат
    os.makedirs('data', exist_ok=True)
    result = {
        'matched':    matched,
        'only_excel': only_excel,
        'only_api':   only_api,
    }
    out_path = 'data/matched_records.json'
    with open(out_path, 'w', encoding='utf-8') as f:
        json.dump(result, f, ensure_ascii=False, indent=2)

    print(f'\n✅ Сохранено: {out_path}')
    print(f'   matched: {len(matched)}, only_excel: {len(only_excel)}, only_api: {len(only_api)}')


if __name__ == '__main__':
    main()
