# -*- coding: utf-8 -*-
import sqlite3

db_path = r"C:\Users\Admin\Desktop\test模版\design_agent\pod_trend_agent\db\spu_sku.db"

conn = sqlite3.connect(db_path)
cur = conn.cursor()

# 若表已存在则先删除（重建表结构）
cur.execute("DROP TABLE IF EXISTS SKU")
cur.execute("DROP TABLE IF EXISTS SPU")

# ---------- SPU 表 ----------
cur.execute("""
CREATE TABLE SPU (
    id                     INTEGER PRIMARY KEY AUTOINCREMENT,
    code                   TEXT,
    material               TEXT,
    component_1            TEXT,
    component_2            TEXT,
    component_3            TEXT,
    component_proportion_1 TEXT,
    component_proportion_2 TEXT,
    component_proportion_3 TEXT,
    pattern                TEXT,
    details                TEXT,
    collar_style           TEXT,
    style                  TEXT,
    care_Instructions      TEXT,
    fabric                 TEXT,
    target_audience        TEXT,
    season                 TEXT,
    is_transparent         TEXT,
    layout                 TEXT,
    weaving_method         TEXT,
    printing_type          TEXT,
    fabric_texture_1       TEXT,
    fabric_weight_1        TEXT,
    fabric_weight_unit_1   TEXT,
    lining_texture         TEXT,
    country                TEXT,
    mark                   TEXT
)
""")

# ---------- SKU 表 ----------
cur.execute("""
CREATE TABLE SKU (
    id                  INTEGER PRIMARY KEY AUTOINCREMENT,
    spu_id              INTEGER NOT NULL,
    code                TEXT,
    price               REAL,
    color               TEXT,
    size                TEXT,
    size_group          TEXT,
    size_type           TEXT,
    shoulder_width      TEXT,
    bust                TEXT,
    clothing_length     TEXT,
    sleeve_length       TEXT,
    longest_side        TEXT,
    secondary_long_side TEXT,
    shortest_side       TEXT,
    package_weight      TEXT,
    img_url_2           TEXT,
    img_url_3           TEXT,
    img_url_4           TEXT,
    img_url_5           TEXT,
    FOREIGN KEY (spu_id) REFERENCES SPU(id)
)
""")

# 索引：SKU 按 spu_id 快速查询
cur.execute("CREATE INDEX idx_sku_spu_id ON SKU(spu_id)")

conn.commit()

# ---------- 验证 ----------
cur.execute("SELECT name FROM sqlite_master WHERE type='table'")
tables = cur.fetchall()
print("数据库文件:", db_path)
print("表列表:", [t[0] for t in tables])

cur.execute("PRAGMA table_info(SPU)")
spu_cols = cur.fetchall()
print("\nSPU 字段 ({}个):".format(len(spu_cols)))
for row in spu_cols:
    print(" ", row[1], "|", row[2], "| 主键" if row[5] else "")

cur.execute("PRAGMA table_info(SKU)")
sku_cols = cur.fetchall()
print("\nSKU 字段 ({}个):".format(len(sku_cols)))
for row in sku_cols:
    print(" ", row[1], "|", row[2], "| 主键" if row[5] else "")

conn.close()
print("\n创建完成")
