<?php

namespace App\Http\Controllers\Admin\Meal;

use App\Http\Controllers\Controller;
use App\Models\Meal\MealReview;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class MealReviewController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = MealReview::query()->with(['mealPlan', 'customer']);
        $query->when($request->customer_id, fn($q, $v) => $q->where('customer_id', $v));
        $query->when($request->daily_meal_plan_id, fn($q, $v) => $q->where('daily_meal_plan_id', $v));
        $query->when($request->score, fn($q, $v) => $q->where('score', $v));

        return $this->paginate($query->latest('created_at')->paginate($request->input('per_page', 15)));
    }

    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'daily_meal_plan_id' => 'required|exists:daily_meal_plans,id',
            'customer_id' => 'required|exists:customers,id',
            'score' => 'nullable|integer|min:1|max:5',
            'content' => 'nullable|string',
        ]);
        return $this->success(MealReview::create($validated));
    }
}
