#!/usr/bin/env python3
"""
商品品类分析脚本
批量读取商品标题，调用大模型进行品类分析，并保存结果
"""

import csv
import os
import json
import logging
import time
from datetime import datetime
from typing import List, Dict, Tuple
import requests
from pathlib import Path
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

# 配置
BATCH_SIZE = 20
API_BASE_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1"
MODEL_NAME = "qwen-max"
API_KEY = os.environ.get("DASHSCOPE_API_KEY")
MAX_RETRIES = 3
RETRY_DELAY = 5  # 秒
REQUEST_TIMEOUT = 180  # 秒

# 禁用代理
os.environ['NO_PROXY'] = '*'
os.environ['no_proxy'] = '*'

# 文件路径
INPUT_FILE = "saas_170_products.csv"
OUTPUT_DIR = Path("output_logs")
OUTPUT_FILE = OUTPUT_DIR / "products_analyzed.csv"
LOG_DIR = OUTPUT_DIR / "logs"

# 设置日志
LOG_DIR.mkdir(parents=True, exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
log_file = LOG_DIR / f"process_{timestamp}.log"

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler(log_file, encoding='utf-8'),
        logging.StreamHandler()
    ]
)
logger = logging.getLogger(__name__)


def create_prompt(products: List[Dict[str, str]]) -> str:
    """创建LLM提示词"""
    prompt = """请对输入的每条商品标题，分析并提取以下信息：

1. 商品中文标题：将输入商品名称翻译为中文
2. 品类路径：从大类到细分品类，用">"分隔（例如：服装>女装>裤子>工装裤）
3. 细分标签：商品的风格、特点、功能等（例如：碎花，收腰，法式）
4. 适用人群：性别/年龄段等（例如：年轻女性）
5. 使用场景
6. 适用季节
7. 关键属性
8. 材质说明 
9. 功能特点
10. 商品卖点：分析和提取一句话核心卖点，用于推荐理由
11. 锚文本：生成一组能够代表该商品、并可能被用户用于搜索的词语或短语。这些词语应覆盖用户需求的各个维度，如品类、细分标签、功能特性、需求场景等等。

输入商品列表：

"""

    prompt_tail = """
请严格按照以下markdown表格格式返回，每列内部的多值内容都用逗号分隔，不要添加任何其他说明：

| 序号 | 商品中文标题 | 品类路径 | 细分标签 | 适用人群 | 使用场景 | 适用季节 | 关键属性 | 材质说明 | 功能特点 | 商品卖点 | 锚文本 |
|----|----|----|----|----|----|----|----|----|----|----|----|
"""

    for idx, product in enumerate(products, 1):
        prompt += f'{idx}. {product["title"]}\n'
    prompt += prompt_tail

    return prompt


def call_llm(prompt: str) -> Tuple[str, str]:
    """调用大模型API（带重试机制）"""
    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }

    payload = {
        "model": MODEL_NAME,
        "messages": [
            {
                "role": "system",
                "content": "你是一名电商平台的商品标注员，你的工作是对输入的每个商品进行理解、分析和标注，按要求格式返回Markdown表格。"
            },
            {
                "role": "user",
                "content": prompt
            }
        ],
        "temperature": 0.3,
        "top_p": 0.8
    }

    request_data = {
        "headers": {k: v for k, v in headers.items() if k != "Authorization"},
        "payload": payload
    }

    logger.info(f"\n{'='*80}")
    logger.info(f"LLM Request (Model: {MODEL_NAME}):")
    logger.info(json.dumps(request_data, ensure_ascii=False, indent=2))
    logger.info(f"\nPrompt:\n{prompt}")

    # 创建session，禁用代理
    session = requests.Session()
    session.trust_env = False  # 忽略系统代理设置

    try:
        # 重试机制
        for attempt in range(MAX_RETRIES):
            try:
                response = session.post(
                    f"{API_BASE_URL}/chat/completions",
                    headers=headers,
                    json=payload,
                    timeout=REQUEST_TIMEOUT,
                    proxies={"http": None, "https": None}  # 明确禁用代理
                )

                response.raise_for_status()
                result = response.json()

                logger.info(f"\nLLM Response:")
                logger.info(json.dumps(result, ensure_ascii=False, indent=2))

                content = result["choices"][0]["message"]["content"]
                logger.info(f"\nExtracted Content:\n{content}")

                return content, json.dumps(result, ensure_ascii=False)

            except requests.exceptions.ProxyError as e:
                logger.warning(f"Attempt {attempt + 1}/{MAX_RETRIES}: Proxy error - {str(e)}")
                if attempt < MAX_RETRIES - 1:
                    logger.info(f"Retrying in {RETRY_DELAY} seconds...")
                    time.sleep(RETRY_DELAY)
                else:
                    raise

            except requests.exceptions.RequestException as e:
                logger.warning(f"Attempt {attempt + 1}/{MAX_RETRIES}: Request error - {str(e)}")
                if attempt < MAX_RETRIES - 1:
                    logger.info(f"Retrying in {RETRY_DELAY} seconds...")
                    time.sleep(RETRY_DELAY)
                else:
                    raise

            except Exception as e:
                logger.error(f"Unexpected error on attempt {attempt + 1}/{MAX_RETRIES}: {str(e)}")
                if attempt < MAX_RETRIES - 1:
                    logger.info(f"Retrying in {RETRY_DELAY} seconds...")
                    time.sleep(RETRY_DELAY)
                else:
                    raise

    finally:
        session.close()


def parse_markdown_table(markdown_content: str) -> List[Dict[str, str]]:
    """解析markdown表格内容"""
    lines = markdown_content.strip().split('\n')
    data = []
    data_started = False

    for line in lines:
        line = line.strip()
        if not line:
            continue

        # 跳过表头
        if line.startswith('|'):
            # 跳过分隔行
            if set(line.replace('|', '').strip()) <= {'-', ':'}:
                data_started = True
                continue

            # 跳过表头行
            if not data_started:
                if '序号' in line or '商品中文标题' in line:
                    continue
                data_started = True
                continue

            # 解析数据行
            parts = [p.strip() for p in line.split('|')]
            parts = [p for p in parts if p]  # 移除空字符串

            if len(parts) >= 2:
                row = {
                    "seq_no": parts[0],
                    "title_cn": parts[1],  # 商品中文标题
                    "category_path": parts[2] if len(parts) > 2 else "",  # 品类路径
                    "tags": parts[3] if len(parts) > 3 else "",  # 细分标签
                    "target_audience": parts[4] if len(parts) > 4 else "",  # 适用人群
                    "usage_scene": parts[5] if len(parts) > 5 else "",  # 使用场景
                    "season": parts[6] if len(parts) > 6 else "",  # 适用季节
                    "key_attributes": parts[7] if len(parts) > 7 else "",  # 关键属性
                    "material": parts[8] if len(parts) > 8 else "",  # 材质说明
                    "features": parts[9] if len(parts) > 9 else "",  # 功能特点
                    "selling_points": parts[10] if len(parts) > 10 else "",  # 商品卖点
                    "anchor_text": parts[11] if len(parts) > 11 else ""  # 锚文本
                }
                data.append(row)

    return data


def process_batch(batch_data: List[Dict[str, str]], batch_num: int) -> List[Dict[str, str]]:
    """处理一个批次的数据"""
    logger.info(f"\n{'#'*80}")
    logger.info(f"Processing Batch {batch_num} ({len(batch_data)} items)")

    # 创建提示词
    prompt = create_prompt(batch_data)

    # 调用LLM
    try:
        raw_response, full_response_json = call_llm(prompt)

        # 解析结果
        parsed_results = parse_markdown_table(raw_response)

        logger.info(f"\nParsed Results ({len(parsed_results)} items):")
        logger.info(json.dumps(parsed_results, ensure_ascii=False, indent=2))

        # 映射回原始ID
        results_with_ids = []
        for i, parsed_item in enumerate(parsed_results):
            if i < len(batch_data):
                original_id = batch_data[i]["id"]
                result = {
                    "id": original_id,
                    "title": batch_data[i]["title"],  # 原始英文标题
                    "title_cn": parsed_item.get("title_cn", ""),  # 中文标题
                    "category_path": parsed_item.get("category_path", ""),  # 品类路径
                    "tags": parsed_item.get("tags", ""),  # 细分标签
                    "target_audience": parsed_item.get("target_audience", ""),  # 适用人群
                    "usage_scene": parsed_item.get("usage_scene", ""),  # 使用场景
                    "season": parsed_item.get("season", ""),  # 适用季节
                    "key_attributes": parsed_item.get("key_attributes", ""),  # 关键属性
                    "material": parsed_item.get("material", ""),  # 材质说明
                    "features": parsed_item.get("features", ""),  # 功能特点
                    "selling_points": parsed_item.get("selling_points", ""),  # 商品卖点
                    "anchor_text": parsed_item.get("anchor_text", "")  # 锚文本
                }
                results_with_ids.append(result)
                logger.info(f"Mapped: seq={parsed_item['seq_no']} -> original_id={original_id}")

        # 保存日志
        batch_log = {
            "batch_num": batch_num,
            "timestamp": datetime.now().isoformat(),
            "input_products": batch_data,
            "raw_response": raw_response,
            "full_response_json": full_response_json,
            "parsed_results": parsed_results,
            "final_results": results_with_ids
        }

        batch_log_file = LOG_DIR / f"batch_{batch_num:04d}_{timestamp}.json"
        with open(batch_log_file, 'w', encoding='utf-8') as f:
            json.dump(batch_log, f, ensure_ascii=False, indent=2)

        logger.info(f"Batch log saved to: {batch_log_file}")

        return results_with_ids

    except Exception as e:
        logger.error(f"Error processing batch {batch_num}: {str(e)}", exc_info=True)
        # 返回空结果，保持ID映射
        return [{"id": item["id"], "title": item["title"],
                 "title_cn": "", "category_path": "", "tags": "", "target_audience": "",
                 "usage_scene": "", "season": "", "key_attributes": "",
                 "material": "", "features": "", "selling_points": "", "anchor_text": "",
                 "error": str(e)} for item in batch_data]


def read_products(input_file: str) -> List[Dict[str, str]]:
    """读取CSV文件"""
    products = []
    with open(input_file, 'r', encoding='utf-8') as f:
        reader = csv.DictReader(f)
        for row in reader:
            products.append({
                "id": row["id"],
                "title": row["title"]
            })
    return products


def write_results(results: List[Dict[str, str]], output_file: Path):
    """写入结果到CSV文件"""
    output_file.parent.mkdir(parents=True, exist_ok=True)

    fieldnames = ["id", "title", "title_cn", "category_path", "tags",
                  "target_audience", "usage_scene", "season",
                  "key_attributes", "material", "features", "selling_points", "anchor_text"]

    with open(output_file, 'w', encoding='utf-8', newline='') as f:
        writer = csv.DictWriter(f, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerows(results)

    logger.info(f"\nResults written to: {output_file}")


def main():
    """主函数"""
    if not API_KEY:
        logger.error("Error: DASHSCOPE_API_KEY environment variable not set!")
        return

    logger.info(f"Starting product analysis process")
    logger.info(f"Input file: {INPUT_FILE}")
    logger.info(f"Output file: {OUTPUT_FILE}")
    logger.info(f"Batch size: {BATCH_SIZE}")
    logger.info(f"Model: {MODEL_NAME}")

    # 读取产品数据
    logger.info(f"\nReading products from {INPUT_FILE}...")
    products = read_products(INPUT_FILE)
    logger.info(f"Total products to process: {len(products)}")

    # 分批处理
    all_results = []
    total_batches = (len(products) + BATCH_SIZE - 1) // BATCH_SIZE

    for i in range(0, len(products), BATCH_SIZE):
        batch_num = i // BATCH_SIZE + 1
        batch = products[i:i + BATCH_SIZE]

        logger.info(f"\nProgress: Batch {batch_num}/{total_batches}")

        results = process_batch(batch, batch_num)
        all_results.extend(results)

        # 每处理完一个批次就写入一次（断点续传）
        write_results(all_results, OUTPUT_FILE)
        logger.info(f"Progress saved: {len(all_results)}/{len(products)} items completed")

    logger.info(f"\n{'='*80}")
    logger.info(f"Processing completed!")
    logger.info(f"Total processed: {len(all_results)} items")
    logger.info(f"Output file: {OUTPUT_FILE}")
    logger.info(f"Log file: {log_file}")


if __name__ == "__main__":
    main()
