#!/usr/bin/env python3
"""Import HS classifier into Odoo.

HS2/HS4 -> products.category (tree)
HS6 -> products.product
UUIDs are generated; HS code stored in hs_code.
"""
import csv
import sys
import re
import uuid
from pathlib import Path

# Allow import of datasets.hub.client when run as a script
sys.path.append(str(Path(__file__).resolve().parents[1]))

from hub.client import OdooClient  # type: ignore

HS_CSV = Path(__file__).parent / 'hs_comtrade_reference.csv'

_invalid_xml = re.compile(r'[\x00-\x08\x0B\x0C\x0E-\x1F]')

def clean(s: str) -> str:
    if s is None:
        return ''
    return _invalid_xml.sub('', s)


def load_rows():
    with HS_CSV.open('r', encoding='utf-8') as f:
        return list(csv.DictReader(f))


def main():
    client = OdooClient()
    client.connect()

    rows = load_rows()

    # Filter rows by levels
    hs2 = [r for r in rows if r.get('aggrLevel') == '2']
    hs4 = [r for r in rows if r.get('aggrLevel') == '4']
    hs6 = [r for r in rows if r.get('aggrLevel') == '6' and r.get('isLeaf') == '1']

    hs_codes = {r.get('id') for r in rows if r.get('id') and r.get('id') != 'TOTAL'}
    hs6_codes = {r.get('id') for r in hs6 if r.get('id')}

    # Purge old HS categories/products by hs_code
    batch = 500
    while True:
        ids = client.search('products.category', [['hs_code', 'in', list(hs_codes)]], limit=batch)
        if not ids:
            break
        client.unlink('products.category', ids)

    while True:
        ids = client.search('products.product', [['hs_code', 'in', list(hs6_codes)]], limit=batch)
        if not ids:
            break
        client.unlink('products.product', ids)

    # Create categories HS2/HS4
    by_hs = {}
    created_cat = 0

    for row in hs2:
        code = row.get('id')
        if not code:
            continue
        name = clean(row.get('text') or code)
        vals = {
            'uuid': str(uuid.uuid4()),
            'hs_code': code,
            'name': name,
            'parent_id': False,
            'level': 2,
        }
        new_id = client.create('products.category', vals)
        by_hs[code] = new_id
        created_cat += 1

    for row in hs4:
        code = row.get('id')
        if not code:
            continue
        name = clean(row.get('text') or code)
        parent_code = row.get('parent')
        parent_id = by_hs.get(parent_code) if parent_code else False
        vals = {
            'uuid': str(uuid.uuid4()),
            'hs_code': code,
            'name': name,
            'parent_id': parent_id,
            'level': 4,
        }
        new_id = client.create('products.category', vals)
        by_hs[code] = new_id
        created_cat += 1

    # Create products HS6
    created_prod = 0
    for row in hs6:
        code = row.get('id')
        if not code:
            continue
        name = clean(row.get('text') or code)
        parent_code = row.get('parent')
        category_id = by_hs.get(parent_code)
        if not category_id:
            category_id = by_hs.get(code[:2])
        if not category_id:
            continue
        vals = {
            'uuid': str(uuid.uuid4()),
            'hs_code': code,
            'name': name,
            'category_id': category_id,
        }
        client.create('products.product', vals)
        created_prod += 1

    print(f"HS import complete: categories={created_cat}, products={created_prod}")


if __name__ == '__main__':
    main()
