File size: 60,680 Bytes
3e19b1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
"""Tenant-bound PDP workflows backed by typed projects and atomic ASIN part claims."""

from contextlib import contextmanager
from dataclasses import dataclass
from importlib import import_module
import base64
import hashlib
import io
import json
import re
import uuid
import zipfile
from xml.etree import ElementTree

from core import amazon_projects as projects

WORKFLOWS = 'amazon_pdp_workflows'
ITEMS = 'amazon_pdp_items'
CLAIMS = 'amazon_pdp_claims'
PARTS = 'amazon_pdp_parts'
PART_CHANGES = 'amazon_pdp_part_changes'
BRAND_TEMPLATES = 'amazon_pdp_brand_templates'
BRAND_TEXT = ('name', 'head', 'body', 'photo', 'voice')
BRAND_LISTS = ('logos', 'colors', 'extra', 'photos', 'rules', 'donts')
BRAND_MAX_BYTES = 50 * 1024 * 1024
BRAND_TYPES = {'pdf': 'application/pdf', 'png': 'image/png', 'jpg': 'image/jpeg',
               'jpeg': 'image/jpeg', 'webp': 'image/webp', 'svg': 'image/svg+xml',
               'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
               'pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation'}
TABLES = (WORKFLOWS, ITEMS, CLAIMS, PARTS)
OBSERVED_PARTS = ('title', 'bullets', 'images')
SCOPES = (
    {'key': 'images', 'label': 'Gallery images', 'slots': ('MAIN', *(f'PT{i:02}' for i in range(1, 9)), 'VIDEO'), 'role': 'image_approver'},
    {'key': 'title', 'label': 'Title', 'slots': ('TITLE',), 'role': 'copy_approver'},
    {'key': 'highlights', 'label': 'Item highlights', 'slots': ('HIGHLIGHTS',), 'role': 'copy_approver'},
    {'key': 'bullets', 'label': 'Bullet points', 'slots': ('BULLETS',), 'role': 'copy_approver'},
    {'key': 'desc', 'label': 'Description', 'slots': ('DESC',), 'role': 'copy_approver'},
    {'key': 'terms', 'label': 'Search terms', 'slots': ('TERMS',), 'role': 'copy_approver'},
    {'key': 'aplus', 'label': 'A+ content', 'slots': tuple(f'A{i}' for i in range(1, 6)), 'role': 'image_approver'},
    {'key': 'story', 'label': 'Brand Story', 'slots': ('STORY',), 'role': 'image_approver'},
    {'key': 'details', 'label': 'Product details', 'slots': ('DETAILS',), 'role': 'copy_approver'},
)
SCOPE_PARTS = tuple(row['key'] for row in SCOPES)
SLOTS = {slot: row for row in SCOPES for slot in row['slots']}
SLOT_ALIASES = {row['key']: row['slots'][0] for row in SCOPES if len(row['slots']) == 1}
ASSIGNEES = ('copy_approver', 'image_approver', 'publisher')
STAGE_PROGRESS = {'setup': 0, 'draft': 25, 'review': 50, 'publish': 75, 'published': 100}
ITEM_COLUMNS = ('asin', 'stage', 'published_on')
WRITER = 'amazon_pdp'
VIA = 'amazon_pdp'


class Refused(ValueError):
    """A named workflow refusal with plain copy."""

    def __init__(self, code, message):
        self.code = code
        super().__init__(message)


@dataclass(frozen=True)
class BoundRuntime:
    """Carry trusted, request bound readers without importing the API or harness."""

    runtime: object
    live: object = None
    validate: object = None
    competitors: object = None
    can_edit: object = None
    details: object = None

    def __getattr__(self, name):
        return getattr(self.runtime, name)


def bind(rt, *, live=None, validate=None, competitors=None, can_edit=None, details=None):
    """Bind server readers whose values cannot be supplied in an HTTP body."""
    return BoundRuntime(rt, live, validate, competitors, can_edit, details)


def _editor(rt, by):
    by = projects._text(by, 'by')
    if not isinstance(rt, BoundRuntime) or rt.can_edit is None or not rt.can_edit(by):
        raise Refused('forbidden', 'Only an editor of Command Center can change PDP parts.')
    return by


def _writable(item):
    if item['stage'] == 'published':
        raise Refused('published', 'This item is published and cannot be changed.')


def _slot(slot):
    slot = SLOT_ALIASES.get(slot, slot)
    if slot not in SLOTS:
        raise ValueError('Choose a PDP slot.')
    return slot


def _text_slot(part):
    if part not in SLOT_ALIASES:
        raise ValueError('Choose a text part.')
    return SLOT_ALIASES[part]


def _json(value):
    return json.loads(value) if isinstance(value, str) else (value or {})


def _slot_content(value):
    saved = _json(value)
    aliases = {SLOT_ALIASES.get(slot, slot): state for slot, state in saved.items()}
    return {**aliases, **{slot: state for slot, state in saved.items() if slot in SLOTS}}


class PartTaken(ValueError):
    """An open workflow already holds the requested ASIN part."""

    code = 'part_taken'

    def __init__(self, asin, part, workflow_id, name):
        self.asin, self.part, self.workflow_id = asin, part, workflow_id
        super().__init__(f'{asin} {part} is held by workflow "{name}" ({workflow_id}).')


def _asins(values):
    if not isinstance(values, (list, tuple)) or not values:
        raise ValueError('Choose at least one ASIN.')
    return list(dict.fromkeys(projects.target('asin', value)[1] for value in values))


def _scope(values):
    if not isinstance(values, (list, tuple)) or not values:
        raise ValueError('Choose at least one PDP part.')
    return list(dict.fromkeys(projects._choice(value, 'part', SCOPE_PARTS) for value in values))


def _backend(rt):
    return PgPdpStore(projects._backend(rt), rt)


def create(rt, *, name, asins, scope, listed=True, by=None, owner=None,
           priority='No priority', start_on=None, target_on=None,
           copy_approver=None, image_approver=None, publisher=None):
    """Create a project, workflow, setup items and part claims in one transaction."""
    if type(listed) is not bool:
        raise ValueError('listed must be true or false.')
    asins, scope = _asins(asins), _scope(scope)
    backend = _backend(rt)
    record = projects._fields(dict(name=name, status='In progress', priority=priority, owner=owner,
                                    start_on=start_on, target_on=target_on, progress=0, notes=None,
                                    completed_on=None))
    stamp = projects._now()
    record.update(id=uuid.uuid4().hex, tenant=backend.tenant_slug, type='pdp', listed=listed,
                  created_at=stamp, updated_at=stamp, source_id=None,
                  created_by=projects._text(by, 'by', optional=True))
    settings = {key: projects._text(value, key, optional=True)
                for key, value in zip(ASSIGNEES, (copy_approver, image_approver, publisher))}
    return backend.create(record, asins, scope, stamp, settings)


def get(rt, workflow_id):
    """Read a workflow and its items, including an unlisted project's row."""
    return _backend(rt).get(projects._text(workflow_id, 'workflow_id'))


def list_workflows(rt):
    """Read every tenant PDP workflow, including unlisted and published role sources."""
    return _backend(rt).list_all()


def set_stage(rt, workflow_id, asin, stage, *, by=None):
    """Change an item stage and derive project progress and completion from all items."""
    by = _editor(rt, by)
    return _backend(rt).set_stage(projects._text(workflow_id, 'workflow_id'),
                                  projects.target('asin', asin)[1],
                                  projects._choice(stage, 'stage', STAGE_PROGRESS), by=by)


def mark_published(rt, workflow_id, asin, *, by=None):
    """Complete one published item and free its claims in the same transaction."""
    projects._text(by, 'by', optional=True)
    return _backend(rt).set_stage(projects._text(workflow_id, 'workflow_id'),
                                  projects.target('asin', asin)[1], 'published', trusted=True)


def add_scope(rt, workflow_id, part, *, by):
    """Claim an additional family for every item without changing drafts or stages."""
    _editor(rt, by)
    return _backend(rt).add_scope(projects._text(workflow_id, 'workflow_id'), _scope([part])[0])


def record_part_change(rt, asin, part, day, *, source='publish'):
    """Record a proven publish date without inventing a content snapshot."""
    asin = projects.target('asin', asin)[1]
    part = projects._choice(part, 'part', SCOPE_PARTS)
    source = projects._choice(source, 'source', ('publish',))
    day = projects._date(day, 'day')
    if day is None:
        raise ValueError('day must use YYYY-MM-DD.')
    return _backend(rt).record_part_change(asin, part, day, source)


def observe_part_hashes(rt, snapshots, day):
    """Persist independent part hashes, leaving the first observation undated."""
    day = projects._date(day, 'day')
    if day is None:
        raise ValueError('day must use YYYY-MM-DD.')
    rows = [(projects.target('asin', asin)[1], projects._choice(part, 'part', OBSERVED_PARTS), digest)
            for asin, parts in snapshots.items() for part, digest in parts.items()]
    return _backend(rt).observe_part_hashes(rows, day) if rows else 0


def part_dates(rt, asins):
    """Read recorded part dates without creating schema or observing listings."""
    return _backend(rt).part_dates(_asins(asins)) if asins else {}


def settings(rt, workflow_id, values, *, by, project_values=None):
    """Save workflow role usernames with no inherited tenant defaults."""
    _editor(rt, by)
    if set(values) - set(ASSIGNEES):
        raise ValueError('Choose workflow approvers and publisher.')
    if set(project_values or {}) - {'name', 'owner', 'priority', 'start_on', 'target_on', 'notes'}:
        raise ValueError('Choose editable workflow settings.')
    values = {key: projects._text(value, key, optional=True) for key, value in values.items()}
    return _backend(rt).settings(workflow_id, values, project_values or {})


def brand(rt, workflow_id):
    """Read the workflow's stored kit dictionary without a second brand store."""
    workflow = get(rt, workflow_id)
    empty = {**dict.fromkeys(BRAND_TEXT, ''), **{key: [] for key in BRAND_LISTS}, 'files': [], 'drafted': 0}
    return {**empty, 'name': workflow['name'], **workflow['settings'].get('brand', {})}


def checked_brand(value, files):
    """Validate a complete kit and keep file identities under server control."""
    keys = {*BRAND_TEXT, *BRAND_LISTS, 'files', 'drafted'}
    if not isinstance(value, dict) or set(value) != keys:
        raise ValueError('Send every brand kit section.')
    clean = {}
    for key in BRAND_TEXT:
        text = value[key]
        if not isinstance(text, str) or len(text) > 12000:
            raise ValueError('Keep each brand description within 12,000 characters.')
        clean[key] = text.strip()
    for key in BRAND_LISTS:
        rows = value[key]
        if not isinstance(rows, list) or len(rows) > 200 or any(
                not isinstance(row, str) or len(row) > 12000 for row in rows):
            raise ValueError('Use text lists with at most 200 entries per brand section.')
        clean[key] = [row.strip() for row in rows]
    if any(color and not re.fullmatch(r'#[0-9a-fA-F]{6}', color) for key in ('colors', 'extra') for color in clean[key]):
        raise ValueError('Use six digit hex codes for colours.')
    images = {file['id'] for file in files if file['mime'].startswith('image/')}
    if any(identity not in images for key in ('logos', 'photos') for identity in clean[key]):
        raise ValueError('Choose logos and photos from this project\'s uploaded images.')
    wanted = value['files']
    if not isinstance(wanted, list) or any(not isinstance(row, dict) or row.get('id') not in
                                          {file['id'] for file in files} for row in wanted):
        raise ValueError('Choose files uploaded to this project.')
    retained = {row['id'] for row in wanted} | set(clean['logos']) | set(clean['photos'])
    stored = [file for file in files if file['id'] in retained]
    return {**clean, 'files': stored, 'drafted': sum(bool(file.get('read')) for file in stored)}


def save_brand(rt, workflow_id, value, *, by):
    """Save one complete canonical kit on its workflow."""
    _editor(rt, by)
    kit = checked_brand(value, brand(rt, workflow_id)['files'])
    return _backend(rt).settings(workflow_id, {'brand': kit}, {})


def brand_templates(rt):
    """Read all templates in the current tenant without creating schema."""
    return _backend(rt).brand_templates()


def save_brand_template(rt, workflow_id, *, by):
    """Keep an independent tenant template from the saved workflow kit."""
    _editor(rt, by)
    kit = brand(rt, workflow_id)
    if not kit['name'].strip():
        raise ValueError('Name the brand kit before saving a template.')
    return _backend(rt).save_brand_template(kit, by)


def load_brand_template(rt, workflow_id, template_id, *, by):
    """Copy template assets and values without changing the template."""
    _editor(rt, by)
    get(rt, workflow_id)
    kit = _backend(rt).brand_template(template_id)['kit']
    files = []
    for file in kit['files']:
        content = rt.download_bytes(file['path'])
        stored = _brand_file(rt, workflow_id, file['name'], file['mime'], content)
        files.append({**stored, 'read': file.get('read', False)})
    copied = checked_brand({**kit, 'files': files}, files)
    return _backend(rt).settings(workflow_id, {'brand': copied}, {})


def _office_text(data, extension):
    """Extract bounded Word paragraphs or ordered PowerPoint slides using standard library XML."""
    try:
        with zipfile.ZipFile(io.BytesIO(data)) as archive:
            members = archive.infolist()
            if len(members) > 2048 or sum(row.file_size for row in members) > 100 * 1024 * 1024:
                raise ValueError('This document is too large to read. Upload a smaller document.')
            pattern = r'word/document\.xml' if extension == 'docx' else r'ppt/slides/slide[0-9]+\.xml'
            selected = sorted((row for row in members if re.fullmatch(pattern, row.filename)),
                              key=lambda row: (len(row.filename), row.filename))
            if not selected or any(row.file_size > 4 * 1024 * 1024 or row.flag_bits & 1 for row in selected):
                raise ValueError('This document cannot be read. Upload a smaller, unencrypted document.')
            paragraphs = []
            for member in selected:
                raw = archive.read(member)
                if b'\0' in raw or b'<!DOCTYPE' in raw.upper() or b'<!ENTITY' in raw.upper():
                    raise ValueError('This document contains unsupported XML.')
                tree = ElementTree.fromstring(raw)
                paragraphs.extend(''.join(node.itertext()) for node in tree.iter() if node.tag.endswith('}t'))
                if sum(map(len, paragraphs)) > 200000:
                    raise ValueError('This document has too much text. Upload a shorter document.')
            return '\n'.join(paragraphs)
    except (zipfile.BadZipFile, ElementTree.ParseError, RuntimeError, NotImplementedError):
        raise ValueError('This document cannot be read. Upload a valid Word or PowerPoint file.') from None


def _brand_svg(data):
    tags = {'svg', 'g', 'path', 'rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'text', 'tspan',
            'defs', 'linearGradient', 'radialGradient', 'stop', 'clipPath', 'mask', 'pattern', 'symbol', 'use', 'title', 'desc'}
    xml = re.sub(rb'^\s*<\?xml\s+[^?]*\?>', b'', data, count=1)
    if (len(data) > 1024 * 1024 or b'\0' in data or b'<!DOCTYPE' in data.upper()
            or b'<!ENTITY' in data.upper() or b'<?' in xml):
        raise ValueError('Upload an SVG without scripts or external content, within 1 MB.')
    try:
        root = ElementTree.fromstring(data)
    except ElementTree.ParseError:
        raise ValueError('Upload a valid SVG file.') from None
    if root.tag != '{http://www.w3.org/2000/svg}svg':
        raise ValueError('Upload a valid SVG file.')
    for node in root.iter():
        if node.tag.removeprefix('{http://www.w3.org/2000/svg}') not in tags:
            raise ValueError('Upload an SVG without scripts or external content.')
        for key, value in node.attrib.items():
            key = key.split('}')[-1].lower()
            if (key.startswith('on') or key in ('base', 'src') or key == 'href' and not re.fullmatch(r'#[\w-]+', value)
                    or any(word in value.lower() for word in ('url(', 'javascript:', 'expression(', '\\', '@'))):
                raise ValueError('Upload an SVG without scripts or external content.')


def _brand_content(name, mime, data):
    if not isinstance(data, bytes) or not data:
        raise ValueError('Choose a file with content.')
    if len(data) > BRAND_MAX_BYTES:
        raise ValueError('Each file must be 50 MB or smaller.')
    extension = name.rsplit('.', 1)[-1].lower()
    if extension not in BRAND_TYPES or mime != BRAND_TYPES[extension]:
        raise ValueError('Upload PDF, Word, PowerPoint, PNG, JPG, SVG or WebP files.')
    if extension in ('docx', 'pptx'):
        _office_text(data, extension)
    elif extension == 'svg':
        _brand_svg(data)
    elif extension == 'pdf':
        if not data.startswith(b'%PDF-') or b'%%EOF' not in data[-2048:]:
            raise ValueError('Upload a valid PDF file.')
    else:
        from PIL import Image, UnidentifiedImageError
        try:
            with Image.open(io.BytesIO(data)) as image:
                if image.format != {'png': 'PNG', 'jpg': 'JPEG', 'jpeg': 'JPEG', 'webp': 'WEBP'}[extension]:
                    raise ValueError('The image type does not match its file name.')
                if image.width * image.height > 40000000:
                    raise ValueError('Use an image with at most 40 million pixels.')
                image.verify()
        except (UnidentifiedImageError, OSError, SyntaxError, Image.DecompressionBombError):
            raise ValueError('Upload a valid image file.') from None
    return extension


def _brand_file(rt, workflow_id, name, mime, data):
    name = projects._text(name, 'file name')
    if len(name) > 240 or '/' in name or '\\' in name:
        raise ValueError('Use a file name without folders, within 240 characters.')
    extension = _brand_content(name, mime, data)
    identity = hashlib.sha256(data).hexdigest()
    path = f'amazon_pdp/brand/{workflow_id}/{identity}.{extension}'
    rt.upload_bytes(path, data)
    return dict(id=identity, name=name, size=len(data), mime=mime, path=path, read=False)


def upload_brand_file(rt, workflow_id, name, mime, data, *, by, purpose='files'):
    """Validate and store bytes under one deterministic workflow prefix."""
    _editor(rt, by)
    purpose = projects._choice(purpose, 'file purpose', ('files', 'logos', 'photos'))
    kit = brand(rt, workflow_id)
    if purpose != 'files' and not mime.startswith('image/'):
        raise ValueError('Choose an image for a logo or example photo.')
    file = _brand_file(rt, workflow_id, name, mime, data)
    file['read'] = next((row.get('read', False) for row in kit['files'] if row['id'] == file['id']), False)
    files = [row for row in kit['files'] if row['id'] != file['id']] + [file]
    kit = {**kit, 'files': files}
    if purpose != 'files':
        kit[purpose] = list(dict.fromkeys([*kit[purpose], file['id']]))
    return _backend(rt).settings(workflow_id, {'brand': checked_brand(kit, files)}, {})


def brand_file(rt, workflow_id, file_id):
    """Read only an asset retained by this workflow."""
    file = next((file for file in brand(rt, workflow_id)['files'] if file['id'] == file_id), None)
    if file is None:
        raise KeyError('Brand file not found.')
    return file, rt.download_bytes(file['path'])


def brand_draft_inputs(rt, workflow_id):
    """Keep PDFs and images as typed inputs and Office archives as extracted text."""
    files = brand(rt, workflow_id)['files']
    if not files:
        raise ValueError('Upload brand files before writing the kit.')
    if sum(file['size'] for file in files) > 60 * 1024 * 1024:
        raise ValueError('Choose at most 60 MB of brand files for one draft.')
    content = []
    for file in files:
        data = rt.download_bytes(file['path'])
        extension = _brand_content(file['name'], file['mime'], data)
        if hashlib.sha256(data).hexdigest() != file['id']:
            raise Refused('brand_file_changed', 'A brand file changed. Upload it again.')
        if extension in ('docx', 'pptx'):
            content.append({'type': 'text', 'text': file['name'] + '\n' + _office_text(data, extension)})
            continue
        uri = f"data:{file['mime']};base64," + base64.b64encode(data).decode('ascii')
        content.append({'type': 'image_url', 'image_url': {'url': uri}} if file['mime'].startswith('image/') else
                       {'type': 'file', 'file': {'filename': file['name'], 'file_data': uri}})
    return content


def page(rt, workflow_id, asin, *, user):
    """Read live facts and all scoped slot states without writing."""
    return _backend(rt).page(workflow_id, projects.target('asin', asin)[1], user=user)


def part_state(rt, workflow_id, asin, slot, *, user):
    """Read one slot through the same state model used by the page and publisher."""
    slot = _slot(slot)
    return next(row for row in page(rt, workflow_id, asin, user=user)['parts'] if row['slot'] == slot)


def text_part(rt, workflow_id, asin, part, *, user):
    """Read a text part through the shared slot model."""
    return part_state(rt, workflow_id, asin, _text_slot(part), user=user)


def save_text_draft(rt, workflow_id, asin, part, value, *, option_id, by):
    """Save text through the same transition used by image selections."""
    return save_draft(rt, workflow_id, asin, _text_slot(part), value, option_id=option_id, by=by)


def save_draft(rt, workflow_id, asin, slot, value, *, option_id=None, by):
    """Save or clear one slot, removing its approval and resolved request."""
    return _backend(rt).mutate(workflow_id, projects.target('asin', asin)[1], slot,
                               'draft', by=_editor(rt, by), value=value, option_id=option_id)


def _details_provider():
    try:
        return import_module('core.amazon_listing_attributes')
    except ModuleNotFoundError as exc:
        if exc.name != 'core.amazon_listing_attributes':
            raise
        raise Refused('details_provider_unavailable', 'Product details are unavailable. Try again later.') from None


def details_problems(attrs, *, title=''):
    """Apply the shared physical checks to canonical editor values and a trusted listing title."""
    provider = _details_provider()
    if not isinstance(attrs, dict) or set(attrs) - provider.EDITOR_KEYS or any(
            not isinstance(value, str) for value in attrs.values()):
        raise Refused('invalid_details', 'Send product attributes as text.')
    return provider.pd_flags(title, attrs)


def _details_schema(rt, workflow_id, asin):
    provider = _details_provider()
    if not isinstance(rt, BoundRuntime) or rt.details is None:
        raise Refused('details_provider_unavailable', 'Product details are unavailable. Try again later.')
    schema = rt.details(workflow_id, asin)
    if not isinstance(schema, provider.DetailsSchema):
        raise Refused('details_schema_unavailable', 'Product details schema is unavailable. Try again later.')
    return provider, schema


def _details_values(schema, attrs, *, draft=False):
    provider = _details_provider()
    try:
        if draft:
            if not isinstance(attrs, dict):
                raise provider.DetailFailure('InvalidDetailsDraft')
            for name, values in attrs.items():
                schema.validate_attribute(name, values)
        return schema.render_all(attrs)
    except provider.DetailFailure as exc:
        raise Refused('invalid_details', f'Product attribute refused: {exc.reason}'
                      + (f' ({exc.field}).' if exc.field else '.')) from None


def _details_draft(rt, workflow_id, asin, value, facts):
    provider, schema = _details_schema(rt, workflow_id, asin)
    if value is None or value == {}:
        return None
    try:
        patch = schema.patch(value)
        raw = {**facts['slots']['DETAILS'],
               **{row['path'].removeprefix('/attributes/'): row['value'] for row in patch['patches']}}
        live = _details_values(schema, facts['slots']['DETAILS'])
        return None if _details_values(schema, raw) == live else raw
    except provider.DetailFailure as exc:
        raise Refused('invalid_details', f'Product attribute refused: {exc.reason}'
                      + (f' ({exc.field}).' if exc.field else '.')) from None


def details_part(rt, workflow_id, asin, *, user):
    """Expose editor strings separately from the canonical slot's raw attribute arrays."""
    state = part_state(rt, workflow_id, asin, 'DETAILS', user=user)
    _provider, schema = _details_schema(rt, workflow_id, asin)
    live = _details_values(schema, state['live'])
    draft = _details_values(schema, state['draft'], draft=True) if state['draft'] is not None else {}
    facts = _facts(rt, workflow_id, asin)
    return dict(state=state, live=live, draft=draft,
                checks=details_problems({**live, **draft}, title=facts['slots'].get('TITLE') or ''),
                product_type=schema.product_type, marketplace_id=schema.marketplace,
                unsupported=list(schema.unsupported),
                attributes={key: field['attribute'] for key, field in schema.fields.items()})


def save_details_draft(rt, workflow_id, asin, draft, *, by):
    """Save editor changes through the shared guarded slot transition and return canonical text."""
    if not isinstance(draft, dict):
        raise Refused('invalid_details', 'Send draft product attributes as an object.')
    save_draft(rt, workflow_id, asin, 'DETAILS', draft, by=by)
    return details_part(rt, workflow_id, asin, user=by)


def approve(rt, workflow_id, asin, slot, *, by):
    """Approve a changed slot only as its assigned role and never as its drafter."""
    return _backend(rt).mutate(workflow_id, projects.target('asin', asin)[1], slot,
                               'approve', by=_editor(rt, by))


def request_changes(rt, workflow_id, asin, slot, note, *, by):
    """Keep Review and attach the assigned approver's request to one slot."""
    note = projects._text(note, 'request', limit=4000)
    return _backend(rt).mutate(workflow_id, projects.target('asin', asin)[1], slot,
                               'request', by=_editor(rt, by), value=note)


def clear_approval(rt, workflow_id, asin, slot, *, by):
    """Invalidate a slot approval when an image provider changes its selected content."""
    return _backend(rt).mutate(workflow_id, projects.target('asin', asin)[1], slot,
                               'clear_approval', by=_editor(rt, by))


def save_competitors(rt, workflow_id, asin, selected, *, by):
    """Save selected competitor identities resolved by the trusted source."""
    by = _editor(rt, by)
    selected = _asins(selected) if selected else []
    return _backend(rt).save_competitors(workflow_id, projects.target('asin', asin)[1], selected, by=by)


def publish_snapshot(rt, workflow_id, asin, *, user):
    """Read the publisher and every scoped slot from the canonical part model."""
    result = page(rt, workflow_id, asin, user=user)
    fields = ('part', 'slot', 'live', 'draft', 'changed', 'approved_by', 'requested', 'fails', 'option_id')
    return {key: result[key] for key in ('workflow_id', 'asin', 'sku', 'stage', 'publisher')} | {
        'parts': [{key: row[key] for key in fields} for row in result['parts'] if row['part'] in result['scope']]}


def _facts(rt, workflow_id, asin):
    if not isinstance(rt, BoundRuntime) or rt.live is None:
        return {'slots': {}, 'unavailable': {}, 'sku': None, 'last_updated': None}
    return rt.live(workflow_id, asin)


def _fails(rt, workflow_id, asin, slot, value, facts):
    reason = facts.get('unavailable', {}).get(slot)
    if reason or slot not in facts['slots'] or not facts.get('sku') or not facts.get('last_updated'):
        return [reason or 'Live listing facts are unavailable for this part.']
    if slot == 'DETAILS':
        try:
            _provider, schema = _details_schema(rt, workflow_id, asin)
            values = _details_values(schema, value, draft=True)
            return list(details_problems(values, title=facts['slots'].get('TITLE') or '').values())
        except Refused as exc:
            return [str(exc)]
    if not isinstance(rt, BoundRuntime) or rt.validate is None:
        return ['Rules are unavailable for this part.']
    fails = rt.validate(workflow_id, asin, slot, value)
    if not isinstance(fails, list) or any(not isinstance(reason, str) or not reason.strip() for reason in fails):
        return ['Rules are unavailable for this part.']
    return fails


def _state(rt, workflow, item, slot, saved, facts, user):
    part = SLOTS[slot]['key']
    live = facts['slots'].get(slot)
    changed = saved.get('draft') is not None and saved['draft'] != live
    requested = saved.get('requested')
    approved = saved.get('approved_by') if changed and not requested else None
    editable_stage = item['stage'] == 'draft' or (item['stage'] == 'review' and bool(requested))
    editor = isinstance(rt, BoundRuntime) and rt.can_edit is not None and rt.can_edit(user)
    fails = _fails(rt, workflow['id'], item['asin'], slot, saved['draft'], facts) if changed else []
    label = ('Changes requested' if requested else 'No change' if not changed else
             'Fails rules' if fails else 'Approved' if approved else 'Saved draft')
    return dict(workflow_id=workflow['id'], asin=item['asin'], part=part, slot=slot, stage=item['stage'],
                scope=workflow['scope'], editable=bool(editor and part in workflow['scope'] and editable_stage),
                requested=requested, live=live, draft=saved['draft'] if changed else None, state=label,
                draft_by=saved.get('draft_by') if changed else None, approved_by=approved, fails=fails,
                option_id=saved.get('option_id') if changed else None, changed=changed,
                unavailable=facts.get('unavailable', {}).get(slot))


def _draft_value(saved, value, live, option_id, by):
    """Every draft save, clear and reselection invalidates its approval."""
    result = dict(saved)
    result['approved_by'] = None
    if value is None or value == live:
        return {}
    return {**result, 'draft': value, 'draft_by': by, 'option_id': option_id, 'requested': None}


def _approve_state(workflow, state, by):
    if state['draft_by'] == by:
        raise Refused('self_approval', 'The drafter cannot approve this part.')
    if by != workflow.get(SLOTS[state['slot']]['role']):
        raise Refused('wrong_approver', 'Only the assigned approver can review this part.')
    if not state['changed']:
        raise Refused('no_change', 'Save a changed draft before approving it.')
    if state['fails']:
        raise Refused('fails_rules', 'Fix the failing rules before approving this part.')
    if state['requested']:
        raise Refused('changes_requested', 'Resolve the request before approving this part.')


def _stage_rules(workflow, item, stage, states):
    current = item['stage']
    if stage == 'published':
        raise Refused('trusted_publish_required', 'Publish this item through the publish action.')
    if current == 'setup' and stage == 'draft' and not workflow['settings'].get('competitors', {}).get(item['asin']):
        raise Refused('no_competitor', 'Save at least one competitor before starting Draft.')
    if stage == current or stage in ('setup', 'draft'):
        return
    changed = [state for state in states if state['changed']]
    if stage == 'review' and current == 'draft':
        if not changed:
            raise Refused('no_change', 'Save a changed draft before submitting for review.')
        if any(state['fails'] for state in changed):
            raise Refused('fails_rules', 'Fix the failing rules before submitting for review.')
    elif stage == 'publish' and current == 'review':
        if not changed or any(state['requested'] for state in states) or any(not s['approved_by'] for s in changed):
            raise Refused('unapproved', 'Approve every changed part and resolve every request before publishing.')
        if any(state['fails'] for state in changed):
            raise Refused('fails_rules', 'Fix the failing rules before publishing.')
    elif not (stage == 'review' and current == 'publish'):
        raise Refused('invalid_stage', 'Move this item through Setup, Draft, Review and Publish in order.')


def delete(rt, workflow_id, *, by=None):
    """Delete a workflow and its project atomically and return its touched ASINs."""
    projects._text(by, 'by', optional=True)
    return _backend(rt).delete(projects._text(workflow_id, 'workflow_id'))


def rows(rt, asins):
    """Read open workflow items for the requested ASINs without creating schema."""
    return _backend(rt).rows(_asins(asins))


def taken(rt, asins):
    """Read the tenant's named part holders for the requested ASINs."""
    return _backend(rt).taken(_asins(asins))


def add_items(rt, workflow_id, asins, *, by=None):
    """Join ASINs and claim their workflow parts atomically."""
    projects._text(by, 'by', optional=True)
    return _backend(rt).change_items(projects._text(workflow_id, 'workflow_id'), _asins(asins), remove=False)


def remove_items(rt, workflow_id, asins, *, by=None):
    """Remove ASINs, their targets and their workflow graph atomically."""
    projects._text(by, 'by', optional=True)
    return _backend(rt).change_items(projects._text(workflow_id, 'workflow_id'), _asins(asins), remove=True)


def _purge_images(rt, workflow_id):
    try:
        images = import_module('core.pdp_images')
    except ModuleNotFoundError as exc:
        if exc.name != 'core.pdp_images':
            raise
        return
    images.purge(rt, workflow_id)


class PgPdpStore:
    """Reuse the projects backend, tenant guard and transaction for PDP authorship."""

    def __init__(self, backend, runtime=None):
        self.projects = backend
        self.rt = runtime
        self.tenant_slug = backend.tenant_slug
        self.t = backend.t

    def _exists(self, con):
        return con.execute('SELECT to_regclass(%s)', (self.t(WORKFLOWS),)).fetchone()[0] is not None

    def _ensure_part_changes(self, con):
        con.execute(f'''CREATE TABLE IF NOT EXISTS {self.t(PART_CHANGES)} (
            tenant TEXT NOT NULL, asin TEXT NOT NULL,
            part TEXT NOT NULL CHECK (part IN ({projects._in_sql(SCOPE_PARTS)})),
            content_hash TEXT, observed_on DATE, changed_on DATE, source TEXT,
            PRIMARY KEY (tenant, asin, part))''')

    def record_part_change(self, asin, part, day, source):
        self.projects._write_guard()
        with self.projects._connection() as con:
            self._ensure_part_changes(con)
            con.execute(f'''INSERT INTO {self.t(PART_CHANGES)}
                (tenant, asin, part, changed_on, source) VALUES (%s, %s, %s, %s, %s)
                ON CONFLICT (tenant, asin, part) DO UPDATE
                SET changed_on = EXCLUDED.changed_on, source = EXCLUDED.source
                WHERE changed_on IS NULL OR changed_on <= EXCLUDED.changed_on''',
                        (self.tenant_slug, asin, part, day, source))

    def observe_part_hashes(self, rows, day):
        self.projects._write_guard()
        changed = ("content_hash IS NOT NULL AND content_hash <> EXCLUDED.content_hash "
                   "AND (changed_on IS NULL OR changed_on <= EXCLUDED.observed_on)")
        with self.projects._connection() as con:
            self._ensure_part_changes(con)
            for asin, part, digest in rows:
                con.execute(f'''INSERT INTO {self.t(PART_CHANGES)}
                    (tenant, asin, part, content_hash, observed_on) VALUES (%s, %s, %s, %s, %s)
                    ON CONFLICT (tenant, asin, part) DO UPDATE
                    SET content_hash = EXCLUDED.content_hash, observed_on = EXCLUDED.observed_on,
                        changed_on = CASE WHEN {changed} THEN EXCLUDED.observed_on ELSE changed_on END,
                        source = CASE WHEN {changed} THEN 'snapshot' ELSE source END
                    WHERE observed_on IS NULL OR observed_on <= EXCLUDED.observed_on''',
                            (self.tenant_slug, asin, part, digest, day))
        return len(rows)

    def part_dates(self, asins):
        with self.projects._connection() as con:
            if con.execute('SELECT to_regclass(%s)', (self.t(PART_CHANGES),)).fetchone()[0] is None:
                return {}
            rows = []
            for offset in range(0, len(asins), 2048):
                batch = asins[offset:offset + 2048]
                marks = ', '.join('%s' for _ in batch)
                rows.extend(con.execute(f'''SELECT asin, part, changed_on FROM {self.t(PART_CHANGES)}
                    WHERE tenant = %s AND asin IN ({marks}) AND changed_on IS NOT NULL''',
                                        (self.tenant_slug, *batch)).fetchall())
        result = {}
        for asin, part, day in rows:
            result.setdefault(asin, {})[part + '_last_updated'] = projects._date(day, 'changed_on')
        return result

    def ensure_schema(self, con):
        con.execute(f'''CREATE TABLE IF NOT EXISTS {self.t(WORKFLOWS)} (
            tenant TEXT NOT NULL, id TEXT NOT NULL, scope JSONB NOT NULL,
            PRIMARY KEY (tenant, id),
            FOREIGN KEY (tenant, id) REFERENCES {self.t(projects.PROJECTS)} (tenant, id)
                ON DELETE CASCADE)''')
        con.execute(f"ALTER TABLE {self.t(WORKFLOWS)} ADD COLUMN IF NOT EXISTS settings JSONB NOT NULL DEFAULT '{{}}'")
        con.execute(f'''CREATE TABLE IF NOT EXISTS {self.t(ITEMS)} (
            tenant TEXT NOT NULL, workflow_id TEXT NOT NULL, asin TEXT NOT NULL,
            stage TEXT NOT NULL CHECK (stage IN ({projects._in_sql(STAGE_PROGRESS)})),
            published_on DATE,
            PRIMARY KEY (tenant, workflow_id, asin),
            FOREIGN KEY (tenant, workflow_id) REFERENCES {self.t(WORKFLOWS)} (tenant, id)
                ON DELETE CASCADE,
            CHECK ((stage = 'published' AND published_on IS NOT NULL)
                OR (stage <> 'published' AND published_on IS NULL)))''')
        con.execute(f'''CREATE TABLE IF NOT EXISTS {self.t(CLAIMS)} (
            tenant TEXT NOT NULL, asin TEXT NOT NULL, part TEXT NOT NULL,
            workflow_id TEXT NOT NULL, CHECK (part IN ({projects._in_sql(SCOPE_PARTS)})),
            PRIMARY KEY (tenant, asin, part),
            FOREIGN KEY (tenant, workflow_id, asin)
                REFERENCES {self.t(ITEMS)} (tenant, workflow_id, asin) ON DELETE CASCADE)''')
        con.execute(f'CREATE INDEX IF NOT EXISTS "amazon_pdp_claims_workflow" '
                    f'ON {self.t(CLAIMS)} (tenant, workflow_id, asin)')
        con.execute(f'''CREATE TABLE IF NOT EXISTS {self.t(PARTS)} (
            tenant TEXT NOT NULL, workflow_id TEXT NOT NULL, asin TEXT NOT NULL,
            part TEXT NOT NULL CHECK (part IN ({projects._in_sql(SCOPE_PARTS)})),
            content JSONB NOT NULL DEFAULT '{{}}', updated_at TEXT NOT NULL,
            PRIMARY KEY (tenant, workflow_id, asin, part),
            FOREIGN KEY (tenant, workflow_id, asin)
                REFERENCES {self.t(ITEMS)} (tenant, workflow_id, asin) ON DELETE CASCADE)''')

    @contextmanager
    def _write(self):
        with self.projects._write() as con:
            self.ensure_schema(con)
            yield con

    def _workflow(self, con, workflow_id, *, lock=False):
        if not self.projects._exists(con) or not self._exists(con):
            raise KeyError('PDP workflow not found.')
        project = self.projects._one(con, workflow_id, lock=lock)
        row = con.execute(f"SELECT scope, to_jsonb(w)->'settings' FROM {self.t(WORKFLOWS)} w "
                          'WHERE tenant = %s AND id = %s',
                          (self.tenant_slug, workflow_id)).fetchone()
        if project['type'] != 'pdp' or row is None:
            raise KeyError('PDP workflow not found.')
        config = _json(row[1])
        return dict(project, scope=_json(row[0]), settings=config,
                    **{key: config.get(key) for key in ASSIGNEES})

    def _items(self, con, workflow_id):
        rows = con.execute(f'SELECT {", ".join(ITEM_COLUMNS)} FROM {self.t(ITEMS)} '
                           'WHERE tenant = %s AND workflow_id = %s ORDER BY asin',
                           (self.tenant_slug, workflow_id)).fetchall()
        return [dict(asin=row[0], stage=row[1], published_on=projects._date(row[2], 'published_on'))
                for row in rows]

    def _read(self, con, workflow_id):
        return dict(self._workflow(con, workflow_id), items=self._items(con, workflow_id))

    def get(self, workflow_id):
        with self.projects._connection() as con:
            return self._read(con, workflow_id)

    def list_all(self):
        with self.projects._connection() as con:
            if not self.projects._exists(con) or not self._exists(con):
                return []
            cursor = con.execute(f'SELECT w.id FROM {self.t(WORKFLOWS)} w '
                                 f'JOIN {self.t(projects.PROJECTS)} p ON p.tenant = w.tenant AND p.id = w.id '
                                 "WHERE w.tenant = %s AND p.type = 'pdp' ORDER BY p.created_at, p.id",
                                 (self.tenant_slug,))
            records = []
            for batch in iter(lambda: cursor.fetchmany(100), []):
                records.extend(self._read(con, row[0]) for row in batch)
            return records

    def _context(self, con, workflow_id, asin, *, lock=False):
        workflow = self._workflow(con, workflow_id, lock=lock)
        item = next((row for row in self._items(con, workflow_id) if row['asin'] == asin), None)
        if item is None:
            raise KeyError('ASIN not found in this PDP workflow.')
        return workflow, item

    def _content(self, con, workflow_id, asin):
        rows = con.execute(f'SELECT part, content FROM {self.t(PARTS)} '
                           'WHERE tenant = %s AND workflow_id = %s AND asin = %s',
                           (self.tenant_slug, workflow_id, asin)).fetchall()
        return {part: _slot_content(value) for part, value in rows}

    def _states(self, con, workflow, item, user):
        content = self._content(con, workflow['id'], item['asin'])
        facts = _facts(self.rt, workflow['id'], item['asin'])
        states = [_state(self.rt, workflow, item, slot, content.get(row['key'], {}).get(slot, {}), facts, user)
                  for slot, row in SLOTS.items()]
        return states, facts

    def page(self, workflow_id, asin, *, user):
        with self.projects._connection() as con:
            workflow, item = self._context(con, workflow_id, asin)
            states, facts = self._states(con, workflow, item, user)
            return dict(workflow_id=workflow_id, asin=asin, stage=item['stage'], sku=facts.get('sku'),
                        last_updated=facts.get('last_updated'), scope=workflow['scope'], parts=states,
                        publisher=workflow['publisher'],
                        competitors=workflow['settings'].get('competitors', {}).get(asin, []))

    def _save_settings(self, con, workflow_id, config):
        con.execute(f'UPDATE {self.t(WORKFLOWS)} SET settings = %s::jsonb WHERE tenant = %s AND id = %s',
                    (json.dumps(config), self.tenant_slug, workflow_id))

    def settings(self, workflow_id, values, project_values):
        with self._write() as con:
            workflow = self._workflow(con, workflow_id, lock=True)
            for item in self._items(con, workflow_id):
                _writable(item)
            candidate = projects._fields(project_values, previous=workflow)
            config = {**workflow['settings'], **values}
            if (any(workflow[key] != config.get(key) for key in ASSIGNEES[:2])
                    or workflow['settings'].get('brand') != config.get('brand')):
                for item in self._items(con, workflow_id):
                    self._clear_approvals(con, workflow_id, item['asin'])
            self._save_settings(con, workflow_id, config)
            fields = sorted(project_values)
            con.execute(f'UPDATE {self.t(projects.PROJECTS)} SET '
                        + ''.join(f'{key} = %s, ' for key in fields)
                        + 'updated_at = %s WHERE tenant = %s AND id = %s',
                        tuple(candidate[key] for key in fields) + (projects._now(), self.tenant_slug, workflow_id))
            return self._read(con, workflow_id)

    def brand_templates(self):
        with self.projects._connection() as con:
            if con.execute('SELECT to_regclass(%s)', (self.t(BRAND_TEMPLATES),)).fetchone()[0] is None:
                return []
            cursor = con.execute(f'SELECT id, name FROM {self.t(BRAND_TEMPLATES)} '
                                 'WHERE tenant = %s ORDER BY name, id', (self.tenant_slug,))
            return [dict(id=row[0], name=row[1]) for batch in iter(lambda: cursor.fetchmany(100), []) for row in batch]

    def brand_template(self, template_id):
        projects._text(template_id, 'template id')
        with self.projects._connection() as con:
            if con.execute('SELECT to_regclass(%s)', (self.t(BRAND_TEMPLATES),)).fetchone()[0] is not None:
                row = con.execute(f'SELECT kit FROM {self.t(BRAND_TEMPLATES)} WHERE tenant = %s AND id = %s',
                                  (self.tenant_slug, template_id)).fetchone()
                if row:
                    return {'id': template_id, 'kit': _json(row[0])}
        raise KeyError('Brand template not found.')

    def save_brand_template(self, kit, by):
        identity = hashlib.sha256(json.dumps(kit, sort_keys=True).encode()).hexdigest()
        with self._write() as con:
            con.execute(f'''CREATE TABLE IF NOT EXISTS {self.t(BRAND_TEMPLATES)} (
                tenant TEXT NOT NULL, id TEXT NOT NULL, name TEXT NOT NULL, kit JSONB NOT NULL,
                created_at TEXT NOT NULL, created_by TEXT NOT NULL, PRIMARY KEY (tenant, id))''')
            con.execute(f'''INSERT INTO {self.t(BRAND_TEMPLATES)}
                (tenant, id, name, kit, created_at, created_by) VALUES (%s, %s, %s, %s, %s, %s)
                ON CONFLICT (tenant, id) DO NOTHING''',
                        (self.tenant_slug, identity, kit['name'], json.dumps(kit), projects._now(), by))
        return {'id': identity, 'name': kit['name']}

    def save_competitors(self, workflow_id, asin, selected, *, by):
        with self._write() as con:
            workflow, item = self._context(con, workflow_id, asin, lock=True)
            _writable(item)
            if item['stage'] != 'setup':
                raise Refused('setup_only', 'Choose competitors in Setup.')
            if selected and (not isinstance(self.rt, BoundRuntime) or self.rt.competitors is None):
                raise Refused('competitors_unavailable', 'Saved competitors are unavailable. Try again later.')
            found = self.rt.competitors(workflow_id, asin, selected) if selected else []
            if not isinstance(found, list) or {row.get('asin') for row in found} != set(selected):
                raise Refused('invalid_competitor', 'Choose competitors saved for this item.')
            config = dict(workflow['settings'])
            config['competitors'] = {**config.get('competitors', {}), asin: found}
            self._save_settings(con, workflow_id, config)
            return self._read(con, workflow_id)

    def _save_content(self, con, workflow_id, asin, part, content):
        con.execute(f'UPDATE {self.t(PARTS)} SET content = %s::jsonb, updated_at = %s '
                    'WHERE tenant = %s AND workflow_id = %s AND asin = %s AND part = %s',
                    (json.dumps(content), projects._now(), self.tenant_slug, workflow_id, asin, part))

    def _clear_approvals(self, con, workflow_id, asin):
        for part, content in self._content(con, workflow_id, asin).items():
            if any(saved.get('approved_by') for saved in content.values()):
                self._save_content(con, workflow_id, asin, part,
                                   {slot: {**saved, 'approved_by': None} for slot, saved in content.items()})

    def mutate(self, workflow_id, asin, slot, action, *, by, value=None, option_id=None):
        slot = _slot(slot)
        family = SLOTS[slot]
        option_id = projects._text(option_id, 'option_id', optional=True)
        with self._write() as con:
            workflow, item = self._context(con, workflow_id, asin, lock=True)
            _writable(item)
            if family['key'] not in workflow['scope']:
                raise Refused('out_of_scope', 'This part is outside the workflow scope.')
            content = self._content(con, workflow_id, asin)[family['key']]
            saved = content.get(slot, {})
            facts = _facts(self.rt, workflow_id, asin)
            state = _state(self.rt, workflow, item, slot, saved, facts, by)
            updated = self._transition(workflow, state, saved, action, by, value, option_id, facts)
            if updated:
                content[slot] = updated
            else:
                content.pop(slot, None)
            self._save_content(con, workflow_id, asin, family['key'], content)
            return _state(self.rt, workflow, item, slot, updated, facts, by)

    def _transition(self, workflow, state, saved, action, by, value, option_id, facts):
        if action in ('draft', 'clear_approval'):
            if not state['editable']:
                raise Refused('not_editable', 'Edit in Draft or fix a requested part in Review.')
            if action == 'clear_approval':
                return {**saved, 'approved_by': None} if saved else {}
            if state['slot'] not in facts['slots'] or facts.get('unavailable', {}).get(state['slot']):
                raise Refused('live_unavailable', 'Live listing facts are unavailable for this part.')
            if state['slot'] == 'DETAILS':
                value = _details_draft(self.rt, workflow['id'], state['asin'], value, facts)
            updated = _draft_value(saved, value, state['live'], option_id, by)
            if updated and state['slot'] == 'DETAILS':
                updated['fails'] = _fails(self.rt, workflow['id'], state['asin'], state['slot'], value, facts)
            return updated
        if state['stage'] != 'review':
            raise Refused('review_only', 'Review this part in Review.')
        if action == 'approve':
            _approve_state(workflow, state, by)
            return {**saved, 'approved_by': by}
        if by != workflow[SLOTS[state['slot']]['role']]:
            raise Refused('wrong_approver', 'Only the assigned approver can review this part.')
        return {**saved, 'requested': value}

    def rows(self, asins):
        found = {asin: [] for asin in asins}
        with self.projects._connection() as con:
            if not self.projects._exists(con) or not self._exists(con):
                return found
            placeholders = ', '.join(['%s'] * len(asins))
            items = con.execute(f'SELECT i.asin, i.workflow_id, p.name, i.stage '
                                f'FROM {self.t(ITEMS)} i JOIN {self.t(projects.PROJECTS)} p '
                                'ON p.tenant = i.tenant AND p.id = i.workflow_id '
                                f'WHERE i.tenant = %s AND i.asin IN ({placeholders}) '
                                "AND i.stage <> 'published' ORDER BY p.created_at, p.id",
                                (self.tenant_slug, *asins)).fetchall()
            for asin, workflow_id, name, stage in items:
                found[asin].append(dict(id=workflow_id, name=name, stage=stage, progress=STAGE_PROGRESS[stage]))
        return found

    def taken(self, asins):
        with self.projects._connection() as con:
            if not self.projects._exists(con) or not self._exists(con):
                return []
            placeholders = ', '.join(['%s'] * len(asins))
            claims = con.execute(f'SELECT c.asin, c.part, c.workflow_id, p.name FROM {self.t(CLAIMS)} c '
                                 f'JOIN {self.t(projects.PROJECTS)} p '
                                 'ON p.tenant = c.tenant AND p.id = c.workflow_id '
                                 f'WHERE c.tenant = %s AND c.asin IN ({placeholders}) '
                                 'ORDER BY c.asin, c.part', (self.tenant_slug, *asins)).fetchall()
            return [dict(asin=asin, part=part, workflowId=workflow_id, name=name)
                    for asin, part, workflow_id, name in claims]

    def _claim(self, con, workflow_id, asin, scope):
        for part in sorted(scope):
            inserted = con.execute(f'INSERT INTO {self.t(CLAIMS)} (tenant, asin, part, workflow_id) '
                                   'VALUES (%s, %s, %s, %s) ON CONFLICT DO NOTHING RETURNING workflow_id',
                                   (self.tenant_slug, asin, part, workflow_id)).fetchone()
            if inserted is not None:
                continue
            holder = con.execute(f'SELECT c.workflow_id, p.name FROM {self.t(CLAIMS)} AS c '
                                 f'JOIN {self.t(projects.PROJECTS)} AS p '
                                 'ON p.tenant = c.tenant AND p.id = c.workflow_id '
                                 'WHERE c.tenant = %s AND c.asin = %s AND c.part = %s',
                                 (self.tenant_slug, asin, part)).fetchone()
            if holder is None:
                raise RuntimeError('The holding PDP workflow changed. Retry the request.')
            if holder[0] != workflow_id:
                raise PartTaken(asin, part, *holder)

    def create(self, record, asins, scope, stamp, settings):
        workflow_id = record['id']
        with self._write() as con:
            self.projects._insert(con, record, [('asin', asin) for asin in asins], stamp)
            con.execute(f'INSERT INTO {self.t(WORKFLOWS)} (tenant, id, scope, settings) '
                        'VALUES (%s, %s, %s::jsonb, %s::jsonb)',
                        (self.tenant_slug, workflow_id, json.dumps(scope), json.dumps(settings)))
            for asin in sorted(asins):
                self._insert_item(con, workflow_id, asin, scope, stamp)
            return self._read(con, workflow_id)

    def _insert_item(self, con, workflow_id, asin, scope, stamp):
        con.execute(f'INSERT INTO {self.t(ITEMS)} (tenant, workflow_id, asin, stage) '
                    "VALUES (%s, %s, %s, 'setup')", (self.tenant_slug, workflow_id, asin))
        self._claim(con, workflow_id, asin, scope)
        for part in scope:
            con.execute(f'INSERT INTO {self.t(PARTS)} '
                        '(tenant, workflow_id, asin, part, updated_at) VALUES (%s, %s, %s, %s, %s)',
                        (self.tenant_slug, workflow_id, asin, part, stamp))

    def add_scope(self, workflow_id, part):
        with self._write() as con:
            workflow = self._workflow(con, workflow_id, lock=True)
            if part in workflow['scope']:
                return self._read(con, workflow_id)
            items = self._items(con, workflow_id)
            # Shared scope cannot change after any item has published.
            for item in items:
                _writable(item)
            stamp = projects._now()
            for item in items:
                self._claim(con, workflow_id, item['asin'], [part])
                con.execute(f'INSERT INTO {self.t(PARTS)} '
                            '(tenant, workflow_id, asin, part, updated_at) VALUES (%s, %s, %s, %s, %s) '
                            'ON CONFLICT DO NOTHING',
                            (self.tenant_slug, workflow_id, item['asin'], part, stamp))
            scope = [key for key in SCOPE_PARTS if key in {*workflow['scope'], part}]
            con.execute(f'UPDATE {self.t(WORKFLOWS)} SET scope = %s::jsonb WHERE tenant = %s AND id = %s',
                        (json.dumps(scope), self.tenant_slug, workflow_id))
            return self._read(con, workflow_id)

    def change_items(self, workflow_id, asins, *, remove):
        with self._write() as con:
            workflow = self._workflow(con, workflow_id, lock=True)
            current = {item['asin'] for item in self._items(con, workflow_id)}
            stamp = projects._now()
            if remove:
                for asin in sorted(set(asins) & current):
                    _writable(next(item for item in self._items(con, workflow_id) if item['asin'] == asin))
                    for table in (PARTS, CLAIMS, ITEMS):
                        con.execute(f'DELETE FROM {self.t(table)} '
                                    'WHERE tenant = %s AND workflow_id = %s AND asin = %s',
                                    (self.tenant_slug, workflow_id, asin))
                    con.execute(f'DELETE FROM {self.t(projects.TARGETS)} '
                                "WHERE tenant = %s AND project_id = %s AND grain = 'asin' AND key = %s",
                                (self.tenant_slug, workflow_id, asin))
            else:
                for asin in sorted(set(asins) - current):
                    self._insert_item(con, workflow_id, asin, workflow['scope'], stamp)
                self.projects._insert_targets(con, workflow_id, [('asin', asin) for asin in asins], stamp)
            self._sync_progress(con, workflow_id)
            return self._read(con, workflow_id)

    def _sync_progress(self, con, workflow_id):
        items = self._items(con, workflow_id)
        progress = round(sum(STAGE_PROGRESS[item['stage']] for item in items) / len(items)) if items else 0
        completed = bool(items) and all(item['stage'] == 'published' for item in items)
        status = projects.COMPLETED if completed else 'In progress'
        previous = self.projects._one(con, workflow_id)
        completed_on = (previous['completed_on'] or projects._today().isoformat()) if completed else None
        con.execute(f'UPDATE {self.t(projects.PROJECTS)} SET progress = %s, status = %s, completed_on = %s, '
                    'updated_at = %s, rollup = NULL, impact = %s::jsonb WHERE tenant = %s AND id = %s',
                    (progress, status, completed_on, projects._now(),
                     json.dumps(projects._with_result_configs(None, previous['result_configs'])),
                     self.tenant_slug, workflow_id))

    def set_stage(self, workflow_id, asin, stage, *, by=None, trusted=False):
        with self._write() as con:
            workflow, current = self._context(con, workflow_id, asin, lock=True)
            if trusted and current['stage'] == 'published':
                return self._read(con, workflow_id)
            _writable(current)
            if not trusted:
                states, _facts_read = self._states(con, workflow, current, by)
                _stage_rules(workflow, current, stage, [s for s in states if s['part'] in workflow['scope']])
            if stage in ('setup', 'draft'):
                self._clear_approvals(con, workflow_id, asin)
            published_on = (current['published_on'] or projects._today().isoformat()) if stage == 'published' else None
            con.execute(f'UPDATE {self.t(ITEMS)} SET stage = %s, published_on = %s '
                        'WHERE tenant = %s AND workflow_id = %s AND asin = %s',
                        (stage, published_on, self.tenant_slug, workflow_id, asin))
            if stage == 'published':
                con.execute(f'DELETE FROM {self.t(CLAIMS)} WHERE tenant = %s AND workflow_id = %s AND asin = %s',
                            (self.tenant_slug, workflow_id, asin))
            self._sync_progress(con, workflow_id)
            return self._read(con, workflow_id)

    def delete(self, workflow_id):
        with self._write() as con:
            self._workflow(con, workflow_id, lock=True)
            items = self._items(con, workflow_id)
            for item in items:
                _writable(item)
            asins = [item['asin'] for item in items]
            _purge_images(self.rt, workflow_id)
            for table in (PARTS, CLAIMS, ITEMS):
                con.execute(f'DELETE FROM {self.t(table)} WHERE tenant = %s AND workflow_id = %s',
                            (self.tenant_slug, workflow_id))
            con.execute(f'DELETE FROM {self.t(WORKFLOWS)} WHERE tenant = %s AND id = %s',
                        (self.tenant_slug, workflow_id))
            self.projects._delete(con, workflow_id)
        return {'deleted': workflow_id, 'asins': asins}