# Implementation Plan: Multi-Level Leave Approval System

This plan details the implementation of a robust, hierarchical leave approval system based on the exact specifications provided.

## User Review Required
> [!IMPORTANT]
> - By default, the `leave_requests` table `status` column only accepts `pending`, `approved`, and `rejected`. To support the requested `cancel()` functionality, I will create a migration to **add `cancelled` to this enum**. 
> - I will use the employee's **primary assigned site** (their first associated site) to determine the local approvers (Supervisor and Cinema Manager), as `LeaveRequest` does not currently specify a single `site_id`. Let me know if this needs adjustment!

## Proposed Changes

### 1. Database Migrations

#### [NEW] `[timestamp]_add_cancelled_status_to_leave_requests_table.php`
- Modify the `status` enum on `leave_requests` and `leave_approvals` to include `cancelled`.

### 2. Core Service Logic

#### [NEW] `app/Services/LeaveApprovalService.php`
A dedicated service to dynamically fetch approvers.
- Maps the requester's role to the required Approval Levels.
- L1 & L2 determination logic:
    - `crew` / `crew_leader` -> L1: Supervisor (Site), L2: Cinema Manager (Site)
    - `supervisor` -> L1: Cinema Manager (Site), L2: Head Dev Ops (Global)
    - `cinema_technical_support` -> L1: Cinema Manager (Site), L2: Head Dev Tech (Global)
    - `cinema_manager` -> L1: Head Dev Ops (Global)
- Throws clear exceptions if required managers are missing from the system.

### 3. Application Controllers

#### [MODIFY] `app/Http/Controllers/Api/LeaveRequestController.php`
- `index()`: Filterable list for Admins/Approvers.
- `myRequests()`: Authenticated user's personal leave history.
- `store()`: Utilizes `LeaveApprovalService` to instantiate `LeaveApproval` rows instantly. Triggers Notification to L1.
- `show()`: Displays complete routing history.
- `cancel()`: Immediately cancels the request and cascading approvals if still pending.

#### [MODIFY] `app/Http/Controllers/Api/LeaveApprovalController.php`
- `approve()`: Updates the specific approval row. If it's L1 and L2 exists, notifies L2. If it's the final level, updates the parent `LeaveRequest` to `approved`.
- `reject()`: Marks the specific approval and parent request as `rejected`.

### 4. Notifications

#### [NEW] `app/Notifications/LeaveActionNotification.php`
Used for two scenarios (database string channels):
1. Notifying an Approver that they have an action pending.
2. Notifying the requester that their status has been updated (Approved/Rejected).

### 5. Policies & Resources

#### [NEW] `app/Policies/LeaveApprovalPolicy.php`
- Only the user explicitly named in `approver_id` or a `super_admin`/`hr` override can approve/reject.

#### [MODIFY] `app/Http/Resources/LeaveRequestResource.php` & `LeaveApprovalResource.php`
- Adjust to cleanly display the updated states and hierarchical level info.

#### [MODIFY] `routes/api.php`
- Register `my-requests` and `cancel` endpoints for leave requests.

## Verification Plan
1. **Migrations**: Ensure DB structure applies without data loss.
2. **Logic Mapping**: Use a test script to simulate request creations from various roles (Crew, Supervisor, etc.) ensuring the correct user IDs are generated for `leave_approvals`.
3. **Escalation Flow**: Trigger an L1 approval and ensure `LeaveRequest` remains pending but L2 receives a notification.
