<?php

namespace App\Http\Controllers;

use App\Models\Bestsellers;
use Illuminate\Http\Request;
use Pest\Support\View;
use RealRashid\SweetAlert\Facades\Alert;

class BestsellersController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(Bestsellers $bestsellers)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit( $id)
    {
        $bestsellers = Bestsellers::findOrFail($id);
        return view('backend.pages.bestseller.edit', compact('bestsellers'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request,  $id)
    {
        $bestsellers = Bestsellers::findOrFail($id);

        $request->validate([
            'image'       => 'nullable|image',
            'title'        => 'nullable|string',
            'alt_text' => 'nullable|string',
            'price' => 'nullable|string',
            'description'  => 'nullable|string',
            'author' => 'nullable|string',
            'amazon_link' => 'nullable|string',

            'quta_title'        => 'nullable|string',
            'quta_description'  => 'nullable|string',
            'quta_author' => 'nullable|string',

        ]);



        $imagePath = $bestsellers->image;

        if ($request->hasFile('image')) {
            // Delete old image safely
            if ($bestsellers->image && file_exists(public_path($bestsellers->image))) {
                @unlink(public_path($bestsellers->image));
            }

            $file = $request->file('image');
            $filename = time() . '_' . $file->getClientOriginalName();
            $file->move(public_path('storage/bestsellers_images'), $filename);
            $imagePath = 'storage/bestsellers_images/' . $filename;
        }

        $bestsellers->update([
            'image'       => $imagePath,
            'title'        => $request->title,
            'alt_text' => $request->alt_text,
            'price' => $request->price,
            'amazon_link' => $request->amazon_link,
            'author' => $request->author,
            'description' => $request->description,

            'quta_title'        => $request->quta_title,
            'quta_author' => $request->quta_author,
            'quta_description' => $request->quta_description,


        ]);

            Alert::success('Success ', 'Bestsellers item updated successfully.');
            return redirect()->route('bestsellers.edit', $bestsellers->id);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Bestsellers $bestsellers)
    {
        //
    }
}
