<?php

namespace App\Http\Controllers;

use App\Models\SiteSetting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class SiteSettingController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('permission:view-dashboard');
    }

    /**
     * Display the site settings form
     */
    public function index()
    {
        $settings = SiteSetting::getSettings();
        
        return view('backend.site-settings.index', compact('settings'));
    }

    /**
     * Update the site settings
     */
    public function update(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'site_name' => 'required|string|max:255',
            'currency' => 'required|string|max:10',
        ]);

        if ($validator->fails()) {
            return redirect()->back()
                ->withErrors($validator)
                ->withInput();
        }

        $settings = SiteSetting::getSettings();
        $settings->update([
            'site_name' => $request->site_name,
            'currency' => $request->currency,
        ]);

        return redirect()->route('site-settings.index')
            ->with('success', 'Site settings updated successfully!');
    }
}

