<?php

namespace App\Http\Controllers;

use App\Models\Company;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class SettingsController extends Controller
{
    public function edit(Request $request): View
    {
        $company = $request->user()->company;
        $this->authorize('update', $company);

        return view('settings.edit', ['company' => $company]);
    }

    public function update(Request $request): RedirectResponse
    {
        $company = $request->user()->company;
        $this->authorize('update', $company);

        $validated = $request->validate([
            'name' => ['required', 'string', 'max:255'],
        ]);

        $company->update(['name' => $validated['name']]);

        return redirect()->route('settings.edit')->with('success', 'Company settings saved.');
    }
}
