<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\SiteSetting;
use Illuminate\Http\Request;

class SiteSettingController extends Controller
{
    public function index()
    {
        $settings = SiteSetting::pluck('value', 'key')->toArray();
        return view('admin.settings.index', compact('settings'));
    }

    public function update(Request $request)
    {
        $data = $request->except('_token', 'site_logo');
        
        if ($request->hasFile('site_logo')) {
            $imageName = time() . '_logo.' . $request->file('site_logo')->extension();
            $request->file('site_logo')->move(public_path('uploads/settings'), $imageName);
            SiteSetting::updateOrCreate(['key' => 'site_logo'], ['value' => 'uploads/settings/' . $imageName]);
        }
        
        foreach ($data as $key => $value) {
            SiteSetting::updateOrCreate(['key' => $key], ['value' => $value]);
        }

        return back()->with('success', 'Settings updated successfully.');
    }
}
