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 
 
 
 
 
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
#!/usr/bin/env python3
"""
Phase 4 — Variant Assignment / Variant Modeling
Compliments Reference DB Pipeline

Input: Phase 3 outputs + Phase 2 output
Output: Variant tables and mappings

This phase transforms the Phase 3 product-group representation into a
structured product variant model, where products belonging to the same
reference product group can have different purchasable variants such as
size, package quantity, formulation, flavour, fat level, etc.

CRITICAL: This phase does NOT use nutrition data.
CRITICAL: This phase does NOT use any external taxonomy.
CRITICAL: This phase is 100% deterministic (no LLM).
"""

import json
import hashlib
import sys
from datetime import datetime, timezone
from pathlib import Path

import pandas as pd
import numpy as np

# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
BASE_DIR = Path(__file__).resolve().parent.parent
PHASE3_DIR = BASE_DIR.parent / "phase3"
PHASE2_DIR = BASE_DIR.parent / "phase2"
OUTPUT_DIR = BASE_DIR / "outputs"
VALIDATION_DIR = BASE_DIR / "validation"
STATISTICS_DIR = BASE_DIR / "statistics"
AUDIT_DIR = BASE_DIR / "audit"

VERSION = "1.0.0"
TIMESTAMP = datetime.now(timezone.utc).isoformat()


def log(msg: str) -> None:
    print(f"[Phase4] {msg}")


# ---------------------------------------------------------------------------
# 1. VARIANT DEFINITION
# ---------------------------------------------------------------------------
# A variant is a purchasable representation of the same reference product
# that differs in attributes such as:
#   - Size (500 g, 1 kg, 2 kg)
#   - Package quantity (1 × 500 g, 2 × 500 g)
#   - Pack configuration (single, multipack, case)
#
# Variants within the same group share the same identity attributes:
#   - is_organic, is_gluten_free, is_naturally_simple, etc.
#   - product_line
#   - flavour
#   - formulation
#   - fat_level
#
# Variants differ only in size/package attributes.


# ---------------------------------------------------------------------------
# 2. VARIANT KEY GENERATION
# ---------------------------------------------------------------------------

def build_variant_key(row: dict) -> str:
    """
    Build a deterministic variant key from normalized variant attributes.
    
    The variant key captures size/package information that distinguishes
    variants within the same reference product group.
    
    Structure: amount|unit|qty|count
    """
    va_str = row.get("variant_attributes", "{}")
    if isinstance(va_str, str):
        try:
            va = json.loads(va_str)
        except (json.JSONDecodeError, TypeError):
            va = {}
    elif isinstance(va_str, dict):
        va = va_str
    else:
        va = {}
    
    # Extract variant-distinguishing attributes
    amount = va.get("amount")
    unit = va.get("unit", "")
    qty = va.get("qty", 1)
    count = va.get("count")
    multiplier = va.get("multiplier", 1)
    raw = va.get("raw", "")
    
    # Build key parts
    parts = []
    
    if count is not None:
        # Count-based variant (e.g., "20 per pack")
        parts.append(f"count{int(count)}")
    elif amount is not None:
        # Size-based variant
        parts.append(f"amt{amount}")
        if unit:
            parts.append(f"unit{unit}")
        if qty and qty > 1:
            parts.append(f"qty{int(qty)}")
        if multiplier and multiplier > 1:
            parts.append(f"mult{int(multiplier)}")
    elif raw:
        # Fallback to raw string
        parts.append(f"raw{raw.strip().lower()}")
    else:
        # No size information
        parts.append("nosize")
    
    return "|".join(parts)


def generate_variant_id(group_id: str, variant_key: str) -> str:
    """
    Generate deterministic variant_id from group_id + variant_key.
    
    Uses UUID5 (namespace-based) for reproducibility.
    """
    import uuid
    namespace = uuid.NAMESPACE_DNS
    name = f"{group_id}||{variant_key}"
    return str(uuid.uuid5(namespace, name))


# ---------------------------------------------------------------------------
# 3. VARIANT ASSIGNMENT
# ---------------------------------------------------------------------------

def assign_variants(df: pd.DataFrame) -> pd.DataFrame:
    """
    Assign variant_ids to products within each group.
    
    Products in the same group with the same variant_key get the same variant_id.
    Products in the same group with different variant_keys get different variant_ids.
    """
    log("Building variant keys ...")
    df["variant_key"] = df.apply(lambda row: build_variant_key(row.to_dict()), axis=1)
    
    log("Generating variant IDs ...")
    df["variant_id"] = df.apply(
        lambda row: generate_variant_id(row["group_id"], row["variant_key"]),
        axis=1
    )
    
    n_variants = df["variant_id"].nunique()
    log(f"Total unique variants: {n_variants}")
    
    return df


# ---------------------------------------------------------------------------
# 4. OUTPUT GENERATION
# ---------------------------------------------------------------------------

def build_variant_table(df: pd.DataFrame) -> pd.DataFrame:
    """
    Build the main variant table: one row per unique variant.
    """
    log("Building variant table ...")
    
    # Group by variant_id and aggregate
    variant_rows = []
    for vid, grp in df.groupby("variant_id"):
        row = grp.iloc[0]
        
        # Get all sizes in this variant
        sizes = grp["size"].dropna().unique().tolist()
        
        variant_rows.append({
            "variant_id": vid,
            "group_id": row["group_id"],
            "group_name": row["group_name"],
            "brand": row["brand"],
            "product_line": row["product_line"],
            "product_domain": row["product_domain"],
            "reference_db_taxonomy": row["reference_db_taxonomy"],
            "core_title": row["core_title"],
            "variant_key": row["variant_key"],
            "size": sizes[0] if len(sizes) == 1 else "; ".join(str(s) for s in sizes),
            "size_amount": row.get("size_amount"),
            "size_unit": row.get("size_unit"),
            "size_qty": row.get("size_qty"),
            "flavour": row.get("flavour"),
            "formulation": row.get("formulation"),
            "fat_level": row.get("fat_level"),
            "fat_percentage": row.get("fat_percentage"),
            "product_count": len(grp),
            "unique_upcs": grp["upc"].nunique(),
            "source": row["source"],
            "source_url": row["source_url"],
        })
    
    variant_df = pd.DataFrame(variant_rows)
    log(f"  Variant table: {len(variant_df)} variants")
    return variant_df


def build_variant_mapping(df: pd.DataFrame) -> pd.DataFrame:
    """
    Build the product → variant mapping table.
    """
    log("Building variant mapping ...")
    
    mapping = df[[
        "external_id", "upc", "group_id", "variant_id",
        "core_title", "original_title", "brand", "size",
        "variant_key", "source", "source_url"
    ]].copy()
    
    log(f"  Variant mapping: {len(mapping)} products")
    return mapping


def build_variant_summary(df: pd.DataFrame) -> pd.DataFrame:
    """
    Build the group-level variant summary.
    """
    log("Building variant summary ...")
    
    summary_rows = []
    for gid, grp in df.groupby("group_id"):
        variant_ids = grp["variant_id"].unique().tolist()
        row = grp.iloc[0]
        summary_rows.append({
            "group_id": gid,
            "group_name": row["group_name"],
            "brand": row["brand"],
            "product_domain": row["product_domain"],
            "reference_db_taxonomy": row["reference_db_taxonomy"],
            "variant_count": len(variant_ids),
            "variant_ids": ", ".join(variant_ids),
            "product_count": len(grp),
        })
    
    summary_df = pd.DataFrame(summary_rows)
    log(f"  Variant summary: {len(summary_df)} groups")
    return summary_df


# ---------------------------------------------------------------------------
# 5. VALIDATION
# ---------------------------------------------------------------------------

def validate_phase4(df: pd.DataFrame, variant_table: pd.DataFrame,
                    variant_mapping: pd.DataFrame, variant_summary: pd.DataFrame) -> dict:
    """Comprehensive Phase 4 validation."""
    checks = {}
    
    # Rule 1: Every product has exactly one variant
    checks["rule_1_product_has_one_variant"] = {
        "description": "Every product has exactly one variant_id",
        "products": len(df),
        "unique_variant_ids_per_product": int(df.groupby("external_id")["variant_id"].nunique().max()),
        "pass": int(df.groupby("external_id")["variant_id"].nunique().max()) == 1,
    }
    
    # Rule 2: Every variant belongs to exactly one group
    checks["rule_2_variant_belongs_to_one_group"] = {
        "description": "Every variant belongs to exactly one group_id",
        "variants": len(variant_table),
        "unique_groups_per_variant": int(variant_table.groupby("variant_id")["group_id"].nunique().max()),
        "pass": int(variant_table.groupby("variant_id")["group_id"].nunique().max()) == 1,
    }
    
    # Rule 3: No product maps to multiple variants
    checks["rule_3_no_product_multiple_variants"] = {
        "description": "No product maps to multiple variant_ids",
        "products_with_multiple_variants": int((df.groupby("external_id")["variant_id"].nunique() > 1).sum()),
        "pass": int((df.groupby("external_id")["variant_id"].nunique() > 1).sum()) == 0,
    }
    
    # Rule 4: Variant count matches
    checks["rule_4_variant_count"] = {
        "description": "Variant count in mapping equals variant table",
        "mapping_variants": int(variant_mapping["variant_id"].nunique()),
        "table_variants": len(variant_table),
        "pass": int(variant_mapping["variant_id"].nunique()) == len(variant_table),
    }
    
    # Rule 5: All products have group_id
    checks["rule_5_all_products_have_group"] = {
        "description": "All products have group_id",
        "null_group_ids": int(df["group_id"].isna().sum()),
        "pass": int(df["group_id"].isna().sum()) == 0,
    }
    
    # Rule 6: All products have variant_id
    checks["rule_6_all_products_have_variant"] = {
        "description": "All products have variant_id",
        "null_variant_ids": int(df["variant_id"].isna().sum()),
        "pass": int(df["variant_id"].isna().sum()) == 0,
    }
    
    # Overall
    all_pass = all(c.get("pass", True) for c in checks.values())
    checks["overall"] = {"result": "PASS" if all_pass else "FAIL"}
    
    return checks


# ---------------------------------------------------------------------------
# 6. STATISTICS
# ---------------------------------------------------------------------------

def build_statistics(df: pd.DataFrame, variant_table: pd.DataFrame,
                     variant_mapping: pd.DataFrame, variant_summary: pd.DataFrame) -> dict:
    """Build Phase 4 statistics."""
    
    # Variant count distribution
    variant_counts = variant_summary["variant_count"].value_counts().sort_index()
    
    return {
        "version": VERSION,
        "timestamp": TIMESTAMP,
        "input": {
            "source": "phase3_product_group_mapping.csv + phase2_output.parquet",
            "products": len(df),
            "groups": df["group_id"].nunique(),
        },
        "output": {
            "variants": len(variant_table),
            "products_mapped": len(variant_mapping),
            "groups": len(variant_summary),
        },
        "variant_distribution": {
            "groups_with_1_variant": int((variant_summary["variant_count"] == 1).sum()),
            "groups_with_2_variants": int((variant_summary["variant_count"] == 2).sum()),
            "groups_with_3_variants": int((variant_summary["variant_count"] == 3).sum()),
            "groups_with_4plus_variants": int((variant_summary["variant_count"] >= 4).sum()),
            "max_variants_per_group": int(variant_summary["variant_count"].max()),
            "avg_variants_per_group": float(variant_summary["variant_count"].mean()),
        },
        "variant_count_distribution": {str(k): int(v) for k, v in variant_counts.items()},
        "missing_size": {
            "products_with_missing_size": int(df["size"].isna().sum()),
            "products_with_no_size_variant": int((df["variant_key"] == "nosize").sum()),
        },
    }


# ---------------------------------------------------------------------------
# 7. PEANUT BUTTER EXAMPLE
# ---------------------------------------------------------------------------

def generate_peanut_butter_example(df: pd.DataFrame) -> pd.DataFrame:
    """Generate Peanut Butter variant example for audit."""
    log("Generating Peanut Butter example ...")
    
    # Find peanut butter products
    pb = df[df["original_title"].str.contains("peanut butter", case=False, na=False)].copy()
    
    # Select relevant columns
    example = pb[[
        "external_id", "upc", "group_id", "variant_id",
        "original_title", "core_title", "brand", "size",
        "variant_key", "variant_attributes", "product_line",
        "identity_hash", "fat_level", "product_domain", "reference_db_taxonomy"
    ]].copy()
    
    # Sort by group_id and size
    example = example.sort_values(["group_id", "size"]).reset_index(drop=True)
    
    log(f"  Peanut Butter example: {len(example)} products in {example['group_id'].nunique()} groups")
    return example


# ---------------------------------------------------------------------------
# MAIN
# ---------------------------------------------------------------------------

def main():
    log("Starting Phase 4 (v1.0.0 — Variant Assignment)")
    
    # Ensure output dirs exist
    OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
    VALIDATION_DIR.mkdir(parents=True, exist_ok=True)
    STATISTICS_DIR.mkdir(parents=True, exist_ok=True)
    AUDIT_DIR.mkdir(parents=True, exist_ok=True)
    
    # ------------------------------------------------------------------
    # 1. LOAD PHASE 3 OUTPUTS
    # ------------------------------------------------------------------
    log("Loading Phase 3 outputs ...")
    mapping_path = PHASE3_DIR / "outputs" / "product_group_mapping.csv"
    mapping = pd.read_csv(mapping_path, dtype={"upc": "string"})
    log(f"  product_group_mapping: {len(mapping)} rows")

    # UPC integrity validation
    if "upc" in mapping.columns:
        upc_col = mapping["upc"]
        dot_zero_count = upc_col.dropna().astype(str).str.endswith(".0").sum()
        if dot_zero_count > 0:
            raise ValueError(f"UPC integrity check failed: {dot_zero_count} UPCs end with '.0'")
        log(f"  UPC integrity: PASS (no .0 suffixes, dtype={upc_col.dtype})")
    
    # ------------------------------------------------------------------
    # 2. LOAD PHASE 2 OUTPUT (for variant-relevant columns)
    # ------------------------------------------------------------------
    log("Loading Phase 2 output ...")
    p2_path = PHASE2_DIR / "outputs" / "phase2_output.parquet"
    p2 = pd.read_parquet(p2_path)
    log(f"  phase2_output: {len(p2)} rows, {len(p2.columns)} columns")
    
    # ------------------------------------------------------------------
    # 3. MERGE PHASE 3 MAPPING WITH PHASE 2 VARIANT DATA
    # ------------------------------------------------------------------
    log("Merging Phase 3 mapping with Phase 2 variant data ...")
    variant_cols = ["external_id", "variant_attributes", "flavour", "formulation",
                    "fat_level", "fat_percentage", "size_amount", "size_unit", "size_qty"]
    # Drop duplicate columns from Phase 3 mapping before merge to avoid _x/_y suffixes
    overlap_cols = [c for c in variant_cols if c != "external_id" and c in mapping.columns]
    mapping_clean = mapping.drop(columns=overlap_cols)
    df = mapping_clean.merge(p2[variant_cols], on="external_id", how="left")
    log(f"  Merged: {len(df)} rows, {len(df.columns)} columns")
    
    # ------------------------------------------------------------------
    # 4. ASSIGN VARIANTS
    # ------------------------------------------------------------------
    df = assign_variants(df)
    
    # ------------------------------------------------------------------
    # 5. GENERATE OUTPUTS
    # ------------------------------------------------------------------
    variant_table = build_variant_table(df)
    variant_mapping = build_variant_mapping(df)
    variant_summary = build_variant_summary(df)
    
    # ------------------------------------------------------------------
    # 6. VALIDATION
    # ------------------------------------------------------------------
    log("Running validation ...")
    validation = validate_phase4(df, variant_table, variant_mapping, variant_summary)
    
    # ------------------------------------------------------------------
    # 7. STATISTICS
    # ------------------------------------------------------------------
    log("Building statistics ...")
    statistics = build_statistics(df, variant_table, variant_mapping, variant_summary)
    
    # ------------------------------------------------------------------
    # 8. PEANUT BUTTER EXAMPLE
    # ------------------------------------------------------------------
    pb_example = generate_peanut_butter_example(df)
    
    # ------------------------------------------------------------------
    # 9. SAVE OUTPUTS
    # ------------------------------------------------------------------
    log("Saving outputs ...")
    
    # Main outputs (Parquet)
    variant_table.to_parquet(OUTPUT_DIR / "reference_product_variants.parquet", index=False)
    log(f"  Saved reference_product_variants.parquet ({len(variant_table)} rows)")
    
    variant_mapping.to_parquet(OUTPUT_DIR / "product_variant_mapping.parquet", index=False)
    log(f"  Saved product_variant_mapping.parquet ({len(variant_mapping)} rows)")
    
    variant_summary.to_parquet(OUTPUT_DIR / "reference_product_variant_summary.parquet", index=False)
    log(f"  Saved reference_product_variant_summary.parquet ({len(variant_summary)} rows)")
    
    # CSV versions for inspection
    variant_table.to_csv(OUTPUT_DIR / "reference_product_variants.csv", index=False)
    variant_mapping.to_csv(OUTPUT_DIR / "product_variant_mapping.csv", index=False)
    variant_summary.to_csv(OUTPUT_DIR / "reference_product_variant_summary.csv", index=False)
    
    # Validation
    with open(VALIDATION_DIR / "phase4_validation.json", "w") as f:
        json.dump(validation, f, indent=2, default=str)
    log("  Saved phase4_validation.json")
    
    # Statistics
    with open(STATISTICS_DIR / "phase4_statistics.json", "w") as f:
        json.dump(statistics, f, indent=2, default=str)
    log("  Saved phase4_statistics.json")
    
    # Peanut Butter example
    pb_example.to_parquet(AUDIT_DIR / "phase4_peanut_butter_example.parquet", index=False)
    log(f"  Saved phase4_peanut_butter_example.parquet ({len(pb_example)} rows)")
    
    # ------------------------------------------------------------------
    # SUMMARY
    # ------------------------------------------------------------------
    log("")
    log("=== PHASE 4 COMPLETE (v1.0.0) ===")
    log(f"Input:  {len(df)} products, {df['group_id'].nunique()} groups")
    log(f"Output: {len(variant_table)} variants, {len(variant_mapping)} products mapped")
    log(f"Validation: {validation['overall']['result']}")
    log("========================")
    
    return variant_table, variant_mapping, variant_summary, validation, statistics


if __name__ == "__main__":
    main()