import sqlite3
import json
import sys

sys.stdout.reconfigure(encoding='utf-8')

conn = sqlite3.connect('data/map_styles.db')
cur = conn.cursor()

print("=== TABLES ===")
cur.execute("SELECT name, sql FROM sqlite_master WHERE type='table' ORDER BY name")
for row in cur.fetchall():
    print(f"Table: {row[0]}")
    print(f"  SQL: {row[1]}")

print("\n=== ROW COUNTS ===")
for t in ['map_styles', 'map_style_tiles', 'map_style_props']:
    cur.execute(f"SELECT count(*) FROM {t}")
    print(f"{t}: {cur.fetchone()[0]}")

print("\n=== INTEGRITY & FOREIGN KEYS ===")
cur.execute("PRAGMA integrity_check")
print("integrity_check:", cur.fetchall())
cur.execute("PRAGMA foreign_key_check")
print("foreign_key_check:", cur.fetchall())

print("\n=== BIOME CODES & STYLES (All 30) ===")
cur.execute("SELECT biome_code, style_id, name_vi, name_en, theme_category, movement_modifier FROM map_styles ORDER BY biome_code")
styles = cur.fetchall()
print(f"Total styles fetched: {len(styles)}")
for s in styles:
    print(f"[{s[0]:2d}] {s[1]}: {s[2]} | {s[3]} | {s[4]} | speed={s[5]}")

print("\n=== THEMES SUMMARY ===")
cur.execute("SELECT theme_category, count(*), group_concat(biome_code) FROM map_styles GROUP BY theme_category ORDER BY min(biome_code)")
for r in cur.fetchall():
    print(f"Theme {r[0]}: {r[1]} styles, biome_codes=[{r[2]}]")

print("\n=== TILE DISTRIBUTION PER STYLE ===")
cur.execute("SELECT count(DISTINCT style_id), count(DISTINCT tile_type_id), count(*) FROM map_style_tiles")
print("Tiles summary (distinct styles, distinct tile_type_ids, total):", cur.fetchall())

cur.execute("SELECT style_id, count(*) FROM map_style_tiles GROUP BY style_id HAVING count(*) != 20")
unusual_tiles = cur.fetchall()
print("Styles with tile count != 20:", unusual_tiles)

print("\n=== SAMPLE TILES (biome 1: 5 tiles) ===")
cur.execute("""
    SELECT t.tile_type_id, t.tile_type_name, t.texture_filename, t.normal_filename, t.elevation_px, t.base_color_fallback
    FROM map_style_tiles t JOIN map_styles s ON t.style_id=s.style_id
    WHERE s.biome_code=1 ORDER BY t.tile_type_id LIMIT 5
""")
for r in cur.fetchall():
    print(f"  Tile {r[0]} ({r[1]}): tex={r[2]}, norm={r[3]}, elev={r[4]}, fallback={r[5]}")

print("\n=== PROP DISTRIBUTION PER STYLE ===")
cur.execute("SELECT count(DISTINCT style_id), count(*) FROM map_style_props")
print("Props summary (distinct styles, total):", cur.fetchall())

cur.execute("SELECT style_id, count(*) FROM map_style_props GROUP BY style_id HAVING count(*) != 3")
unusual_props = cur.fetchall()
print("Styles with prop count != 3:", unusual_props)

print("\n=== SAMPLE PROPS (biome 1) ===")
cur.execute("""
    SELECT p.prop_id, p.prop_name_vi, p.spawn_weight, p.placement_rule, p.blocks_movement, p.footprint_w, p.footprint_h
    FROM map_style_props p JOIN map_styles s ON p.style_id=s.style_id
    WHERE s.biome_code=1
""")
for r in cur.fetchall():
    print(f"  Prop {r[0]} ({r[1]}): weight={r[2]}, rule={r[3]}, blocks={r[4]}, footprint={r[5]}x{r[6]}")

conn.close()
