<?php

namespace App\Http\Controllers;

use App\Http\Requests\UpdateBusinessRequest;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class BusinessSettingsController extends Controller
{
    public function edit(): View
    {
        abort_unless(auth()->user()->isOwner(), 403);

        return view('settings.business', ['business' => auth()->user()->business]);
    }

    public function update(UpdateBusinessRequest $request): RedirectResponse
    {
        $data = $request->validated();
        $notifications = $data['notifications'] ?? [];
        unset($data['notifications']);
        $request->user()->business->update([...$data, 'settings' => [...($request->user()->business->settings ?? []), 'notifications' => $notifications]]);

        return back()->with('status', 'Business settings updated.');
    }
}
