<?php

namespace App\Http\Controllers\Admin;

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

class SiteSettingController extends Controller
{
    public function edit()
    {
        $settings = SiteSetting::firstOrCreate(['id' => 1]);

        return view('admin.settings.edit', compact('settings'));
    }

    public function update(Request $request)
    {
        $validated = $request->validate([
            'company_name' => 'nullable|string|max:255',
            'phone_primary' => 'nullable|string|max:255',
            'phone_secondary' => 'nullable|string|max:255',
            'email_primary' => 'nullable|email|max:255',
            'email_secondary' => 'nullable|email|max:255',
            'address' => 'nullable|string|max:500',
            'whatsapp' => 'nullable|string|max:255',
            'facebook' => 'nullable|url|max:500',
            'instagram' => 'nullable|url|max:500',
            'linkedin' => 'nullable|url|max:500',
            'youtube' => 'nullable|url|max:500',
            'map_url' => 'nullable|url|max:1000',
            'footer_description' => 'nullable|string',
            'copyright' => 'nullable|string|max:255',
            'working_hours' => 'nullable|string|max:255',
            'logo' => 'nullable|image|mimes:jpeg,jpg,png,webp,svg|max:2048',
            'favicon' => 'nullable|image|mimes:jpeg,jpg,png,webp,svg|max:1024',
        ]);

        $settings = SiteSetting::firstOrCreate(['id' => 1]);

        if ($request->hasFile('logo')) {
            $validated['logo'] = FileUploadService::store($request->file('logo'), 'settings');
        }
        if ($request->hasFile('favicon')) {
            $validated['favicon'] = FileUploadService::store($request->file('favicon'), 'settings');
        }

        $settings->update($validated);

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