# Folder Types Redesign Plan

## Overview

This plan outlines the redesign of the email organizer to support two distinct types of folders:
1. **Folders to Tidy**: These folders contain emails that need to be processed and organized
2. **Destination Folders**: These folders are targets for organizing emails from other folders

## Current State

Currently, all folders are treated the same way with:
- Pending and processed counts
- Organization rules
- Toggle for enabling/disabling organization

The system doesn't differentiate between source folders (where emails are processed from) and destination folders (where emails are moved to).

## Proposed Changes

### 1. Data Model Updates

#### Folder Model Changes

Add a new field to the `Folder` model:
```python
folder_type = db.Column(db.String(20), default='destination', nullable=False)
```

Possible values:
- `'tidy'`: Folders that contain emails to be processed (e.g., Inbox)
- `'destination'`: Folders that are targets for email organization (default for new folders)

#### New Field Requirements:
- Must be a string to allow for potential future folder types
- Default value should be 'destination' for backward compatibility
- Not nullable to ensure all folders have a defined type

### 2. Business Logic Changes

#### Folder Synchronization Logic
- When synchronizing IMAP folders:
  - If folder name is 'inbox' (case-insensitive), set `folder_type = 'tidy'`
  - For all other folders, set `folder_type = 'destination'`
  - Existing folders will keep their current type unless explicitly changed

#### Folder Count Logic
- **Folders to Tidy**:
  - Show `pending_count` (emails waiting to be processed)
  - Show `processed_count` (emails that have been processed)
  - May show `total_count` (total emails in the folder)
  
- **Destination Folders**:
  - Show `emails_count` (number of emails that have been put into this folder)
  - No need to show pending/processed counts

### 3. UI Changes

#### Main Page Layout
Create two distinct sections:
1. **Folders to Tidy Section**
   - Contains folders with `folder_type = 'tidy'`
   - Shows pending and processed counts
   - May include special styling to indicate these are source folders

2. **Destination Folders Section**
   - Contains folders with `folder_type = 'destination'`
   - Shows count of emails moved to this folder
   - May include special styling to indicate these are target folders

#### Folder Card Changes
- **Tidy Folder Card**:
  - Display: Folder name, pending count, processed count
  - Actions: View pending emails, toggle organization
  
- **Destination Folder Card**:
  - Display: Folder name, emails count
  - Actions: View emails in folder (if implemented), basic folder management

### 4. API Changes

#### New Endpoints
- `/api/folders/filter?type={tidy|destination}` - Get folders filtered by type
- `/api/folders/<id>/type` - Update folder type (admin function)

#### Existing Endpoint Updates
- `/api/folders` - Add filtering capability
- `/api/imap/sync` - Implement new folder type logic
- Folder-related templates should adapt based on folder type

### 5. Migration Strategy

#### Database Migration
1. Create a new migration to add the `folder_type` column
2. Set default value to 'destination' for existing folders
3. Special handling for 'inbox' folders to set type to 'tidy'

#### Data Migration
- Scan all existing folders for user with 'inbox' in the name
- Set `folder_type = 'tidy'` for these folders
- Set `folder_type = 'destination'` for all other folders

### 6. Implementation Steps

1. **Phase 1: Data Model**
   - Create migration for `folder_type` column
   - Update `Folder` model
   - Apply migration to database

2. **Phase 2: Backend Logic**
   - Update folder synchronization logic
   - Modify folder query methods
   - Update count calculation logic

3. **Phase 3: Frontend UI**
   - Create two new partial templates for folder sections
   - Update folder card templates based on type
   - Modify main page to show two sections

4. **Phase 4: Testing**
   - Test folder synchronization with new types
   - Verify UI shows correct sections
   - Test folder count calculations

### 7. Future Considerations

#### Potential Enhancements
- Add ability to change folder type through UI
- Implement email movement tracking
- Add analytics for email organization efficiency
- Support for additional folder types in the future

#### Backward Compatibility
- Existing folders will be treated as destination folders
- No breaking changes to existing functionality
- Gradual rollout of new UI sections

## Success Criteria

1. Folders are correctly categorized as either 'tidy' or 'destination'
2. Inbox folders are automatically set as 'tidy' type
3. UI displays two distinct sections for folder types
4. Folder cards show appropriate counts based on type
5. All existing functionality continues to work