# Implementation Plan: Enhanced Exit Permit System

This plan details the upgrade for the Exit Permit module to enforce strict attendance rules, dynamic manager routing, and reporting capabilities.

## Proposed Changes

### 1. Database Adjustments

#### [NEW] `[timestamp]_add_fields_to_exit_permits_table.php`
- Modify the `exit_permits` table to include:
  - `estimated_return_time` (datetime): as requested.
  - `notes` (text, nullable): to store rejection reasons or additional comments.

### 2. Service Integration

#### [MODIFY] `app/Services/LeaveApprovalService.php` (Renaming/Refactoring)
- I will rename or utilize `LeaveApprovalService` (possibly aliased as a general `ApprovalRoutingService`) to dynamically fetch the L1 approver based on the user's role and primary site, keeping logic DRY (Don't Repeat Yourself).

### 3. Application Controllers

#### [MODIFY] `app/Http/Controllers/Api/ExitPermitController.php`
- `store()`: 
  - **Validation 1**: Check `Attendance` table to ensure the user has clocked in today and has not clocked out.
  - **Validation 2**: Check `ExitPermit` table to ensure the user has no active permits (pending or approved but not returned).
  - Triggers Notification directly to the dynamically resolved L1 Approver.
- `approve()`: Standard approval.
- `reject()`: Enforces the requirement of a `notes` field for rejection reason.
- `checkIn()`: Renames/updates the existing return logic.
- `myPermits()`: Displays personal history.
- `summary()`: New endpoint to aggregate exit permits grouped by `site_id` and formatted by month.

### 4. Notifications

#### [NEW] `app/Notifications/ExitPermitActionNotification.php`
- A database notification sent to the L1 Approver upon creation, and sent to the requester upon approval/rejection.

### 5. Policies & Resources

#### [MODIFY] `app/Policies/ExitPermitPolicy.php`
- Restrict `approve` and `reject` entirely to the designated dynamic L1 approver (or Super Admin/HR override).

#### [MODIFY] `app/Http/Resources/ExitPermitResource.php`
- Include the new `estimated_return_time` and `notes`.

#### [MODIFY] `routes/api.php`
- Register `my-permits`, `check-in` (replacing `return`), and `summary` report endpoints.

## Verification Plan
1. **Validation Checks**: Test `store()` with a user who hasn't clocked in yet to verify rejection.
2. **Approver Resolution**: Submit an exit permit and verify the correct L1 manager receives a database notification.
3. **Reporting Check**: Access the `summary` endpoint to confirm correctly grouped monthly metrics per site.
