"""SuDirectAcquisitions — Beletage's own direct acquisitions (won)."""
from __future__ import annotations

from ..xml_utils import (
    find_child_local, find_local, text_under, text_direct,
    int_under, decimal_under, datetime_under, sysitem_name, to_jsonable,
)


def parse(el) -> dict | None:
    da_id = int_under(el, 'DirectAcquisitionId') or int_under(el, 'DirectAcquisitionID')
    if da_id is None:
        return None

    cpv_code = sysitem_name(el, 'CpvCode')
    return {
        'da_id': da_id,
        'da_name': text_under(el, 'DirectAcquisitionName'),
        'unique_identification_code': text_under(el, 'UniqueIdentificationCode'),
        'cpv_code': cpv_code,
        'cpv_name': cpv_code,                      # name == code in this WSDL
        'contract_type': sysitem_name(el, 'SysAcquisitionContractType'),
        'publication_date': datetime_under(el, 'PublicationDate'),
        'finalization_date': datetime_under(el, 'FinalizationDate'),
        'estimated_value': decimal_under(el, 'EstimatedValue') or decimal_under(el, 'EstimatedValueRon'),
        'closing_value': decimal_under(el, 'ClosingValue'),
        'currency': 'RON',
        'da_state': sysitem_name(el, 'SysDirectAcquisitionState'),
        'authority_id': int_under(el, 'ContractingAuthorityID'),
        'authority_name': text_under(el, 'ContractingAuthority'),
        'details': to_jsonable(el),
    }
