<?php
namespace App\Http\Controllers\Admin\Nanny;
use App\Http\Controllers\Controller;
use App\Models\Nanny\NannySchedule;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class NannyScheduleController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = NannySchedule::query()->with(['nanny','order']);
        $query->when($request->nanny_id, fn($q, $v) => $q->where('nanny_id', $v));
        $query->when($request->start_date, fn($q, $v) => $q->where('schedule_date', '>=', $v));
        $query->when($request->end_date, fn($q, $v) => $q->where('schedule_date', '<=', $v));
        return $this->paginate($query->orderBy('schedule_date')->paginate($request->input('per_page', 31)));
    }

    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'nanny_id'=>'required|exists:nannies,id','nanny_order_id'=>'nullable|exists:nanny_orders,id',
            'schedule_date'=>'required|date','type'=>'nullable|integer|in:1,2,3','remark'=>'nullable|string|max:255',
        ]);
        return $this->success(NannySchedule::create($validated));
    }
}
