<?php

namespace App\Http\Controllers;

use App\Models\ObatMasuk;
use Illuminate\Http\Request;

class KadaluarsaController extends Controller
{
    public function index(Request $request)
    {
        // Only show medicines that are expired (minus days) or expiring within 60 days (awas/waspada)
        $query = ObatMasuk::with(['obat.kategori'])
            ->where('tanggal_kadaluarsa', '<=', now()->addMonths(4));

        // Filter by remaining days
        $filter = $request->get('filter', 'all');
        
        if ($filter === 'expired') {
            // Only expired (minus days)
            $query->where('tanggal_kadaluarsa', '<', now());
        } elseif ($filter === '30') {
            // Awas: expired or <= 30 days remaining
            $query->where('tanggal_kadaluarsa', '<=', now()->addDays(30));
        } elseif ($filter === '120') {
            // Waspada: expired or <= 4 months remaining
            $query->where('tanggal_kadaluarsa', '<=', now()->addMonths(4));
        }

        $obatKadaluarsa = $query->orderBy('tanggal_kadaluarsa', 'asc')->paginate(15);

        return view('kadaluarsa.index', compact('obatKadaluarsa', 'filter'));
    }
}
