<?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
{
    /**
     * Redirect to edit if setting exists, else add page
     */
    public function checktoredirect()
    {
        $data = SiteSetting::first();
        return $data
            ? redirect()->route('edit.sitesetting', $data->id)
            : redirect()->route('sitesetting.add');
    }

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

    /**
     * Store new site settings
     */
    public function store(Request $request)
    {
        $request->validate($this->getValidationRules());

        $dataArray = $request->except('site_logo');

        // Handle logo upload
        if ($request->hasFile('site_logo')) {
            $image = $request->file('site_logo');

            // Optional: prepend company/site title to filename
            $prefix = $request->site_title ? Str::slug($request->site_title) . '_' : '';
            $imageName = $prefix . 'logo_' . Str::uuid() . '.' . $image->getClientOriginalExtension();

            $image->move(public_path('assets/images'), $imageName);
            $dataArray['site_logo'] = $imageName;
        }

        $data = SiteSetting::create($dataArray);

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

    /**
     * Show edit page
     */
    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'));
    }

    /**
     * Update existing site settings
     */
    public function Update(Request $request, $id)
    {
        $request->validate($this->getValidationRules());

        $data = SiteSetting::findOrFail($id);
        $dataArray = $request->except('site_logo');

        if ($request->hasFile('site_logo')) {
            $image = $request->file('site_logo');
            $prefix = $request->site_title ? Str::slug($request->site_title) . '_' : '';
            $imageName = $prefix . 'logo_' . Str::uuid() . '.' . $image->getClientOriginalExtension();

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

            $image->move(public_path('assets/images'), $imageName);
            $dataArray['site_logo'] = $imageName;
        }
        $data->fill($dataArray);
        $data->save();

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

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