<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\HomeLogos;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\JsonResponse;
class HomeLogosController extends Controller
{
    public function index()
    {
        $sliders = HomeLogos::orderBy('created_at', 'desc')->get();
        
        return view('admin.homelogos.index', compact('sliders'));

    }
    public function create(){
        $today = date("Y-m-d");
        $slider = "";
        $title       = "";
        return view('admin.homelogos.create', compact('today', 'slider'));

    }

    public function store(Request $request)
    {
        $rules = [
            'name' => 'required|string|max:255',
            'status' => 'required|in:0,1',
            'image' => $request->id ? 'nullable|image|mimes:jpg,jpeg,png,gif|max:2048' : 'required|image|mimes:jpg,jpeg,png,gif|max:2048',
        ];

        $validator = Validator::make($request->all(), $rules);

        if ($validator->fails()) {
            return response()->json([
                'status' => 'error',
                'errors' => $validator->errors(),
            ], 422);
        }

        if ($request->id) {
            $slider = homelogos::find($request->id);

            if (!$slider) {
                return response()->json(['status' => 'error', 'message' => 'Home Page Logo not found'], 404);
            }

            // Check for duplicate slider name
            $existingSlider = homelogos::where('name', $request->name)
                ->where('id', '!=', $request->id)
                ->first();

            if ($existingSlider) {
                return response()->json([
                    'status' => 'error',
                    'errors' => [
                        'name' => ['The Home Page Logo title already exists.']
                    ]
                ], 422);
            }

            $slider->name = $request->name;
            $slider->status = $request->status;

            if($request->file("image")){
                $response = image_upload($request,config('global.home_logo_image_upload_dir'),'image');
                if($response['status']){
                    $slider->image = $response['link'];
                }
            }

            $slider->save();

          

            return response()->json(['status' => 'success', 'message' => 'Home Page Logo updated successfully']);
        } else {
            // Check for duplicate slider name
            $existingSlider = homelogos::where('name', $request->name)->first();

            if ($existingSlider) {
                return response()->json([
                    'status' => 'error',
                    'errors' => [
                        'name' => ['The Work Process name already exists.']
                    ]
                ], 422);
            }

            if($request->file("image")){
                $response = image_upload($request,config('global.home_logo_image_upload_dir'),'image');
                if($response['status']){
                    $fileName = $response['link'];
                }
            }
            $slider = homelogos::create([
                'name' => $request->name,
                'status' => $request->status,
                'image' => $fileName,
            ]);

            

            return response()->json([
                'status' => 'success',
                'message' => 'Home Page Logo added successfully',
                'slider' => $slider,
            ]);
        }
    }

    


  
    public function edit($id) {
        $slider = homelogos::findOrFail($id);
        return view('admin.homelogos.create', compact('slider'));
    }

    public function destroy($id): JsonResponse
    {
        try {
            $slider = homelogos::findOrFail($id);
            
            $slider->delete();
            return response()->json([
                'status' => 'success',
                'message' => 'Home Page Logo deleted successfully.'
            ], 200);
        } catch (ModelNotFoundException $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Home Page Logo not found.'
            ], 404);
        } catch (Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'An error occurred while deleting the Slider.'
            ], 500);
        }
    }

    public function statusChange(Request $request, $id) {
        $slider = homelogos::find($id);

        if (!$slider) {
            return response()->json([
                'status' => 'error',
                'message' => 'Home Page Logo not found.'
            ], 404);
        }
    
        $slider->status = $request->status;
        $slider->save();
    
        return response()->json([
            'status' => 'success',
            'message' => 'Home Page Logo status updated successfully.'
        ]);
    }
}
