"""Shared-db read helpers for insurance-sales-pipeline."""
from __future__ import annotations

from insurance_pipeline_cli import db


def customer_exists(customer_id: str) -> bool:
    conn = db.connect()
    try:
        row = conn.execute(
            "SELECT 1 FROM customers WHERE id=? AND status='active'",
            (customer_id,),
        ).fetchone()
        return bool(row)
    finally:
        conn.close()


def customer_query(customer_id: str) -> dict:
    conn = db.connect()
    try:
        rows = list(
            conn.execute("SELECT * FROM customers WHERE id=? AND status='active'", (customer_id,))
        )
        return {"ok": True, "customers": [dict(r) for r in rows], "count": len(rows)}
    finally:
        conn.close()


def product_query(name: str) -> dict:
    conn = db.connect()
    try:
        rows = list(conn.execute("SELECT * FROM products WHERE name=? AND status='active'", (name,)))
        return {"ok": True, "products": [dict(r) for r in rows], "count": len(rows)}
    finally:
        conn.close()
