# Conflict Review Timeout System

## Overview

The Conflict Review Timeout System automatically changes the status of pending conflict reviews to "no conflict" (no objection) if no lawyer responds within a configurable number of working hours (excluding weekends).

## Configuration

### Environment Variable

Add the following environment variable to your `.env` file:

```env
# Conflict review timeout in working hours (excluding weekends)
# Default: 48 hours (2 business days)
CONFLICT_REVIEW_TIMEOUT_HOURS=48
```

You can adjust this value based on your firm's requirements:
- `24` = 1 business day
- `48` = 2 business days  
- `72` = 3 business days
- `120` = 5 business days (1 week)

## How It Works

### 1. Automatic Processing
- The system runs hourly during business hours (8:00 AM - 6:00 PM)
- Only runs on weekdays (Monday through Friday)
- Checks all pending conflict reviews for expiration

### 2. Working Hours Calculation
- **Includes**: Monday through Friday, 24 hours per day
- **Excludes**: Saturdays and Sundays (weekends)
- Example: If timeout is set to 48 hours and a review is created on Friday at 3 PM, it will expire on Tuesday at 3 PM (Friday 3 PM → Monday 24h → Tuesday 24h = 48 working hours)

### 3. Automatic Status Change
When a conflict review expires:
- Status changes from `pending` to `no_conflict`
- Adds automatic note: "Automatically marked as no conflict due to timeout - no response within X working hours"
- Sets `reviewed_at` timestamp to current time
- Triggers proposal status completion check

### 4. Proposal Status Updates
After marking expired reviews as "no conflict":
- System checks if all conflict reviews for the proposal are completed
- If all reviews are complete and no conflicts found: proposal status → `no_conflict`
- If any review found conflicts: proposal status → `conflict`

## Console Command

### Manual Execution
You can manually run the timeout process:

```bash
# Process expired reviews
php artisan conflict-reviews:process-expired

# Dry run (preview without making changes)
php artisan conflict-reviews:process-expired --dry-run
```

### Scheduled Execution
The command runs automatically via Laravel's task scheduler:
- **Frequency**: Every hour
- **Time Window**: 8:00 AM - 6:00 PM
- **Days**: Monday through Friday only
- **Overlap Protection**: Prevents multiple instances running simultaneously

## Logging

### Activity Logs
All automatic timeout processing is logged to:
- File: `storage/logs/conflict-reviews-timeout.log`
- Laravel Log: Includes detailed information about processed reviews

### Log Information
Each processed review logs:
- Proposal ID and reference number
- Lawyer name
- Original creation timestamp
- Configured timeout hours
- Processing timestamp

## Model Methods

### ConflictReview Model

#### `isExpired(): bool`
Checks if a pending conflict review has exceeded the timeout period based on working hours.

#### `markAsNoConflictDueToTimeout()`
Automatically marks the review as "no conflict" with appropriate notes and triggers proposal status update.

## Business Logic

### Timeout Calculation Example
**Configuration**: `CONFLICT_REVIEW_TIMEOUT_HOURS=48`

**Scenario 1 - Mid-week Creation**
- Created: Tuesday 10:00 AM
- Expires: Thursday 10:00 AM (48 working hours later)

**Scenario 2 - Friday Creation**
- Created: Friday 2:00 PM  
- Expires: Tuesday 2:00 PM (Friday: 10h + Monday: 24h + Tuesday: 14h = 48h)

**Scenario 3 - Weekend Handling**
- Weekends (Saturday/Sunday) are completely excluded from timeout calculation
- Only Monday-Friday hours count toward the timeout period

## Integration Points

### Existing Workflow
- Integrates seamlessly with existing conflict review process
- Maintains all existing manual review capabilities
- Does not interfere with manual lawyer responses
- Follows same proposal completion logic

### Notifications
- Uses existing proposal status change notifications
- Lawyers receive normal completion notifications when all reviews finish
- No additional notification system required

## Monitoring

### Health Checks
Monitor the system by checking:
1. Log file: `storage/logs/conflict-reviews-timeout.log`
2. Database: Conflict reviews with automatic timeout notes
3. Laravel logs: Error messages if processing fails

### Common Issues
1. **Clock Synchronization**: Ensure server time is accurate
2. **Timezone Settings**: Verify Laravel timezone matches business hours
3. **Weekend Calculation**: Check dayOfWeek calculation (0=Sunday, 6=Saturday)

## Security & Compliance

### Audit Trail
- All automatic changes are logged with timestamps
- Original creation time preserved
- Timeout reason clearly documented in notes
- No manual review data is lost or modified

### Reversibility  
- Lawyers can still manually change status after automatic timeout
- All standard conflict review workflows remain available
- Automatic notes help identify timeout-based changes