# -*- coding: utf-8 -*-
"""Unit tests for the new ODS ingestion tasks."""
import logging
import os
import sys
from pathlib import Path

# 确保在独立运行测试时能正确解析项目根目录
PROJECT_ROOT = Path(__file__).resolve().parents[2]
if str(PROJECT_ROOT) not in sys.path:
    sys.path.insert(0, str(PROJECT_ROOT))

os.environ.setdefault("ETL_SKIP_DOTENV", "1")

from tasks.ods.ods_tasks import ODS_TASK_CLASSES
from .task_test_utils import create_test_config, get_db_operations, FakeAPIClient


def _build_config(tmp_path):
    archive_dir = tmp_path / "archive"
    temp_dir = tmp_path / "temp"
    return create_test_config("ONLINE", archive_dir, temp_dir)


def test_assistant_accounts_masters_ingest(tmp_path):
    """Ensure ODS_ASSISTANT_ACCOUNT stores raw payload with record_index dedup keys."""
    config = _build_config(tmp_path)
    sample = [
        {
            "id": 5001,
            "assistant_no": "A01",
            "nickname": "小张",
        }
    ]
    api = FakeAPIClient({"/PersonnelManagement/SearchAssistantInfo": sample})
    task_cls = ODS_TASK_CLASSES["ODS_ASSISTANT_ACCOUNT"]

    with get_db_operations() as db_ops:
        task = task_cls(config, db_ops, api, logging.getLogger("test_assistant_accounts_masters"))
        result = task.execute()

    assert result["status"] == "SUCCESS"
    assert result["counts"]["fetched"] == 1
    assert db_ops.commits == 1
    row = db_ops.upserts[0]["rows"][0]
    assert row["id"] == 5001
    assert row["record_index"] == 0
    assert row["source_file"] is None or row["source_file"] == ""
    assert '"id": 5001' in row["payload"]


def test_goods_stock_movements_ingest(tmp_path):
    """Ensure ODS_INVENTORY_CHANGE stores raw payload with record_index dedup keys."""
    config = _build_config(tmp_path)
    sample = [
        {
            "siteGoodsStockId": 123456,
            "stockType": 1,
            "goodsName": "测试商品",
        }
    ]
    api = FakeAPIClient({"/GoodsStockManage/QueryGoodsOutboundReceipt": sample})
    task_cls = ODS_TASK_CLASSES["ODS_INVENTORY_CHANGE"]

    with get_db_operations() as db_ops:
        task = task_cls(config, db_ops, api, logging.getLogger("test_goods_stock_movements"))
        result = task.execute()

    assert result["status"] == "SUCCESS"
    assert result["counts"]["fetched"] == 1
    assert db_ops.commits == 1
    row = db_ops.upserts[0]["rows"][0]
    assert row["sitegoodsstockid"] == 123456
    assert row["record_index"] == 0
    assert '"siteGoodsStockId": 123456' in row["payload"]


def test_member_profiless_ingest(tmp_path):
    """Ensure ODS_MEMBER task stores tenantMemberInfos raw JSON."""
    config = _build_config(tmp_path)
    sample = [{"tenantMemberInfos": [{"id": 101, "mobile": "13800000000"}]}]
    api = FakeAPIClient({"/MemberProfile/GetTenantMemberList": sample})
    task_cls = ODS_TASK_CLASSES["ODS_MEMBER"]

    with get_db_operations() as db_ops:
        task = task_cls(config, db_ops, api, logging.getLogger("test_ods_member"))
        result = task.execute()

    assert result["status"] == "SUCCESS"
    row = db_ops.upserts[0]["rows"][0]
    assert row["record_index"] == 0
    assert '"id": 101' in row["payload"]


def test_ods_payment_ingest(tmp_path):
    """Ensure ODS_PAYMENT task stores payment_transactions raw JSON."""
    config = _build_config(tmp_path)
    sample = [{"payId": 901, "payAmount": "100.00"}]
    api = FakeAPIClient({"/PayLog/GetPayLogListPage": sample})
    task_cls = ODS_TASK_CLASSES["ODS_PAYMENT"]

    with get_db_operations() as db_ops:
        task = task_cls(config, db_ops, api, logging.getLogger("test_ods_payment"))
        result = task.execute()

    assert result["status"] == "SUCCESS"
    row = db_ops.upserts[0]["rows"][0]
    assert row["record_index"] == 0
    assert '"payId": 901' in row["payload"]


def test_ods_settlement_records_ingest(tmp_path):
    """Ensure ODS_SETTLEMENT_RECORDS stores settleList raw JSON."""
    config = _build_config(tmp_path)
    sample = [{"id": 701, "orderTradeNo": 8001}]
    api = FakeAPIClient({"/Site/GetAllOrderSettleList": sample})
    task_cls = ODS_TASK_CLASSES["ODS_SETTLEMENT_RECORDS"]

    with get_db_operations() as db_ops:
        task = task_cls(config, db_ops, api, logging.getLogger("test_settlement_records"))
        result = task.execute()

    assert result["status"] == "SUCCESS"
    row = db_ops.upserts[0]["rows"][0]
    assert row["record_index"] == 0
    assert '"orderTradeNo": 8001' in row["payload"]




