"""SuContracts — Beletage's own contracts (writes to seap.beletage_contracts)."""
from __future__ import annotations

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


def parse(el) -> dict | None:
    contract_id = int_under(el, 'ContractId')
    if contract_id is None:
        return None

    # Top-level fields directly under ContractItem
    contract_no = text_under(el, 'ContractNo')
    contract_title = text_under(el, 'ContractTitle')
    contract_value = decimal_under(el, 'ContractValue')
    default_currency_value = decimal_under(el, 'DefaultCurrencyContractValue')
    awarding_date = datetime_under(el, 'ContractAwardingDate')
    contract_date = datetime_under(el, 'ContractDate')
    publication_date = datetime_under(el, 'PublicationDate')
    duration_months = int_under(el, 'MonthsContractDuration')
    is_current = bool_under(el, 'IsCurrentVersion')
    is_rejected = bool_under(el, 'IsRejected')
    version_no = int_under(el, 'VersionNo')
    version_date = datetime_under(el, 'VersionDate')
    justification = text_under(el, 'Justification')
    additional_info = text_under(el, 'AdditionalInformation')
    contract_phase = sysitem_name(el, 'SysContractPhase')
    contract_state = sysitem_name(el, 'SysContractState')
    contract_type = sysitem_name(el, 'SysContractType')
    currency = sysitem_name(el, 'ContractValueCurrency')

    # CA Notice details: nested under <CANotice><General>...</General></CANotice>
    ca_notice = find_child_local(el, 'CANotice')
    ca_notice_id = None
    ca_notice_no = None
    authority_name = None
    authority_cui = None

    if ca_notice is not None:
        general = find_child_local(ca_notice, 'General')
        if general is not None:
            ca_notice_id = int_under(general, 'CaNoticeId')
            ca_notice_no = text_under(general, 'NoticeNo')

        section1 = find_child_local(ca_notice, 'Section1')
        if section1 is not None:
            auth_addresses = find_local(section1, 'CaAddresses')
            if auth_addresses is not None:
                auth_info = find_local(auth_addresses, 'EntityInformation')
                if auth_info is not None:
                    authority_name = text_direct(auth_info, 'Name')
                    authority_cui = text_direct(auth_info, 'Cif')

    return {
        'contract_id': contract_id,
        'contract_no': contract_no,
        'contract_title': contract_title[:1000] if contract_title else None,
        'contract_type': contract_type,
        'contract_phase': contract_phase,
        'contract_state': contract_state,
        'awarding_date': awarding_date.date() if awarding_date else None,
        'contract_date': contract_date.date() if contract_date else None,
        'publication_date': publication_date,
        'duration_months': duration_months,
        'contract_value': contract_value,
        'default_currency_value': default_currency_value,
        'currency': currency,
        'ca_notice_id': ca_notice_id,
        'ca_notice_no': ca_notice_no,
        'authority_name': authority_name,
        'authority_cui': authority_cui,
        'is_current_version': is_current,
        'is_rejected': is_rejected,
        'version_no': version_no,
        'version_date': version_date,
        'justification': justification,
        'additional_information': additional_info,
        'details': to_jsonable(el),
    }
