# Duplicate Feature Updates

## Changes Made

### ✅ **Permission Updates**
1. **Universal Access**: All users (auth level 1+) can now duplicate ANY report, not just their own
2. **Date Restriction**: Added date validation - reports older than the day before current date cannot be duplicated
3. **User Assignment**: Duplicated reports are always assigned to the current user (regardless of original owner)

### ✅ **Date Validation Logic**
```typescript
// Client-side validation
const reportDate = new Date(report.createdDate);
const dayBeforeToday = new Date();
dayBeforeToday.setDate(dayBeforeToday.getDate() - 1);
dayBeforeToday.setHours(0, 0, 0, 0); // Set to start of day

if (reportDate < dayBeforeToday) {
  return false; // Cannot duplicate
}
```

### ✅ **Server-side Validation**
```typescript
// Server-side validation in duplicate action
const reportDate = new Date(originalReport.createdDate);
const dayBeforeToday = new Date();
dayBeforeToday.setDate(dayBeforeToday.getDate() - 1);
dayBeforeToday.setHours(0, 0, 0, 0);

if (reportDate < dayBeforeToday) {
  return json({ errors: { form: "Cannot duplicate reports older than yesterday" } }, { status: 400 });
}
```

### ✅ **UI Improvements**

#### **Desktop Table View**
- **Active Button**: Green "Duplicate" button for eligible reports
- **Disabled State**: Gray "Duplicate" text with tooltip for old reports
- **Tooltip**: "Cannot duplicate reports older than yesterday"

#### **Mobile Card View**
- **Active Button**: Full-width green button for eligible reports
- **Disabled State**: Gray disabled button with "(Too Old)" indicator
- **Clear Messaging**: Shows why duplication is not available

### ✅ **Business Rules**

#### **Who Can Duplicate**
- ✅ **Level 1 Users**: Can duplicate any report (within date range)
- ✅ **Level 2+ Users**: Can duplicate any report (within date range)
- ✅ **Cross-User**: Users can duplicate reports created by other users

#### **When Can Duplicate**
- ✅ **Today's Reports**: Can be duplicated
- ✅ **Yesterday's Reports**: Can be duplicated
- ❌ **Older Reports**: Cannot be duplicated (day before yesterday and older)

#### **What Gets Duplicated**
- ✅ All operational data (locations, equipment, pipeline, etc.)
- ✅ Time sheet entries
- ✅ Notes
- ❌ Stoppages (excluded)
- ❌ Original employee assignment (assigned to current user)

### ✅ **Error Messages**

#### **New Error Message**
```
"Cannot duplicate reports older than yesterday"
```

#### **Existing Error Messages**
- "Cannot duplicate report - sheet is already complete with both day and night shifts"
- "Cannot duplicate report - night shift already exists for this date and location"
- "Failed to duplicate report"

### ✅ **Use Cases**

#### **Primary Use Case**
1. Employee A creates a day shift report yesterday
2. Employee B (different user) sees the report today
3. Employee B clicks "Duplicate" to create a night shift
4. System creates night shift assigned to Employee B
5. Employee B can then customize the night shift as needed

#### **Date Restriction Use Case**
1. User tries to duplicate a report from 3 days ago
2. Duplicate button is disabled/grayed out
3. Tooltip explains: "Cannot duplicate reports older than yesterday"
4. Prevents duplication of stale operational data

### ✅ **Benefits of Changes**

1. **Improved Collaboration**: Any user can duplicate any recent report
2. **Data Freshness**: Prevents duplication of outdated operational data
3. **User Ownership**: Duplicated reports belong to the duplicating user
4. **Clear Feedback**: Visual indicators show when duplication is not available
5. **Operational Efficiency**: Faster completion of report sheets by any team member

### ✅ **Security Considerations**

1. **Data Access**: Users can see and duplicate any report (business requirement)
2. **Ownership**: Duplicated reports are owned by the current user
3. **Date Validation**: Prevents misuse of old operational data
4. **Sheet Integrity**: Maintains proper sheet relationships and validation

The updated duplicate feature now provides better accessibility and collaboration while maintaining data integrity through date restrictions.