import re
from datetime import datetime


def extract_and_save_customer_info_flow(client, message: str, customer_id: str, db):
    """从消息中提取客户信息并保存。"""
    if not message or not customer_id:
        return

    email_pattern = r"[\w\.-]+@[\w\.-]+\.\w+"
    email_match = re.search(email_pattern, message)
    if email_match:
        db.update_email(customer_id, email_match.group())

    phone_pattern = r"1[3-9]\d{9}"
    phone_match = re.search(phone_pattern, message)
    if phone_match:
        db.update_phone(customer_id, phone_match.group())

    wechat_pattern = r"[Vv微信]+号[:：]?\s*([\w-]+)"
    wechat_match = re.search(wechat_pattern, message)
    if wechat_match:
        db.update_wechat(customer_id, wechat_match.group(1))

    budget_keywords = ["预算", "不超过", "最多", "便宜点", "便宜"]
    for keyword in budget_keywords:
        if keyword in message:
            db.add_personality_tag(customer_id, "关注价格")
            break

    personality_keywords = {
        "爽快": "爽快",
        "干脆": "爽快",
        "纠结": "纠结",
        "墨迹": "纠结",
        "砍价": "砍价",
        "贵": "砍价",
    }
    for keyword, tag in personality_keywords.items():
        if keyword in message:
            db.add_personality_tag(customer_id, tag)

    profile = db.get_customer(customer_id)
    profile.last_contact = datetime.now().isoformat()
    db.save_customer(profile)
