# Implementation Plan: Operational/HR Dashboard & Reporting

This plan details the creation of a high-performance Operational Dashboard with comprehensive caching, advanced Eloquent aggregations, scalable role-based access, and Excel/CSV exporting functionality.

## Core Setup

### 1. Dependencies
- Install `maatwebsite/excel` for handling CSV/Excel exports natively in Laravel.

### 2. Service Layer: `DashboardService.php`
A centralized service to run complex database aggregations efficiently without N+1 query problems.
- Methods:
  - `getSummary(User $user, array $filters = [])` -> Returns deeply aggregated nested metrics.
  - Aggregation methods: `getTotalActiveStaff`, `getTodayAttendanceMetrics`, `getMonthlyLeaveMetrics`, `getTodayExitPermits`.
- **Caching**: The `getSummary` method will wrap its return data in `Cache::remember()` with a 5-minute Time-To-Live (TTL) to guarantee blazing fast load times on the frontend, dynamically caching by the user's ID/Permissions.
- **Authorization**: The service will automatically restrict queries:
  - `super_admin / hr / hq`: Unrestricted.
  - `cinema_manager`: Filtered down to `WHERE site_id IN (manager_sites)`.
  - `supervisor`: Filtered down to `WHERE site_id IN (manager_sites) AND user.role IN ('crew', 'crew_leader')`.

### 3. Application Controllers

#### [NEW] `app/Http/Controllers/Api/DashboardController.php`
- `summary()`: Serves the cached metrics payload.
- `attendanceReport()`: Endpoint serving detailed, paginated `Attendance` logs with extensive filtering (site, date, user).
- `leaveReport()`: Endpoint serving detailed `LeaveRequest` histories with similar filtering.
- `lateReport()`: Serves `Attendance` records explicitly tagged with `status = 'late'`.
*(Note: All endpoints implicitly utilize the Authorization scoping rules defined above).*

#### [NEW] `app/Http/Controllers/Api/ExportController.php`
- `exportAttendance()`: Generates and streams down an `.xlsx` or `.csv` file detailing attendance reports, structured using `Maatwebsite\Excel` Export classes.
- `exportLeave()`: Exports leave requests in similar fashion.

### 4. Export Classes

#### [NEW] `app/Exports/AttendanceExport.php`
Utilizes `FromQuery`, `WithHeadings`, and `WithMapping` from Maatwebsite to chunk data efficiently avoiding memory exhaustion when dealing with thousands of rows.

#### [NEW] `app/Exports/LeaveExport.php`
Similar setup tailored for mapping leave data.

### 5. Routing

#### [MODIFY] `routes/api.php`
- Register the `DashboardController` endpoints under an `/operational/dashboard` prefix.
- Register the `ExportController` endpoints under an `/operational/export` prefix.
- Protect all with the required Operational roles middleware (`check_role:super_admin,hr,hq,cinema_manager,supervisor,head_dev_ops,head_dev_tech,head_dev_hq`).

## Verification Plan
1. **Caching**: Ensure that repeated hits to the `summary` endpoint do not increment database query counts by testing in Tinker.
2. **Authorization Scopes**: Verify that a simulated Supervisor request only returns metrics and datasets for their `crew` within their assigned site.
3. **Exports**: Download a sample CSV directly through the API to ensure file formatting and column mappings are correct.
