# Folder Organization

## Objective

Let people group files into nested folders instead of one endless list.

A folder tree over the app's existing files, with move, rename, breadcrumbs, and per-folder listing.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Add a parent reference to the app's existing file records rather than creating a second storage model. Files already uploaded belong at the root, not in a migration limbo.
2. Build the listing on top of the existing file list and Attachment Gallery views. Extend those with a breadcrumb and a folder row type rather than shipping a second file browser beside them.
3. Support moving files and folders by both drag and an explicit move dialog, because drag alone is unusable on touch and with a keyboard.
4. Enforce folder permissions on the server for every read, move, and delete. A user who cannot see a folder must not be able to move a file into it or guess its contents from an error message.
5. Do not build a general-purpose filesystem with symlinks, shortcuts, or a file living in two folders at once. One parent per item covers the real need and keeps counts, deletion, and permissions comprehensible.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- Moving a folder into one of its own descendants would detach the whole subtree from the root and make it unreachable. Walk the ancestor chain before accepting a move and refuse with a clear message.
- Deleting a folder that still has contents needs a decided, documented answer: refuse while non-empty, cascade to the trash, or promote the children to the parent. Pick one, say so in the confirmation, and never leave orphaned files pointing at a folder that no longer exists.
- Unlimited nesting produces breadcrumbs that wrap over three lines and paths too long for the archive and export paths. Cap the depth, enforce the cap on move as well as on create, and say what the limit is when it is hit.
- Item counts and folder sizes computed at write time drift after a bulk move or a partially failed one. Either derive them on read or recalculate them in the same transaction as the move.
- Two people renaming the same folder at once must not produce a lost update or two siblings with the same name. Resolve on the server, and tell the loser what the folder is now called instead of silently reverting their input.
- Names that differ only by case, trailing spaces, or invisible characters read as duplicates to a human. Normalise before the uniqueness check within a parent.
- A file whose folder was deleted or moved while the user had the listing open must not vanish without explanation. Refresh the view and say what changed.
- Search must span folders rather than only the folder currently open, and each result must show the path it lives at.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Files and folders can be created, renamed, moved, and deleted, and the tree survives a reload.
- [ ] A folder cannot be moved inside itself or any of its descendants.
- [ ] Deleting a non-empty folder follows one documented behaviour, stated in the confirmation.
- [ ] Nesting depth is capped and the cap is enforced on move as well as create.
- [ ] Item counts and sizes stay correct after a bulk move.
- [ ] Folder permissions are enforced server-side on read, move, and delete.
- [ ] Search returns matches from every folder the user can see, each showing its path.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
