# UC-05 Create Survey Template (Module 2) - Architectural Mapping

This document maps the visual components from the **UC-05 Create Survey Template** Use Case diagram to the Laravel backend implementation.

## 1. Actors
* **Actors**: Quality and Admin.
* **Role Protection**: Handled by the same `role:Quality,Admin` middleware protecting the `TemplateController`.

## 2. "Set Template Name & Version"
* **API Endpoint**: `POST /templates`.
* **Factory Pattern**: The creation of a fresh template is managed by the `TemplateFactory` (e.g., `createFresh()`). This centralizes default value assignment (e.g., `IsActive = false`, `HasResponses = false`, `Version = 1`).

## 3. "Inherit from Existing Template (clone)" (<<extend>>)
* **API Endpoint**: `POST /templates/{id}/inherit`.
* **Service Object (SRP)**: The complex logic of deep-copying a template, its sections, questions, and options is isolated inside the `TemplateCloneService::clone()`. This keeps the controller thin and allows the cloning logic to be reused or tested independently.
* **Factory Pattern**: `TemplateFactory::createFromParent()` is utilized here to construct the new instance based on the cloned data.

## 4. "Add Sections", "Add Questions", "Add Options" (<<include>>)
* **API Endpoints**: `POST /templates/{id}/sections`, `POST /sections/{id}/questions`, `POST /questions/{id}/options`.
* **Lock Mechanism (Business Rule)**: The diagram notes "Lock: HasResponses=true blocks edit/delete". This critical rule is enforced system-wide using Laravel Authorization Policies (`SurveyTemplatePolicy::update`). Before any nested child (section, question, option) is added or updated, the policy checks `$template->has_responses`. If true, it aborts with HTTP 409 Conflict. This is a perfect example of DRY logic.

## 5. "Activate Template (IsActive=true)" (<<extend>>)
* **API Endpoint**: `PATCH /templates/{id}/toggle`.
* **Shared Logic**: Reuses the exact same repository and service methods described in UC-04 for toggling the boolean state.
