<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\PageBanner;
use Illuminate\Support\Facades\File;

class PageBannerController extends Controller
{
    public function index()
    {
        $banners = PageBanner::all();
        return view('admin.pages.page-banners.index', compact('banners'));
    }

    public function create()
    {
        return view('admin.pages.page-banners.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'page' => 'required|string',
            'title' => 'required|string|max:255',
            'image' => 'nullable|image|mimes:jpeg,jpg,png,webp|max:2048',
        ]);

        $imagePath = null;

        if ($request->hasFile('image')) {
            $image = $request->file('image');
            $imageName = time() . '_' . $image->getClientOriginalName();
            $uploadPath = 'uploads/page-banners';
            $image->move($uploadPath, $imageName);
            $imagePath = $uploadPath . '/' . $imageName;
        }

        PageBanner::create([
            'page' => $request->page,
            'title' => $request->title,
            'image' => $imagePath,
        ]);

        return redirect()->route('page-banners.index')->with('success', 'Page banner created successfully.');
    }

    public function edit($id)
    {
        $banner = PageBanner::findOrFail($id);
        return view('admin.pages.page-banners.edit', compact('banner'));
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'page' => 'required|string',
            'title' => 'required|string|max:255',
            'image' => 'nullable|image|mimes:jpeg,jpg,png,webp|max:2048',
        ]);

        $banner = PageBanner::findOrFail($id);
        $imagePath = $banner->image;

        if ($request->hasFile('image')) {
            // Delete old image if exists
            if ($banner->image && File::exists($banner->image)) {
                File::delete($banner->image);
            }

            // Upload new image
            $image = $request->file('image');
            $imageName = time() . '_' . $image->getClientOriginalName();
            $uploadPath = 'uploads/page-banners';
            $image->move($uploadPath, $imageName);
            $imagePath = $uploadPath . '/' . $imageName;
        }

        $banner->update([
            'page' => $request->page,
            'title' => $request->title,
            'image' => $imagePath,
        ]);

        return redirect()->route('page-banners.index')->with('success', 'Page banner updated successfully.');
    }

    public function destroy($id)
    {
        $banner = PageBanner::findOrFail($id);

        if ($banner->image && File::exists($banner->image)) {
            File::delete($banner->image);
        }

        $banner->delete();

        return redirect()->route('page-banners.index')->with('success', 'Page banner deleted successfully.');
    }
}