# DocumentRequestForm

Drawer form for creating and editing document request actions within an Employee Lifecycle process step. Action type is `DOCUMENT`.

## File structure

```
DocumentRequestForm/
├── index.tsx          # Re-exports: default from './provider'
├── provider.tsx       # Data loading: useGetFormDefaultValues + folder name resolution → renders DocumentRequestForm
├── form.tsx           # Form shell: useForm, Drawer chrome (Header/Body/Actions), submit/cancel logic
├── content.tsx        # Pure field sections (no Drawer.* components); consumes form context
├── types.ts           # TypeScript types for form values, props, internal types (JSDoc documented)
├── schemas.ts         # Zod schema: createDocumentRequestFormSchema (extends shared base)
├── fields.ts          # Field configuration generator (getDocumentRequestFormFields)
├── validations.ts     # applyDocumentRequestFormRefinements
├── utils.ts           # getDefaultValues + convertFormValuesToDocumentRequestAction
└── components/
    ├── DocumentRequestSection/  # Section grouping: info alert + folder autocomplete + document list
    ├── FolderAutocomplete/      # Autocomplete for selecting/creating a folder in "Documentos personales"
    └── RequiredDocumentsList/   # Dynamic list of required document names (useFieldArray)
```

## Usage

Default export is the Provider (handles data loading). Use it when opening from a drawer layer.

```tsx
import DocumentRequestFormProvider from './DocumentRequestForm';

// Create
<DocumentRequestFormProvider processId={processId} stepId={stepId} />

// Edit
<DocumentRequestFormProvider processId={processId} stepId={stepId} actionId={actionId} />
```

## Form sections (content.tsx)

| Section | Component | Key fields |
|---|---|---|
| Cover image | `CoverImageSection` (shared) | `hasCoverImage`, `coverImage` |
| Icon + name | `IconNameSection` (shared) | `icon`, `name` |
| Assignee + description | `AssigneeDescriptionSection` (shared) | `assignee`, `description` |
| Attachments | `AttachmentsSection` (shared) | `hasAttachments`, `attachments` |
| Document request | `DocumentRequestSection` (local) | `folder`, `requiredDocuments` |
| Due date | `DueDateSection` (shared) | `hasDueDate`, `expiresAfter` |
| Reminders | `ReminderSection` (shared) | `hasReminder`, `remindOnDueDay`, `repeatReminder`, `reminderFrequency`, `reminderFrequencyType`, `reminderChannels` |
| Dependency | `DependencySection` (shared) | `hasDependency`, `dependsOn` |

### DocumentRequestSection

Groups the document-request-specific fields inside a styled container (info alert explaining where documents will be stored, folder autocomplete, required documents list). Follows the `{ disabled, fields }` section pattern used by all shared sections.

### RequiredDocumentsList

Dynamic field list powered by `useFieldArray`. Always has at least one item (default: `[{ name: '' }]`). The delete button is disabled when only one item remains.

## Validation (validations.ts + schemas.ts)

`createDocumentRequestFormSchema` extends the shared base schema and applies `applyDocumentRequestFormRefinements`, which adds:

- All base task refinements (assignee required, etc.) via `applyBaseTaskFormRefinements`
- **Folder**: required (`folder.value` must be present)
- **Required documents**: list must not be empty; each item's `name` must be non-blank

## Provider data flow

The provider has two async steps before rendering the form:

1. `useGetFormDefaultValues` — fetches the existing action (edit mode) or returns empty defaults (create mode)
2. `useQuery(getDocumentsFolder)` — if `defaultValues.folder.value` is set, resolves the folder name from the backend (the backend only persists `folderId`, not `folderName`)

```
DocumentRequestFormProvider (provider.tsx)
  ├── useGetFormDefaultValues({ processId, stepId, actionId, getDefaultValuesFn: getDefaultValues })
  │     └── getDefaultValues(defaultValues, t, action) → { folder: { value, label: '' }, requiredDocuments, ...base }
  └── useQuery(getDocumentsFolder(folderId))   ← resolves folder label for edit mode
        │
        ▼
  DocumentRequestForm (form.tsx)
  ├── useForm({ defaultValues: resolvedDefaultValues, resolver: zodResolver(createDocumentRequestFormSchema) })
  ├── DocumentRequestFormContent (content.tsx)  ← field sections
  └── onSubmit → convertFormValuesToDocumentRequestAction → updateOrCreateCustomTaskAction
```

## Transform (utils.ts)

`convertFormValuesToDocumentRequestAction` extends the shared base transform:

```ts
const base = convertFormValuesToStepAction(values, 'DOCUMENT');
// Merges folderId, folderName, requiredDocuments into base.task
```

The backend persists `folderId` and `requiredDocuments`; `folderName` is sent on write but re-resolved from the backend on load (see Provider section above).

## Related

- `../shared/` — shared field sections, hooks, transforms, base schema
- `../CustomTaskForm/` — same structure for basic tasks
- `../../../../../../types.ts` — `DocumentRequestTaskType`, `ActionTypes`

---

> Update this file when the module structure, form sections, or validation rules change.
