import pymysql

conn = pymysql.connect(
    host="localhost",
    user="moto_user",
    password="moto_pass_2026",
    database="surfboards_db",
    charset="utf8mb4",
)
cur = conn.cursor()

cur.execute("""
CREATE TABLE IF NOT EXISTS fins (
    id INT AUTO_INCREMENT PRIMARY KEY,
    shop_id INT NOT NULL,
    source_id VARCHAR(128) NOT NULL DEFAULT '',
    source_url VARCHAR(512) NOT NULL,
    product_type VARCHAR(32) NOT NULL DEFAULT 'fcs2',
    origin_type VARCHAR(16) NOT NULL DEFAULT 'overseas',
    maker VARCHAR(64) NOT NULL DEFAULT 'その他',
    sub_maker VARCHAR(64) NOT NULL DEFAULT '',
    shaper VARCHAR(128) NOT NULL DEFAULT '',
    series_name VARCHAR(128) NOT NULL DEFAULT '',
    series_slug VARCHAR(128) NOT NULL DEFAULT '',
    model_name VARCHAR(128) NOT NULL DEFAULT '未分類',
    model_slug VARCHAR(128) NOT NULL DEFAULT 'unclassified',
    model_code VARCHAR(128) NOT NULL DEFAULT '',
    maker_sku_key VARCHAR(191) NOT NULL DEFAULT '',
    title VARCHAR(512) NOT NULL,
    title_en VARCHAR(512) NOT NULL DEFAULT '',
    display_title VARCHAR(512) NOT NULL DEFAULT '',
    condition_type VARCHAR(16) NOT NULL DEFAULT 'new',
    condition_rank VARCHAR(8) NOT NULL DEFAULT 'N',
    fin_system VARCHAR(64) NOT NULL DEFAULT 'FCS II',
    fin_setup VARCHAR(64) NOT NULL DEFAULT 'トライ（3フィン）',
    size_code VARCHAR(32) NOT NULL DEFAULT '',
    size_bucket VARCHAR(64) NOT NULL DEFAULT '不明',
    fin_inch DECIMAL(4,2) NOT NULL DEFAULT 0.00,
    material VARCHAR(64) NOT NULL DEFAULT '不明',
    color VARCHAR(64) NOT NULL DEFAULT 'ブラック・カーボン系',
    raw_color VARCHAR(64) NOT NULL DEFAULT '',
    base_price INT NOT NULL DEFAULT 0,
    total_price INT NOT NULL DEFAULT 0,
    original_currency VARCHAR(8) NOT NULL DEFAULT 'JPY',
    original_price DECIMAL(10,2) NOT NULL DEFAULT 0.00,
    fx_rate DECIMAL(10,4) NOT NULL DEFAULT 1.0000,
    shipping_jpy INT NOT NULL DEFAULT 0,
    is_free_shipping TINYINT(1) NOT NULL DEFAULT 0,
    warranty VARCHAR(32) NOT NULL DEFAULT '無し',
    prefecture VARCHAR(32) NOT NULL DEFAULT '',
    city_address VARCHAR(128) NOT NULL DEFAULT '',
    store_name VARCHAR(128) NOT NULL DEFAULT '',
    main_image_url VARCHAR(512) NOT NULL DEFAULT '/img/no-img.png',
    image_urls TEXT,
    description TEXT,
    dedup_hash VARCHAR(64) NOT NULL DEFAULT '',
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    is_c2c TINYINT(1) NOT NULL DEFAULT 0,
    scraped_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY uq_fin_source_url (source_url),
    KEY idx_fin_active_maker_model (is_active, maker, model_slug),
    KEY idx_fin_active_ptype (is_active, product_type),
    KEY idx_fin_active_setup (is_active, fin_setup),
    KEY idx_fin_active_size (is_active, size_bucket),
    KEY idx_fin_maker_sku (maker_sku_key),
    KEY idx_fin_shop (shop_id, is_active)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
""")

# Register official fin brand stores in shops table
fin_shops = [
    (64, "surffcs_us", "FCS Official Store (USA)", "www.surffcs.com", "US", 1, "USD", 4500),
    (65, "futures_us", "Futures Fins Official (California / USA)", "futuresfins.com", "US", 1, "USD", 4500),
    (66, "trueames_us", "True Ames Fins Official (California / USA)", "www.trueames.com", "US", 1, "USD", 4500),
    (67, "captainfin_us", "Captain Fin Co. Official (California / USA)", "captainfin.com", "US", 1, "USD", 4500),
    (68, "nvs_us", "NVS (Naked Viking Surf Official / CA)", "www.surfnvs.com", "US", 1, "USD", 4500),
    (69, "flyingdiamonds_us", "Flying Diamonds of California Official", "flyingdiamondsofcalifornia.com", "US", 1, "USD", 4500),
    (70, "deflow_eu", "Deflow Surf Official (Europe)", "deflowsurf.com", "ES", 1, "EUR", 5000),
]
for sid, skey, sname, dom, ccode, is_ov, curr, ship_jpy in fin_shops:
    cur.execute("""
        INSERT INTO shops (id, site_key, name, domain, channel_type, country_code, is_overseas, base_currency, default_shipping_jpy, shipping_note, warranty_note)
        VALUES (%s, %s, %s, %s, 'direct', %s, %s, %s, %s, '国際小型パケット便（日本発送対応）', %s)
        ON DUPLICATE KEY UPDATE
            site_key = VALUES(site_key),
            name = VALUES(name),
            domain = VALUES(domain),
            country_code = VALUES(country_code),
            is_overseas = VALUES(is_overseas),
            base_currency = VALUES(base_currency),
            default_shipping_jpy = VALUES(default_shipping_jpy)
    """, (sid, skey, sname, dom, ccode, is_ov, curr, ship_jpy, sname + " 正規フィン"))

conn.commit()
print("Created fins table and registered official fin stores (#64-#70).")
