File size: 25,026 Bytes
584ed90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
"""Durable local customer reviews for the Amazon clone.

This module is deliberately independent from :mod:`store` so its schema can be
installed into an existing clone database without rewriting the core schema.
Call :func:`install_schema` once during database startup, after ``schema.sql``
has created ``accounts``, ``browser_sessions``, ``catalog_products``,
``commerce_offers``, ``orders``, and ``order_items``.

Request handlers must pass the server-side session digest, never an account id
from a form.  Review ownership is resolved through ``browser_sessions`` and
``verified_purchase`` is derived at read time from placed orders.  Consequently
neither value can be asserted by a client.

For a full benchmark reset, call :func:`reset_review_data` before clearing the
core tables.  Deleting ``accounts`` with foreign keys enabled also cascades all
reviews and votes, but the explicit reset is clearer and remains safe if the
core reset order changes.  Schema installation is idempotent and refuses an
unknown review-schema version instead of destructively guessing a migration.
"""

from __future__ import annotations

import re
import sqlite3
from datetime import datetime, timezone
from typing import Any


LOCAL_REVIEW_PROVENANCE = "local_user_review"
REVIEW_SCHEMA_VERSION = 1
MAX_HEADLINE_LENGTH = 200
MAX_BODY_LENGTH = 10_000
REVIEW_SORT_RECENT = "recent"
REVIEW_SORT_HELPFUL = "helpful"
REVIEW_SORTS = frozenset({REVIEW_SORT_RECENT, REVIEW_SORT_HELPFUL})
REVIEW_PRODUCT_SCOPES = frozenset(
    {"catalog_product", "commerce_offer", "home_snapshot"}
)

_ASIN_PATTERN = re.compile(r"^[A-Z0-9]{10}$")


class ReviewStoreError(ValueError):
    """Base class for expected review-domain failures."""


class ReviewValidationError(ReviewStoreError):
    """A submitted review field or query option is invalid."""


class ReviewAuthenticationRequired(ReviewStoreError):
    """The browser session exists but is not signed in."""


class ReviewNotFound(ReviewStoreError):
    """A referenced session, product, or review does not exist."""


class ReviewPermissionDenied(ReviewStoreError):
    """The current actor is not allowed to perform the operation."""


class ReviewSchemaError(RuntimeError):
    """The installed review schema is incompatible with this module."""


_BASE_SCHEMA = """
CREATE TABLE IF NOT EXISTS local_review_schema_meta (
    singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
    schema_version INTEGER NOT NULL CHECK (schema_version > 0)
);

INSERT OR IGNORE INTO local_review_schema_meta(singleton, schema_version)
VALUES (1, 1);

CREATE TABLE IF NOT EXISTS product_reviews (
    review_id INTEGER PRIMARY KEY AUTOINCREMENT,
    account_id INTEGER NOT NULL
        REFERENCES accounts(account_id) ON DELETE CASCADE,
    asin TEXT NOT NULL COLLATE NOCASE,
    rating INTEGER NOT NULL CHECK (rating BETWEEN 1 AND 5),
    headline TEXT NOT NULL CHECK (
        length(trim(headline)) BETWEEN 1 AND 200
    ),
    body TEXT NOT NULL CHECK (
        length(trim(body)) BETWEEN 1 AND 10000
    ),
    created_at TEXT NOT NULL,
    updated_at TEXT NOT NULL,
    UNIQUE (account_id, asin)
);

-- Browse-only products intentionally stay out of commerce_offers.  This
-- server-owned registry lets those known PDPs accept local reviews without
-- turning them into purchasable inventory or trusting an ASIN from a form.
CREATE TABLE IF NOT EXISTS review_product_catalog (
    asin TEXT PRIMARY KEY COLLATE NOCASE CHECK (
        length(asin) = 10 AND asin NOT GLOB '*[^A-Z0-9]*'
    ),
    source_scope TEXT NOT NULL CHECK (
        source_scope IN ('catalog_product', 'commerce_offer', 'home_snapshot')
    )
);

CREATE TABLE IF NOT EXISTS review_helpful_votes (
    vote_id INTEGER PRIMARY KEY AUTOINCREMENT,
    review_id INTEGER NOT NULL
        REFERENCES product_reviews(review_id) ON DELETE CASCADE,
    voter_account_id INTEGER
        REFERENCES accounts(account_id) ON DELETE CASCADE,
    voter_session_digest TEXT
        REFERENCES browser_sessions(session_digest) ON DELETE CASCADE,
    created_at TEXT NOT NULL,
    CHECK (
        (voter_account_id IS NOT NULL AND voter_session_digest IS NULL)
        OR
        (voter_account_id IS NULL AND voter_session_digest IS NOT NULL)
    )
);
"""


_INDEXES_AND_TRIGGERS = """
CREATE INDEX IF NOT EXISTS product_reviews_asin_recent_idx
    ON product_reviews(asin, created_at DESC, review_id DESC);
CREATE INDEX IF NOT EXISTS product_reviews_asin_rating_idx
    ON product_reviews(asin, rating, created_at DESC, review_id DESC);
CREATE INDEX IF NOT EXISTS review_helpful_votes_review_idx
    ON review_helpful_votes(review_id);

CREATE UNIQUE INDEX IF NOT EXISTS review_helpful_votes_account_unique_idx
    ON review_helpful_votes(review_id, voter_account_id)
    WHERE voter_account_id IS NOT NULL;
CREATE UNIQUE INDEX IF NOT EXISTS review_helpful_votes_session_unique_idx
    ON review_helpful_votes(review_id, voter_session_digest)
    WHERE voter_account_id IS NULL;

-- Trigger bodies changed when the browse-only review scope was added.  Drop
-- the old v1 definitions so existing databases receive the additive guard.
DROP TRIGGER IF EXISTS product_reviews_product_guard_insert;
DROP TRIGGER IF EXISTS product_reviews_product_guard_update;
DROP TRIGGER IF EXISTS product_reviews_catalog_product_cleanup;
DROP TRIGGER IF EXISTS product_reviews_commerce_offer_cleanup;
DROP TRIGGER IF EXISTS product_reviews_scope_cleanup;

CREATE TRIGGER product_reviews_product_guard_insert
BEFORE INSERT ON product_reviews
WHEN NOT EXISTS (
    SELECT 1 FROM catalog_products WHERE asin = NEW.asin
)
AND NOT EXISTS (
    SELECT 1 FROM commerce_offers WHERE asin = NEW.asin
)
AND NOT EXISTS (
    SELECT 1 FROM review_product_catalog WHERE asin = NEW.asin
)
BEGIN
    SELECT RAISE(ABORT, 'review product does not exist');
END;

CREATE TRIGGER product_reviews_product_guard_update
BEFORE UPDATE OF asin ON product_reviews
WHEN NOT EXISTS (
    SELECT 1 FROM catalog_products WHERE asin = NEW.asin
)
AND NOT EXISTS (
    SELECT 1 FROM commerce_offers WHERE asin = NEW.asin
)
AND NOT EXISTS (
    SELECT 1 FROM review_product_catalog WHERE asin = NEW.asin
)
BEGIN
    SELECT RAISE(ABORT, 'review product does not exist');
END;

CREATE TRIGGER IF NOT EXISTS review_helpful_votes_no_self_insert
BEFORE INSERT ON review_helpful_votes
WHEN NEW.voter_account_id IS NOT NULL
AND EXISTS (
    SELECT 1
    FROM product_reviews
    WHERE review_id = NEW.review_id
      AND account_id = NEW.voter_account_id
)
BEGIN
    SELECT RAISE(ABORT, 'review authors cannot vote for their own review');
END;

-- Reviews disappear only when an ASIN is absent from every server-owned
-- product scope.  Deleting one overlapping source cannot erase valid rows.
CREATE TRIGGER product_reviews_catalog_product_cleanup
AFTER DELETE ON catalog_products
WHEN NOT EXISTS (
    SELECT 1 FROM commerce_offers WHERE asin = OLD.asin
)
AND NOT EXISTS (
    SELECT 1 FROM review_product_catalog WHERE asin = OLD.asin
)
BEGIN
    DELETE FROM product_reviews WHERE asin = OLD.asin;
END;

CREATE TRIGGER product_reviews_commerce_offer_cleanup
AFTER DELETE ON commerce_offers
WHEN NOT EXISTS (
    SELECT 1 FROM catalog_products WHERE asin = OLD.asin
)
AND NOT EXISTS (
    SELECT 1 FROM review_product_catalog WHERE asin = OLD.asin
)
BEGIN
    DELETE FROM product_reviews WHERE asin = OLD.asin;
END;

CREATE TRIGGER product_reviews_scope_cleanup
AFTER DELETE ON review_product_catalog
WHEN NOT EXISTS (
    SELECT 1 FROM catalog_products WHERE asin = OLD.asin
)
AND NOT EXISTS (
    SELECT 1 FROM commerce_offers WHERE asin = OLD.asin
)
BEGIN
    DELETE FROM product_reviews WHERE asin = OLD.asin;
END;
"""


_EXPECTED_COLUMNS = {
    "review_product_catalog": {"asin", "source_scope"},
    "product_reviews": {
        "review_id",
        "account_id",
        "asin",
        "rating",
        "headline",
        "body",
        "created_at",
        "updated_at",
    },
    "review_helpful_votes": {
        "vote_id",
        "review_id",
        "voter_account_id",
        "voter_session_digest",
        "created_at",
    },
}


def install_schema(conn: sqlite3.Connection) -> None:
    """Install the additive v1 schema and validate an existing installation.

    Run this outside an active request transaction: ``executescript`` owns the
    DDL transaction boundary in Python's SQLite driver.  No core row is changed.
    """

    conn.execute("PRAGMA foreign_keys = ON")
    conn.executescript(_BASE_SCHEMA)
    version_row = conn.execute(
        "SELECT schema_version FROM local_review_schema_meta WHERE singleton=?",
        (1,),
    ).fetchone()
    if version_row is None or int(version_row[0]) != REVIEW_SCHEMA_VERSION:
        found = None if version_row is None else version_row[0]
        raise ReviewSchemaError(
            f"unsupported local review schema version: {found!r}"
        )

    for table, expected in _EXPECTED_COLUMNS.items():
        actual = {
            str(row[1]) for row in conn.execute(f"PRAGMA table_info({table})")
        }
        missing = expected - actual
        if missing:
            raise ReviewSchemaError(
                f"{table} is missing required columns: {', '.join(sorted(missing))}"
            )
    conn.executescript(_INDEXES_AND_TRIGGERS)


def reset_review_data(conn: sqlite3.Connection) -> None:
    """Remove mutable review data while preserving the installed schema."""

    conn.execute("DELETE FROM review_helpful_votes")
    conn.execute("DELETE FROM product_reviews")
    conn.execute("DELETE FROM review_product_catalog")


def _normalize_asin(value: object) -> str:
    if not isinstance(value, str):
        raise ReviewValidationError("asin must be text")
    asin = value.strip().upper()
    if not _ASIN_PATTERN.fullmatch(asin):
        raise ReviewValidationError("asin must contain 10 ASCII letters or digits")
    return asin


def register_review_product(
    conn: sqlite3.Connection, *, asin: str, source_scope: str
) -> str:
    """Register one server-known browse/product scope for local reviews.

    This is a privileged catalog operation, not a request-form operation.  It
    deliberately stores no offer, rating, review count, author, or excerpt.
    """

    normalized_asin = _normalize_asin(asin)
    if source_scope not in REVIEW_PRODUCT_SCOPES:
        raise ReviewValidationError("unsupported review product source scope")
    conn.execute(
        """
        INSERT INTO review_product_catalog(asin,source_scope)
        VALUES (?,?)
        ON CONFLICT(asin) DO UPDATE SET source_scope=excluded.source_scope
        """,
        (normalized_asin, source_scope),
    )
    return normalized_asin


def _normalize_session_digest(value: object) -> str:
    if not isinstance(value, str) or not value.strip():
        raise ReviewValidationError("session_digest must be non-empty text")
    return value.strip()


def _normalize_review_id(value: object) -> int:
    if isinstance(value, bool):
        raise ReviewValidationError("review_id must be a positive integer")
    if isinstance(value, int):
        review_id = value
    elif isinstance(value, str) and value.isascii() and value.isdecimal():
        review_id = int(value)
    else:
        raise ReviewValidationError("review_id must be a positive integer")
    if review_id <= 0:
        raise ReviewValidationError("review_id must be a positive integer")
    return review_id


def _normalize_rating(value: object) -> int:
    if isinstance(value, bool) or not isinstance(value, int) or not 1 <= value <= 5:
        raise ReviewValidationError("rating must be an integer from 1 to 5")
    return value


def _normalize_headline(value: object) -> str:
    if not isinstance(value, str):
        raise ReviewValidationError("headline must be text")
    headline = " ".join(value.split())
    if not headline:
        raise ReviewValidationError("headline must not be empty")
    if len(headline) > MAX_HEADLINE_LENGTH:
        raise ReviewValidationError(
            f"headline must be at most {MAX_HEADLINE_LENGTH} characters"
        )
    return headline


def _normalize_body(value: object) -> str:
    if not isinstance(value, str):
        raise ReviewValidationError("body must be text")
    body = value.replace("\r\n", "\n").replace("\r", "\n").strip()
    if not body:
        raise ReviewValidationError("body must not be empty")
    if len(body) > MAX_BODY_LENGTH:
        raise ReviewValidationError(
            f"body must be at most {MAX_BODY_LENGTH} characters"
        )
    return body


def _timestamp(value: str | None) -> str:
    if value is None:
        return (
            datetime.now(timezone.utc)
            .isoformat(timespec="seconds")
            .replace("+00:00", "Z")
        )
    if not isinstance(value, str) or not value.strip() or len(value.strip()) > 64:
        raise ReviewValidationError("at must be a non-empty timestamp string")
    return value.strip()


def _account_for_session(
    conn: sqlite3.Connection,
    session_digest: str,
    *,
    require_authenticated: bool,
) -> int | None:
    row = conn.execute(
        "SELECT account_id FROM browser_sessions WHERE session_digest=?",
        (session_digest,),
    ).fetchone()
    if row is None:
        raise ReviewNotFound("browser session does not exist")
    account_id = None if row[0] is None else int(row[0])
    if require_authenticated and account_id is None:
        raise ReviewAuthenticationRequired("sign in before writing a review")
    return account_id


def _product_exists(conn: sqlite3.Connection, asin: str) -> bool:
    row = conn.execute(
        """
        SELECT EXISTS(
            SELECT 1 FROM catalog_products WHERE asin=?
            UNION ALL
            SELECT 1 FROM commerce_offers WHERE asin=?
            UNION ALL
            SELECT 1 FROM review_product_catalog WHERE asin=?
        )
        """,
        (asin, asin, asin),
    ).fetchone()
    return bool(row and row[0])


def _review_query(
    conn: sqlite3.Connection,
    *,
    where_sql: str,
    where_params: tuple[object, ...],
    viewer_session_digest: str | None,
    order_sql: str,
) -> list[dict[str, Any]]:
    viewer_account_id: int | None = None
    viewer_guest_session: str | None = None
    if viewer_session_digest is not None:
        normalized_session = _normalize_session_digest(viewer_session_digest)
        viewer_account_id = _account_for_session(
            conn, normalized_session, require_authenticated=False
        )
        if viewer_account_id is None:
            viewer_guest_session = normalized_session

    cursor = conn.execute(
        f"""
        SELECT
            r.review_id,
            r.account_id AS reviewer_account_id,
            a.display_name AS author_display_name,
            r.rating,
            r.headline,
            r.body,
            r.created_at,
            r.updated_at,
            EXISTS(
                SELECT 1
                FROM orders o
                JOIN order_items oi ON oi.order_id=o.order_id
                LEFT JOIN shipments shipment ON shipment.order_id=o.order_id
                WHERE o.account_id=r.account_id
                  AND o.status='PLACED'
                  AND COALESCE(shipment.lifecycle_status,'PREPARING')<>'CANCELLED'
                  AND oi.asin=r.asin
            ) AS verified_purchase,
            (
                SELECT COUNT(*)
                FROM review_helpful_votes hv
                WHERE hv.review_id=r.review_id
            ) AS helpful_count,
            CASE
                WHEN ? IS NOT NULL THEN EXISTS(
                    SELECT 1 FROM review_helpful_votes own_vote
                    WHERE own_vote.review_id=r.review_id
                      AND own_vote.voter_account_id=?
                )
                WHEN ? IS NOT NULL THEN EXISTS(
                    SELECT 1 FROM review_helpful_votes own_vote
                    WHERE own_vote.review_id=r.review_id
                      AND own_vote.voter_session_digest=?
                )
                ELSE 0
            END AS viewer_found_helpful
        FROM product_reviews r
        JOIN accounts a ON a.account_id=r.account_id
        WHERE {where_sql}
        ORDER BY {order_sql}
        """,
        (
            viewer_account_id,
            viewer_account_id,
            viewer_guest_session,
            viewer_guest_session,
            *where_params,
        ),
    )
    names = [str(description[0]) for description in cursor.description]
    result: list[dict[str, Any]] = []
    for raw_row in cursor.fetchall():
        row = dict(zip(names, raw_row))
        reviewer_account_id = int(row.pop("reviewer_account_id"))
        owned_by_viewer = (
            viewer_account_id is not None
            and reviewer_account_id == viewer_account_id
        )
        result.append(
            {
                "id": str(row["review_id"]),
                "provenance": LOCAL_REVIEW_PROVENANCE,
                "author_display_name": str(row["author_display_name"]),
                "rating": int(row["rating"]),
                "title": str(row["headline"]),
                "body": str(row["body"]),
                "created_at": str(row["created_at"]),
                "updated_at": str(row["updated_at"]),
                "verified_purchase": bool(row["verified_purchase"]),
                "helpful_count": int(row["helpful_count"]),
                "viewer_found_helpful": bool(row["viewer_found_helpful"]),
                "owned_by_viewer": owned_by_viewer,
                "can_mark_helpful": not owned_by_viewer,
            }
        )
    return result


def upsert_review(
    conn: sqlite3.Connection,
    *,
    session_digest: str,
    asin: str,
    rating: int,
    headline: str,
    body: str,
    at: str | None = None,
) -> dict[str, Any]:
    """Create or update the signed-in account's one review for an ASIN.

    ``verified_purchase`` is intentionally absent from this signature.  The
    returned value is already shaped for ``review_catalog.normalize_local_reviews``.
    The caller owns commit/rollback.
    """

    normalized_session = _normalize_session_digest(session_digest)
    normalized_asin = _normalize_asin(asin)
    normalized_rating = _normalize_rating(rating)
    normalized_headline = _normalize_headline(headline)
    normalized_body = _normalize_body(body)
    timestamp = _timestamp(at)
    account_id = _account_for_session(
        conn, normalized_session, require_authenticated=True
    )
    assert account_id is not None
    if not _product_exists(conn, normalized_asin):
        raise ReviewNotFound("product does not exist")

    conn.execute(
        """
        INSERT INTO product_reviews(
            account_id,asin,rating,headline,body,created_at,updated_at
        ) VALUES (?,?,?,?,?,?,?)
        ON CONFLICT(account_id,asin) DO UPDATE SET
            rating=excluded.rating,
            headline=excluded.headline,
            body=excluded.body,
            updated_at=excluded.updated_at
        """,
        (
            account_id,
            normalized_asin,
            normalized_rating,
            normalized_headline,
            normalized_body,
            timestamp,
            timestamp,
        ),
    )
    rows = _review_query(
        conn,
        where_sql="r.account_id=? AND r.asin=?",
        where_params=(account_id, normalized_asin),
        viewer_session_digest=normalized_session,
        order_sql="r.review_id",
    )
    if len(rows) != 1:  # pragma: no cover - defensive database invariant
        raise ReviewSchemaError("review upsert did not produce one row")
    return rows[0]


def get_review(
    conn: sqlite3.Connection,
    review_id: int | str,
    *,
    viewer_session_digest: str | None = None,
) -> dict[str, Any] | None:
    """Return one review in review-catalog shape, or ``None`` if absent."""

    normalized_id = _normalize_review_id(review_id)
    rows = _review_query(
        conn,
        where_sql="r.review_id=?",
        where_params=(normalized_id,),
        viewer_session_digest=viewer_session_digest,
        order_sql="r.review_id",
    )
    return rows[0] if rows else None


def list_reviews(
    conn: sqlite3.Connection,
    asin: str,
    *,
    star: int | None = None,
    sort: str = REVIEW_SORT_RECENT,
    viewer_session_digest: str | None = None,
) -> list[dict[str, Any]]:
    """List local reviews, optionally filtering by star and sorting by recency/helpfulness."""

    normalized_asin = _normalize_asin(asin)
    if star is not None:
        normalized_star = _normalize_rating(star)
    else:
        normalized_star = None
    if sort not in REVIEW_SORTS:
        raise ReviewValidationError("sort must be 'recent' or 'helpful'")

    where_sql = "r.asin=?"
    params: tuple[object, ...] = (normalized_asin,)
    if normalized_star is not None:
        where_sql += " AND r.rating=?"
        params += (normalized_star,)
    order_sql = (
        "helpful_count DESC, r.created_at DESC, r.review_id DESC"
        if sort == REVIEW_SORT_HELPFUL
        else "r.created_at DESC, r.review_id DESC"
    )
    return _review_query(
        conn,
        where_sql=where_sql,
        where_params=params,
        viewer_session_digest=viewer_session_digest,
        order_sql=order_sql,
    )


def toggle_helpful_vote(
    conn: sqlite3.Connection,
    *,
    session_digest: str,
    review_id: int | str,
    at: str | None = None,
) -> dict[str, Any]:
    """Toggle one helpful vote for the authenticated account or guest session.

    A signed-in account has one durable identity across browser sessions.  An
    anonymous visitor is keyed by the server-created browser session.  Authors
    can never vote for their own review.  The caller owns the outer commit.
    """

    normalized_session = _normalize_session_digest(session_digest)
    normalized_id = _normalize_review_id(review_id)
    timestamp = _timestamp(at)
    voter_account_id = _account_for_session(
        conn, normalized_session, require_authenticated=False
    )
    review_row = conn.execute(
        "SELECT account_id FROM product_reviews WHERE review_id=?",
        (normalized_id,),
    ).fetchone()
    if review_row is None:
        raise ReviewNotFound("review does not exist")
    reviewer_account_id = int(review_row[0])
    if voter_account_id is not None and voter_account_id == reviewer_account_id:
        raise ReviewPermissionDenied("review authors cannot vote for their own review")

    conn.execute("SAVEPOINT amazon_review_vote")
    try:
        if voter_account_id is not None:
            cursor = conn.execute(
                """
                DELETE FROM review_helpful_votes
                WHERE review_id=? AND voter_account_id=?
                """,
                (normalized_id, voter_account_id),
            )
            found_helpful = cursor.rowcount == 0
            if found_helpful:
                conn.execute(
                    """
                    INSERT INTO review_helpful_votes(
                        review_id,voter_account_id,voter_session_digest,created_at
                    ) VALUES (?,?,NULL,?)
                    """,
                    (normalized_id, voter_account_id, timestamp),
                )
        else:
            cursor = conn.execute(
                """
                DELETE FROM review_helpful_votes
                WHERE review_id=? AND voter_account_id IS NULL
                  AND voter_session_digest=?
                """,
                (normalized_id, normalized_session),
            )
            found_helpful = cursor.rowcount == 0
            if found_helpful:
                conn.execute(
                    """
                    INSERT INTO review_helpful_votes(
                        review_id,voter_account_id,voter_session_digest,created_at
                    ) VALUES (?,NULL,?,?)
                    """,
                    (normalized_id, normalized_session, timestamp),
                )
        count_row = conn.execute(
            "SELECT COUNT(*) FROM review_helpful_votes WHERE review_id=?",
            (normalized_id,),
        ).fetchone()
        helpful_count = int(count_row[0])
    except Exception:
        conn.execute("ROLLBACK TO amazon_review_vote")
        conn.execute("RELEASE amazon_review_vote")
        raise
    else:
        conn.execute("RELEASE amazon_review_vote")
    return {
        "review_id": str(normalized_id),
        "found_helpful": found_helpful,
        "helpful_count": helpful_count,
    }


__all__ = [
    "LOCAL_REVIEW_PROVENANCE",
    "MAX_BODY_LENGTH",
    "MAX_HEADLINE_LENGTH",
    "REVIEW_SCHEMA_VERSION",
    "REVIEW_PRODUCT_SCOPES",
    "REVIEW_SORT_HELPFUL",
    "REVIEW_SORT_RECENT",
    "ReviewAuthenticationRequired",
    "ReviewNotFound",
    "ReviewPermissionDenied",
    "ReviewSchemaError",
    "ReviewStoreError",
    "ReviewValidationError",
    "get_review",
    "install_schema",
    "list_reviews",
    "register_review_product",
    "reset_review_data",
    "toggle_helpful_vote",
    "upsert_review",
]