"""
GLAMWalk -- Volume 3: Data at Scale
02_dry_run_commit_pattern.py

The core safety pattern every import script in this book follows: dry-run by default, an explicit --commit flag required to write anything, idempotent inserts, never DELETE.

Source: Volume Three, Section 1.1 -- The Core Discipline, Stated Once
Targets Python 3.11.4 -- see Volume One, Chapter 1.5/1.6.
"""

import argparse
import sqlite3

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--commit", action="store_true",
                         help="Actually write to the database. Without this flag, dry-run only.")
    args = parser.parse_args()

    conn = sqlite3.connect("art_catalog.db")

    def gather_candidate_rows():
        """Stand-in for your real fetch/parse logic — this is where
        you'd return the real rows you're about to insert."""
        return []

    rows_to_insert = gather_candidate_rows()   # illustrative — the real fetch/parse logic

    print(f"Would insert {len(rows_to_insert)} rows.")

    if not args.commit:
        print("Dry run only — no changes made. Re-run with --commit to write.")
        return

    for row in rows_to_insert:
        conn.execute(
            "INSERT OR IGNORE INTO artworks (canonical_id, title, image_url) VALUES (?, ?, ?)",
            (row["canonical_id"], row["title"], row["image_url"]),
        )
    conn.commit()
    print(f"Committed {len(rows_to_insert)} rows.")

if __name__ == "__main__":
    main()
