<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Offer;

class OfferController extends Controller
{
    public function index()
    {
        $offers = Offer::orderBy('id', 'asc')->get()->map(function ($offer) {
            return [
                'id' => $offer->id,
                'title' => $offer->title,
                'sub_title' => $offer->sub_title,
                'image' => getObjUrl('offers', $offer->image),
                'content' => $offer->content,
                'created_at' => $offer->created_at->format('d/m/y, H:i'),
            ];
        });
        return sendSuccessResponse($offers, 'Offers fetched successfully');
    }

    public function show($id)
    {
        $offer = Offer::find($id);
        $offer = [
            'id' => $offer->id,
            'title' => $offer->title,
            'sub_title' => $offer->sub_title,
            'image' => getObjUrl('offers', $offer->image),
            'content' => $offer->content,
            'created_at' => $offer->created_at->format('d/m/y, H:i'),
            'next_offers' => Offer::where('id', '>', $offer->id)
                ->orderBy('id', 'asc')
                ->limit(3)
                ->get()
                ->map(function ($nextOffer) {
                    return [
                        'id' => $nextOffer->id,
                        'title' => $nextOffer->title,
                        'date' => $nextOffer->created_at->format('d/m/y, H:i')
                    ];
                })
        ];
        return sendSuccessResponse($offer, 'Offer fetched successfully');
    }

    public function offers()
    {
        $offers = Offer::orderBy('id', 'desc')->paginate(10);
        return view('Backend.Offers.offers', ['offers' => $offers]);
    }

    public function create()
    {
        return view('Backend.Offers.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'sub_title' => 'required|string|max:255',
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg,webp|max:10240',
            'content' => 'required',
        ]);

        try {
            if ($request->hasFile('image')) {
                $image = uploadImageR2($request->file('image'), 'offers');
            }
            $offer = new Offer();
            $offer->title = $request->title;
            $offer->sub_title = $request->sub_title;
            $offer->image = $image;
            $offer->content = $request->content;
            $offer->save();
            toastr()->success('Offer created successfully');
            return redirect()->route('admin.offers');
        } catch (\Exception $e) {
            toastr()->error('Failed to create offer');
            return redirect()->route('admin.offers');
        }
    }

    public function destroy($id)
    {
        try {
            $offer = Offer::find($id);
            if ($offer) {
                $offer->delete();
                toastr()->success('Offer deleted successfully');
                return redirect()->route('admin.offers');
            } else {
                toastr()->error('Offer not found');
                return redirect()->route('admin.offers');
            }
        } catch (\Exception $e) {
            toastr()->error('Failed to delete offer');
            return redirect()->route('admin.offers');
        }
    }

    public function edit($id)
    {
        $offer = Offer::findOrFail($id);
        return view('Backend.Offers.edit', compact('offer'));
    }

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

        try {
            $offer = Offer::findOrFail($id);

            if ($request->hasFile('image')) {
                $image = uploadImageR2($request->file('image'), 'offers');
                $offer->image = $image;
            }

            $offer->title = $request->title;
            $offer->sub_title = $request->sub_title;
            $offer->content = $request->content;
            $offer->save();

            toastr()->success('Offer updated successfully');
            return redirect()->route('admin.offers');
        } catch (\Exception $e) {
            toastr()->error('Failed to update offer');
            return redirect()->route('admin.offers');
        }
    }
}
