# Generate Variants

<!-- User story defining a specific requirement within a business flow.
The name of the story should start with the actor followed by `--` and the story name.
 -->

<!-- This section should contain the following subsections in order: -->
<!-- 1. ## User Story (required) -->
<!-- 2. ## Story Diagram (required) -->
<!-- 3. ## Scenario Patterns (required) -->
<!-- 4. ## Test Cases (required) -->
<!-- 5. ## Screens (required) -->
<!-- 6. ## Resolvers (required) -->

## User Story

<!-- Define the user story in the format:
As a [role], I want [goal], so that [benefit].
 -->

As a product manager, I want to generate variants from an ACTIVE product's attribute combinations, so that Items with auto-generated SKUs are created for every combination of attribute values.

## Story Diagram

<!-- Mermaid flowchart showing user interactions with screens.
Focus on actors, UI screens, and business outcomes.
Avoid technical details (APIs, databases, endpoints).
 -->

<!-- Code block requirements: -->
<!-- Minimum 1 mermaid code blocks required -->

```mermaid
flowchart TD
    A([Product Manager]) --> B[Product Detail Screen]
    B --> C[Generate Variants Action]
    C --> D{Check Product Status}
    D -- Not ACTIVE --> E[Show Error: Product Must Be Active]
    D -- ACTIVE --> F{Check Attribute Axes}
    F -- No attributes assigned --> G[Show Error: No Attribute Axes Defined]
    F -- Attributes present --> H[Compute Cartesian Product of Attribute Values]
    H --> I{Items Already Exist?}
    I -- Yes, idempotent --> J[Skip Existing, Create Missing Items]
    I -- No --> K[Create All Items with Auto-Generated SKUs]
    J --> L[Variant List Screen]
    K --> L
    E --> B
    G --> B
```

## Scenario Patterns

<!-- List requirements: -->
<!-- Minimum 1 unordered lists required -->
<!-- Minimum 1 items per list -->

- Product manager triggers variant generation on an ACTIVE product with assigned attributes; Items are created for every combination of attribute values with auto-generated SKUs.
- Running variant generation a second time is idempotent: existing Items are not duplicated and only missing combinations are created.
- Triggering variant generation on a non-ACTIVE product results in an error.
- Triggering variant generation on an ACTIVE product with no attributes assigned results in an error.

## Test Cases

<!-- List requirements: -->
<!-- Minimum 1 items per list -->

- Given an ACTIVE product with two attributes each having two values, when variants are generated, then four Items are created with distinct auto-generated SKUs.
- Given variants already generated, when generation is triggered again, then no duplicate Items are created.
- Given a DRAFT or ARCHIVED product, when variant generation is triggered, then an error is shown.
- Given an ACTIVE product with no attributes assigned, when variant generation is triggered, then an error is shown.

## Screens

<!-- Link to screen docs that implement this story's UI.
 -->

<!-- List requirements: -->
<!-- Minimum 1 unordered lists required -->
<!-- Minimum 1 items per list -->

- [product-detail](../../../screen/product-detail.md)
- [variant-list](../../../screen/variant-list.md)

## Resolvers

<!-- Link to resolver docs that implement this story's backend operations.
Write "None" if this story does not require any resolvers.
 -->

<!-- List requirements: -->
<!-- Minimum 1 items per list -->

- [generateVariants](../../../resolver/generateVariants.md)
