"""Marketing helpers."""

from __future__ import annotations

import json
import sqlite3
from typing import Optional


def segment_preset_key(conn: sqlite3.Connection, override: Optional[str] = None) -> str:
    """Default segment preset from store_meta or CLI override."""
    if override:
        return override.upper()
    row = conn.execute("SELECT value_json FROM store_meta WHERE key='segment_preset'").fetchone()
    if not row:
        return "A"
    try:
        val = json.loads(row["value_json"] or '"A"')
    except json.JSONDecodeError:
        return "A"
    preset = str(val).strip('"').upper()
    return preset if preset in ("A", "B") else "A"
