<?php

namespace App\Http\Controllers;
use App\Services\PredictionService;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade\Pdf;
use Carbon\Carbon;

class HistoryPredictionController extends Controller
{
    public function history()
    {
        $data = DB::table('prediction_results as p')
            ->join('items as i', 'p.kode', '=', 'i.kode')
            ->select(
                'p.id',
                'p.kode',
                'i.nama_barang',
                'i.jenis',
                'p.bulan_prediksi',
                'p.tahun_prediksi',
                'p.jumlah_prediksi',
                'i.satuan',
                'p.tanggal_prediksi'
            )
            ->orderBy('p.tanggal_prediksi', 'desc')
            ->get();
        $periodeList = DB::table('prediction_results')
            ->select('bulan_prediksi', 'tahun_prediksi')
            ->distinct()
            ->orderBy('tahun_prediksi')
            ->orderBy('bulan_prediksi')
            ->get();
        $jenisList = DB::table('items')
            ->select('jenis')
            ->distinct()
            ->pluck('jenis');
        return view('riwayat-prediksi', compact(
            'data',
            'periodeList',
            'jenisList'
        ));
    }

    public function cetakPdf(Request $request)
    {
        $bulan = $request->bulan;
        $jenis = $request->jenis;
        $search = $request->search;
        $query = DB::table('prediction_results as p')
            ->join('items as i', 'p.kode', '=', 'i.kode')
            ->select(
                'p.kode',
                'i.nama_barang',
                'i.jenis',
                'p.jumlah_prediksi',
                'i.satuan',
                'p.tanggal_prediksi',
                'p.bulan_prediksi',
                'p.tahun_prediksi'
            );
        if ($bulan) {
            [$tahun, $bln] = explode('-', $bulan);
            $query->where('p.tahun_prediksi', $tahun)
                ->where('p.bulan_prediksi', (int) $bln);
        }
        if ($jenis) {
            $jenisArray = explode(',', $jenis);
            $query->whereIn(
                DB::raw('LOWER(i.jenis)'),
                array_map('strtolower', $jenisArray)
            );
        }
        if ($search) {
            $query->where(function ($q) use ($search) {
                $q->where('p.kode', 'like', "%{$search}%")
                    ->orWhere('i.nama_barang', 'like', "%{$search}%");
            });
        }
        $data = $query
            ->orderBy('p.tanggal_prediksi', 'desc')
            ->get();
        $periode = '-';
        if ($bulan) {
            $periode = Carbon::createFromFormat('Y-m', $bulan)
                ->translatedFormat('F Y');
        }
        $pdf = Pdf::loadView('riwayat-prediksi-pdf', [
            'rows' => $data,
            'periode' => $periode
        ])->setPaper('a4', 'landscape');
        return $pdf->download('riwayat-prediksi.pdf');
    }

    public function destroy($id)
    {
        DB::table('prediction_results')
            ->where('id', $id)
            ->delete();
        return response()->json([
            'success' => true,
            'message' => 'Data berhasil dihapus.'
        ]);
    }
}