<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ScheduleSetting;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class ScheduleSettingController extends Controller
{
    public function edit(): View
    {
        return view('admin.schedule-settings.edit', [
            'item' => ScheduleSetting::query()->first() ?? new ScheduleSetting,
        ]);
    }

    public function update(Request $request): RedirectResponse
    {
        $data = $request->validate([
            'week_start_date' => 'required|date',
        ]);

        $setting = ScheduleSetting::query()->first();

        if ($setting) {
            $setting->update($data);
        } else {
            ScheduleSetting::query()->create($data);
        }

        return redirect()->route('admin.schedule-settings.edit')->with('success', 'Schedule settings updated.');
    }
}
