# Implementation Plan - Advanced Attendance System

This plan refactors the existing attendance logic to meet the specific requirements for validation, storage, and API structure.

## Proposed Changes

### Configuration

#### [MODIFY] [bootstrap/app.php](file:///d:/Laragon/www/absensi/bootstrap/app.php)
- Register Spatie Permission middleware: `role`, `permission`, `role_or_permission`.

### Request Validation

#### [NEW] [ClockRequest.php](file:///d:/Laragon/www/absensi/app/Http/Requests/ClockRequest.php) [NEW] [GetLogRequest.php](file:///d:/Laragon/www/absensi/app/Http/Requests/GetLogRequest.php)
- Implement `ClockRequest` to handle common validation for Clock In and Clock Out (latitude, longitude, site_id, photo).
- Implement `GetLogRequest` for date range filtering with proper date formats.

### Authorization

#### [NEW] [AttendancePolicy.php](file:///d:/Laragon/www/absensi/app/Http/Policies/AttendancePolicy.php)
- Implement `create` (for clocking in/out): Ensure user is assigned to the requested site via `user_sites` table.

### API Resources

#### [NEW] [AttendanceResource.php](file:///d:/Laragon/www/absensi/app/Http/Resources/AttendanceResource.php)
- Format the attendance data, including user info, site info, and human-readable timestamps.

### Core Logic Refactoring

#### [MODIFY] [AttendanceController.php](file:///d:/Laragon/www/absensi/app/Http/Controllers/Api/AttendanceController.php)
- **`clockIn`**:
    - Use `ClockRequest` for validation.
    - Validate site assignment via Policy.
    - Implement base64 and file upload handling for photos.
    - Store photo in `attendances/{user_id}/{date}/` path.
    - Calculate `distance_from_site`.
    - **Mark as Late**: Compare `timestamp` against `schedule.shift_start` + 15 minutes.
- **`clockOut`**:
    - Similar logic to clockIn but for closing the day's record.
- **`getLog`**:
    - Filter by `start_date` and `end_date`.
    - Return data wrapped in `AttendanceResource`.

### API Routes

#### [MODIFY] [api.php](file:///d:/Laragon/www/absensi/routes/api.php)
- Group routes with `auth:sanctum` and `role_or_permission` (e.g., `role:crew|supervisor`).

## Verification Plan

### Automated Tests
- Create a test script to simulate:
    - Successful Clock In within grace period.
    - Late Clock In (past 15 mins).
    - Failed Clock In (outside radius).
    - Failed Clock In (wrong site).
    - Log retrieval with date filtering.

### Manual Verification
- Verify the storage path structure in `storage/app/public/attendances`.
- Test JSON responses for consistency with `AttendanceResource`.
