import psycopg2
import uuid
from datetime import datetime

try:
    conn = psycopg2.connect(
        host="192.168.1.85", 
        port=5432, 
        user="admin", 
        password="admin_password_2026", 
        dbname="lubrication_crm"
    )
    cur = conn.cursor()

    # 1. 存在测试用的客户 ID (LogEntry.vue 默认写的)
    test_client_uuid = "a37dbd8b-a9c0-4deb-ad76-83a0d29bbf28"

    cur.execute(
        """
        INSERT INTO clients (id, name, contact_person, phone)
        VALUES (%s, '自动化联调测试客户', '张经理', '13800138000')
        ON CONFLICT (id) DO NOTHING
        """,
        (test_client_uuid,)
    )

    # 2. 插入一些销售机会数据 (用于展示当月的 Dashboard AI 复盘效果)
    current_year = datetime.now().year
    current_month = datetime.now().month
    created_str = f"{current_year}-{current_month:02d}-15 10:00:00"

    opportunities = [
        {"stage": "意向", "amount": 50000},
        {"stage": "意向", "amount": 20000},
        {"stage": "谈判", "amount": 150000},
        {"stage": "成交", "amount": 300000},
        {"stage": "流失", "amount": 10000},
    ]

    for opp in opportunities:
        cur.execute(
            """
            INSERT INTO sales_opportunities (id, customer_id, stage, amount, created_at)
            VALUES (%s, %s, %s, %s, %s)
            """,
            (
                str(uuid.uuid4()),
                test_client_uuid,
                opp["stage"],
                opp["amount"],
                created_str,
            )
        )

    conn.commit()
    print("Seed data for logging and reports inserted successfully.")

except Exception as e:
    print(f"Database error: {e}")
finally:
    if 'cur' in locals():
        cur.close()
    if 'conn' in locals():
        conn.close()
