<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\AppSetting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class AppSettingController extends Controller
{
    public function edit()
    {
        $logoPath = AppSetting::get('global_logo');
        return view('admin.settings.branding', compact('logoPath'));
    }

    public function update(Request $request)
    {
        $request->validate([
            'logo' => 'required|image|mimes:png,jpg,jpeg,svg|max:2048',
        ]);

        $oldPath = AppSetting::get('global_logo');
        if ($oldPath) {
            Storage::disk('public')->delete($oldPath);
        }

        $path = $request->file('logo')->store('branding', 'public');
        AppSetting::set('global_logo', $path);

        return back()->with('status', 'Global logo updated.');
    }
}
