<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller; 

use Illuminate\Http\Request;
use App\Models\SiteSetting;
use Illuminate\Support\Str;

class SiteSettingController extends Controller
{
    public function checktoredirect()
    {
        $data = SiteSetting::first();
        if ($data) {
            return redirect()->route('edit.sitesetting', $data->id);
        } else {
            return redirect()->route('sitesetting.add');
        }
    }

    public function Add()
    {
        $title = "Create Site Setting";
        $url = route('sitesetting.store');
        return view('admin.sitesetting.create', compact('title', 'url'));
    }

    public function store(Request $request)
    {
        $request->validate($this->getValidationRules());

        // Handle file upload
        if ($request->hasFile('site_logo')) {
            $dirname = public_path('images');
            if (!file_exists($dirname)) {
                mkdir($dirname, 0755, true);
            }

            $image = $request->file('site_logo');
            $imageName = 'site_logo_' . Str::uuid() . '.' . $image->getClientOriginalExtension();
            $image->move($dirname, $imageName);
            $request->merge(['site_logo' => $imageName]);
        } else {
            $request->merge(['site_logo' => null]);
        }

        $data = new SiteSetting($request->all());
        $data->save();

        return redirect()->route('edit.sitesetting', ['id' => $data->id])
            ->with('success', 'Site settings saved successfully!');
    }

    public function Edit($id)
    {
        $siteSetting = SiteSetting::findOrFail($id);
        $title = "Edit Site Setting";
        $url = route('update.sitesetting', $id);

        return view('admin.sitesetting.create', compact('title', 'url', 'siteSetting'));
    }

    public function Update(Request $request, $id)
    {
        $request->validate($this->getValidationRules());

        $data = SiteSetting::findOrFail($id);

        // Handle file upload
        if ($request->hasFile('site_logo')) {
            $dirname = public_path('media/images');
            if (!file_exists($dirname)) {
                mkdir($dirname, 0755, true);
            }

            // Delete old logo if it exists
            if ($data->site_logo && file_exists($dirname . '/' . $data->site_logo)) {
                unlink($dirname . '/' . $data->site_logo);
            }

            $image = $request->file('site_logo');
            $imageName = 'logo.png'; // Always save as logo.png
            $image->move($dirname, $imageName);
            $request->merge(['site_logo' => $imageName]);
        } else {
            $request->merge(['site_logo' => $data->site_logo]);
        }

        $data->fill($request->all());
        $data->updated_at = now();
        $data->save();

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

    // Validation rules extracted for reusability
    private function getValidationRules()
    {
        return [
            'site_title' => 'nullable|string',
            'sidebar_bg_color' => 'nullable|string',
            'sidebar_text_color' => 'nullable|string',
            'nav_bg_color' => 'nullable|string',
            'nav_text_color' => 'nullable|string',
            'navtop_bg_color' => 'nullable|string',
            'navtop_text_color' => 'nullable|string',
            'footer_bg_color' => 'nullable|string',
            'footer_text_color' => 'nullable|string',
            'email' => 'nullable|email',
            'phone' => 'nullable|string',
            'address' => 'nullable|string',
            'facebook' => 'nullable|string',
            'linkedin' => 'nullable|string',
            'twitter' => 'nullable|string',
            'instagram' => 'nullable|string',
            'youtube' => 'nullable|string',
            'site_logo' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            'meta_keyword' => 'nullable|string',
            'meta_description' => 'nullable|string',
        ];
    }

}
