# Create Category

<!-- 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

As a product manager, I want to create a root or child category with a unique code and name, so that I can organize products in a hierarchical structure.

## 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[Category List Screen]
    B --> C[Create Category Form]
    C --> D{Submit}
    D --> E{Duplicate code?}
    E -- No --> F{Parent ID provided?}
    F -- No --> G[Root category created]
    F -- Yes --> H{Parent found?}
    H -- Yes --> I[Child category created]
    H -- No --> J[Error: Parent category not found]
    E -- Yes --> K[Error: Category code already exists]
    G --> B
    I --> B
```

## Scenario Patterns

- Creating a root category: provide code and name without parentId; category is created at the top level
- Creating a child category: provide code, name, and a valid parentId; category is nested under the parent
- Duplicate code error: submitting a code that already exists returns an error and no category is created
- Parent not found error: submitting a parentId that does not exist returns an error and no category is created

## Test Cases

- Given a unique code and name with no parentId, a root category is created successfully
- Given a unique code, name, and valid parentId, a child category is created under the specified parent
- Given a code that already exists, creation fails with a duplicate code error
- Given a parentId that does not exist, creation fails with a parent not found error

## Screens

- [category-list](../../../screen/category-list.md)
- [category-create-form](../../../screen/category-create-form.md)

## Resolvers

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

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