"""Checkpoint starter for Using SQL from Code.

Set DATABASE_URL in your environment. Never paste a password into this file.
"""

import os
from datetime import date

import psycopg


def connection_check() -> None:
    conninfo = os.environ["DATABASE_URL"]
    with psycopg.connect(conninfo) as conn:
        with conn.cursor() as cur:
            cur.execute("SELECT current_database(), current_user, current_timestamp")
            print(cur.fetchone())


def parameterized_report(region: str, start: date, end: date) -> list[tuple]:
    """Replace the table/query with the equivalent for your project."""
    conninfo = os.environ["DATABASE_URL"]
    query = """
        SELECT state, count(*) AS event_count
        FROM storm_events
        WHERE state = %s
          AND begin_date >= %s
          AND begin_date < %s
        GROUP BY state
    """
    with psycopg.connect(conninfo) as conn:
        with conn.cursor() as cur:
            cur.execute(query, (region, start, end))
            return cur.fetchall()


def rollback_test() -> None:
    """Adapt to a team-owned temporary table; verify no partial write remains."""
    raise NotImplementedError("Write the constraint violation and verification query")


if __name__ == "__main__":
    connection_check()
