<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Banner;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class BannerController extends Controller
{
    public function index()
    {
        $banners = Banner::orderBy('sort_order')
            ->orderByDesc('id')
            ->paginate(20);

        return view('backend.banners.index', compact('banners'));
    }

    public function create()
    {
        $banner = new Banner();
        return view('backend.banners.form', [
            'banner' => $banner,
            'mode'   => 'create',
        ]);
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'title'       => ['nullable', 'string', 'max:191'],
            'image'       => ['required', 'image', 'max:4096'], // 4MB
            'button_text' => ['nullable', 'string', 'max:100'],
            'button_url'  => ['nullable', 'string', 'max:255'],
            'sort_order'  => ['nullable', 'integer', 'min:0'],
            'status'      => ['nullable', 'boolean'],
        ]);

        // image upload
        $path = $request->file('image')->store('banners', 'public');
        $data['image'] = $path;
        $data['sort_order'] = $data['sort_order'] ?? 0;
        $data['status'] = $request->boolean('status', true);

        Banner::create($data);

        return redirect()
            ->route('admin.banners.index')
            ->with('success', 'Banner created successfully.');
    }

    public function edit(Banner $banner)
    {
        return view('backend.banners.form', [
            'banner' => $banner,
            'mode'   => 'edit',
        ]);
    }

    public function update(Request $request, Banner $banner)
    {
        $data = $request->validate([
            'title'       => ['nullable', 'string', 'max:191'],
            'image'       => ['nullable', 'image', 'max:4096'],
            'button_text' => ['nullable', 'string', 'max:100'],
            'button_url'  => ['nullable', 'string', 'max:255'],
            'sort_order'  => ['nullable', 'integer', 'min:0'],
            'status'      => ['nullable', 'boolean'],
        ]);

        // image update হলে পুরনোটা delete
        if ($request->hasFile('image')) {
            if ($banner->image) {
                Storage::disk('public')->delete($banner->image);
            }
            $data['image'] = $request->file('image')->store('banners', 'public');
        }

        $data['sort_order'] = $data['sort_order'] ?? 0;
        $data['status']     = $request->boolean('status', false);

        $banner->update($data);

        return redirect()
            ->route('admin.banners.edit', $banner)
            ->with('success', 'Banner updated successfully.');
    }

    public function destroy(Banner $banner)
    {
        if ($banner->image) {
            Storage::disk('public')->delete($banner->image);
        }

        $banner->delete();

        return redirect()
            ->route('admin.banners.index')
            ->with('success', 'Banner deleted successfully.');
    }

    /** এক ক্লিকে active/inactive toggle */
    public function toggleStatus(Banner $banner)
    {
        $banner->status = ! $banner->status;
        $banner->save();

        return back()->with('success', 'Banner status updated.');
    }
}
