<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class LogoUploadController extends Controller
{
    use \App\Http\Controllers\Concerns\ResolvesCurrentOrganization;

    public function update(Request $request)
    {
        abort_unless($this->currentUserCan($request, 'customize_widget'), 403);

        $request->validate([
            'logo' => ['required', 'image', 'max:2048'], // 2MB max
        ]);

        $organization = $this->currentOrganization($request);
        if (! $organization) {
            abort(404);
        }

        if ($request->hasFile('logo')) {
            $path = $request->file('logo')->store('logos', 'public');
            $organization->update(['logo_path' => $path]);
        }

        return back()->with('status', 'Logo updated successfully.');
    }
}
