# Task Expiration System and @Mention Functionality

## Overview

The task system has been enhanced with automatic expiration for overdue tasks and @mention functionality in the description field. The category field has been hidden from the task forms to simplify the interface.

## New Features

### 1. Automatic Task Expiration

#### How It Works
- Tasks that are overdue (past their due date) are automatically marked as "expired"
- The system runs every hour via a scheduled command
- Expired tasks are excluded from overdue calculations and displays
- A system note is added to expired tasks for audit purposes

#### Status Changes
- **New Status**: `expired` - Tasks that have passed their due date
- **Color**: Orange background with orange text
- **Behavior**: Expired tasks are no longer considered "overdue"

#### Automatic Processing
- **Schedule**: Every hour via Laravel's task scheduler
- **Command**: `php artisan tasks:expire-overdue`
- **Logging**: All expired tasks are logged to `storage/logs/tasks-expire.log`

#### Manual Execution
```bash
# Dry run to see what would be expired
php artisan tasks:expire-overdue --dry-run

# Actually expire overdue tasks
php artisan tasks:expire-overdue
```

### 2. @Mention Functionality in Description Field

#### Features
- Users can mention other users in task descriptions using `@username`
- Real-time suggestions appear as you type
- Mentioned users receive notifications when the task is saved
- Works alongside existing @mention functionality in notes field

#### How to Use
1. Type `@` followed by the first few letters of a user's name
2. Select from the dropdown of suggested users
3. The mention will be processed when the task is saved
4. Mentioned users receive notifications

#### Technical Implementation
- Uses the existing `MentionService` for consistency
- Processes mentions in both `description` and `notes` fields
- Logs all mention processing for audit purposes

### 3. Hidden Category Field

#### Changes
- Category field has been removed from all task creation/editing forms
- Simplifies the task creation process
- Reduces form complexity and potential confusion

#### Impact
- Tasks can still have categories assigned programmatically
- Existing tasks with categories remain unchanged
- Category filtering and display still works in task lists

## Database Changes

### Migration
- Added `expired` status to the task status enum
- Migration: `2025_01_27_000001_add_expired_status_to_tasks.php`

### Status Values
The task status enum now includes:
- `pending` - Default status for new tasks
- `in_progress` - Task is being worked on
- `completed` - Task is finished
- `cancelled` - Task was cancelled
- `on_hold` - Task is temporarily paused
- `expired` - Task is past due date (NEW)

## Model Updates

### Task Model
- Added `markAsExpired()` method
- Updated `isOverdue()` method to exclude expired tasks
- Added `scopeExpired()` query scope
- Updated `scopeOverdue()` to exclude expired tasks
- Added @mention processing for description field
- Updated status color attribute to include expired status

### Methods
```php
// Mark a task as expired
$task->markAsExpired();

// Check if task is overdue (excludes expired)
$task->isOverdue();

// Query expired tasks
Task::expired()->get();

// Query overdue tasks (excludes expired)
Task::overdue()->get();
```

## Livewire Component Updates

### TaskManager Component
- Added `expired` status to validation rules
- Added @mention functionality for description field
- Updated status options to include expired
- Hidden category field from form

### CreateTask Component
- Added `expired` status to validation rules
- Added @mention functionality for description field
- Updated status options to include expired
- Hidden category field from form

### CrmDashboard Component
- Added `expired` status to validation rules
- Updated status options to include expired

## View Updates

### Task Forms
- Removed category field from all task creation/editing forms
- Added @mention dropdown functionality to description field
- Updated placeholder text to indicate @mention capability

### Task Lists
- Updated overdue date display logic to exclude expired tasks
- Added proper styling for expired status
- Updated overdue indicators to not show for expired tasks

## Console Commands

### ExpireOverdueTasks Command
- **Signature**: `tasks:expire-overdue`
- **Options**: `--dry-run` for preview mode
- **Description**: Automatically expire overdue tasks
- **Schedule**: Every hour via Laravel scheduler

### Usage Examples
```bash
# Preview what would be expired
php artisan tasks:expire-overdue --dry-run

# Actually expire overdue tasks
php artisan tasks:expire-overdue

# Check the schedule
php artisan schedule:list
```

## Configuration

### Scheduling
The task expiration is automatically scheduled in `app/Console/Kernel.php`:
```php
$schedule->command('tasks:expire-overdue')
         ->hourly()
         ->withoutOverlapping()
         ->appendOutputTo(storage_path('logs/tasks-expire.log'));
```

### Logging
- All expired tasks are logged with detailed information
- Log file: `storage/logs/tasks-expire.log`
- Includes task ID, title, matter, assigned user, and processing timestamp

## Benefits

### For Users
1. **Clearer Status**: Expired tasks have their own distinct status
2. **Reduced Confusion**: Overdue indicators don't show for expired tasks
3. **Simplified Forms**: Category field removal reduces form complexity
4. **Better Communication**: @mention functionality in descriptions

### For Administrators
1. **Automatic Management**: Tasks are automatically expired
2. **Audit Trail**: All expiration actions are logged
3. **Consistent Processing**: Scheduled execution ensures regular updates
4. **Flexible Control**: Manual execution available when needed

## Testing

### Manual Testing
1. Create a task with a past due date
2. Run the expiration command: `php artisan tasks:expire-overdue`
3. Verify the task status changes to "expired"
4. Check that the task no longer appears as overdue

### @Mention Testing
1. Create a task and type `@` in the description
2. Verify user suggestions appear
3. Select a user and save the task
4. Verify the mentioned user receives a notification

### Form Testing
1. Verify category field is hidden in all task forms
2. Verify @mention functionality works in description field
3. Verify expired status appears in status dropdowns

## Migration Notes

### From Previous Version
- Existing overdue tasks will be automatically expired on first run
- Category field removal is cosmetic only - data remains intact
- @mention functionality extends existing system

### Rollback Considerations
- Expired tasks can be manually changed back to other statuses
- Category field can be restored by reverting view changes
- @mention functionality can be disabled by reverting component changes
