File size: 3,135 Bytes
1cd137c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import hashlib
import json
from typing import Any

import pandas as pd


def parse_variant_attributes(value: Any) -> dict[str, Any]:
    if isinstance(value, dict):
        return value

    if isinstance(value, str):
        try:
            parsed = json.loads(value)
            return parsed if isinstance(parsed, dict) else {}
        except (json.JSONDecodeError, TypeError):
            return {}

    return {}


def build_variant_key(row: dict[str, Any]) -> str:
    attributes = parse_variant_attributes(row.get("variant_attributes"))

    amount = attributes.get("amount")
    unit = attributes.get("unit")
    qty = attributes.get("qty")
    count = attributes.get("count")
    multiplier = attributes.get("multiplier")
    raw = attributes.get("raw")

    parts = []

    if count is not None:
        parts.append(f"count{count}")
    elif amount is not None:
        parts.append(f"amt{amount}")
        if unit:
            parts.append(f"unit{unit}")
        if qty is not None and qty != 1:
            parts.append(f"qty{qty}")
        if multiplier is not None and multiplier != 1:
            parts.append(f"mult{multiplier}")
    elif raw:
        parts.append(f"raw{str(raw).strip().lower()}")
    else:
        parts.append("nosize")

    return "|".join(parts)


def generate_variant_id(group_id: str, variant_key: str) -> str:
    value = f"{group_id}||{variant_key}"
    return hashlib.sha256(value.encode("utf-8")).hexdigest()[:16]


def assign_variants(df: pd.DataFrame) -> pd.DataFrame:
    result = df.copy()

    result["variant_key"] = result.apply(
        lambda row: build_variant_key(row.to_dict()),
        axis=1,
    )

    result["variant_id"] = result.apply(
        lambda row: generate_variant_id(
            str(row["group_id"]),
            row["variant_key"],
        ),
        axis=1,
    )

    return result


def build_variant_table(df: pd.DataFrame) -> pd.DataFrame:
    rows = []

    for variant_id, group in df.groupby("variant_id", sort=False):
        row = group.iloc[0]

        rows.append(
            {
                "variant_id": variant_id,
                "group_id": row["group_id"],
                "variant_key": row["variant_key"],
                "core_title": row.get("core_title"),
                "size": row.get("size"),
                "product_count": len(group),
            }
        )

    return pd.DataFrame(rows)


def build_variant_mapping(df: pd.DataFrame) -> pd.DataFrame:
    columns = [
        "external_id",
        "group_id",
        "variant_id",
        "variant_key",
    ]

    return df[columns].copy()


def build_variant_summary(df: pd.DataFrame) -> pd.DataFrame:
    rows = []

    for group_id, group in df.groupby("group_id", sort=False):
        rows.append(
            {
                "group_id": group_id,
                "variant_count": group["variant_id"].nunique(),
                "product_count": len(group),
                "variant_ids": ", ".join(
                    sorted(group["variant_id"].unique())
                ),
            }
        )

    return pd.DataFrame(rows)