<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
// use App\Http\Controllers\PhotoController;
use App\Http\Controllers\SensorController;
use App\Http\Controllers\ESP32CamController;
use App\Http\Controllers\HistoryController;
use Illuminate\Support\Facades\Storage;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "api" middleware group. Make something great!
|
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

// Route untuk ESP32Cam dan foto
Route::post('/esp32cam/upload', [ESP32CamController::class, 'upload']);
Route::get('/esp32cam/cleanup', [ESP32CamController::class, 'cleanup']);
Route::get('/esp32cam/latest', [ESP32CamController::class, 'latest']);
Route::post('/esp32cam/delete', [ESP32CamController::class, 'delete'])->name('api.photos.delete');
Route::post('/esp32cam/fetch-from-camera', [ESP32CamController::class, 'fetchFromCamera'])->name('api.esp32cam.fetch');

// Route untuk proxy kamera ESP32-CAM
Route::get('/proxy-camera/status', [ESP32CamController::class, 'proxyStatus'])->name('api.proxy.status');
Route::get('/proxy-camera/stream', [ESP32CamController::class, 'proxyStream'])->name('api.proxy.stream');
Route::get('/proxy-camera/stopstream', [ESP32CamController::class, 'proxyStopStream'])->name('api.proxy.stopstream');
Route::get('/proxy-camera/settings', [ESP32CamController::class, 'proxySettings'])->name('api.proxy.settings');
Route::get('/proxy-camera/capture', [ESP32CamController::class, 'proxyCapture'])->name('api.proxy.capture');

// Route test untuk ESP32CAM
Route::get('/esp32cam/test', function() {
    return response()->json(['status' => 'success', 'message' => 'ESP32CAM test endpoint berfungsi!']);
});

// Route untuk sensor data
Route::post('/sensor-update', [SensorController::class, 'update']);
Route::get('/sensor-data', [SensorController::class, 'latest']);
Route::get('/sensor-history', [SensorController::class, 'history']);

// Routes untuk history
Route::get('/history', [HistoryController::class, 'getAll']);
Route::post('/history', [HistoryController::class, 'store']);
Route::post('/history/clear', [HistoryController::class, 'clear']);
Route::post('/history/delete', [HistoryController::class, 'delete']);
Route::get('/history/firebase', [HistoryController::class, 'getFirebaseData']);
Route::post('/history/sync-firebase', [HistoryController::class, 'syncFromFirebase']);
Route::get('/history/current-sensor-data', [HistoryController::class, 'getCurrentSensorData']);

// Route untuk testing file report.json
Route::get('/test-report-json', function() {
    $reportFilePath = 'public/reports/report.json';
    $exists = Storage::exists($reportFilePath);
    $data = $exists ? json_decode(Storage::get($reportFilePath), true) : null;
    $size = $exists ? Storage::size($reportFilePath) : 0;
    
    return response()->json([
        'file_exists' => $exists,
        'file_size' => $size,
        'data_count' => $data ? count($data) : 0,
        'data' => $data
    ]);
});
