<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers\PredictionController;
use App\Http\Controllers\ProcurementController;
use App\Http\Controllers\HistoryPredictionController;
use App\Http\Controllers\MonitoringController;

Route::get('/prediksi-barang', [PredictionController::class, 'index']);
Route::post('/prediksi-barang/run', [PredictionController::class, 'runPrediction']);
Route::post('/prediksi-barang/save', [PredictionController::class, 'save']);
Route::get('/search-items', function (Request $request) {
    $q = $request->q;
    $items = DB::table('items')
        ->where('kategori', 0)
        ->where(function ($query) use ($q) {
            $query
                ->where('nama_barang', 'like', '%' . $q . '%')
                ->orWhere('kode', 'like', '%' . $q . '%');
        })
        ->select('kode', 'nama_barang', 'jenis')
        ->limit(10)
        ->get();
    return response()->json($items);
});
Route::get('/all-items', function () {
    $items = DB::table('items as i')
        ->join('item_usage_monthly as h', 'i.kode', '=', 'h.kode')
        ->where('i.kategori', 0)
        ->groupBy(
            'i.kode',
            'i.nama_barang',
            'i.jenis'
        )
        ->havingRaw('COUNT(h.kode) >= 6')
        ->select(
            'i.kode',
            'i.nama_barang',
            'i.jenis'
        )
        ->orderBy('i.nama_barang')
        ->get();
    return response()->json($items);
});

Route::get('/riwayat-prediksi', [HistoryPredictionController::class, 'history']);
Route::delete('/riwayat-prediksi/{id}', [HistoryPredictionController::class, 'destroy'])
    ->name('riwayat-prediksi.destroy');
Route::get('/riwayat-prediksi/cetak/pdf', [HistoryPredictionController::class, 'cetakPdf'])
    ->name('riwayat-prediksi.pdf');

Route::match(['get', 'post'], '/rencana-pengadaan', [ProcurementController::class, 'index']);
Route::get('/rencana-pengadaan/cetak', [ProcurementController::class, 'cetak']);
Route::post('/rencana-pengadaan/set-periode', [ProcurementController::class, 'setPeriode']);

Route::get('/detail-rencana-pengadaan/{kode}/{periode}', [ProcurementController::class, 'detail']);

Route::get('/monitoring-prediksi', [MonitoringController::class, 'index'])->name('monitoring-prediksi');
Route::delete('/monitoring/delete/{kode}', [MonitoringController::class, 'delete']);
Route::get('/monitoring/print', [MonitoringController::class, 'printPdf'])
    ->name('monitoring.print');

// Route::get('/', function () {
//     return view('welcome');
// });