<?php

namespace App\Http\Controllers;

use App\Models\StageAcademyBanner;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;

class StageAcademyBannerController extends Controller
{
    public function __construct()
    {
        $this->middleware('permission:stage_academy.banner.view')->only(['index']);
        $this->middleware('permission:stage_academy.banner.create')->only(['store']);
        $this->middleware('permission:stage_academy.banner.update')->only(['update']);
        $this->middleware('permission:stage_academy.banner.delete')->only(['destroy']);
    }

    public function index()
    {
        $banners = StageAcademyBanner::query()
            ->orderByDesc('id')
            ->get();

        return view('stage_academy.banner', compact('banners'));
    }

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name_ar' => ['required', 'string', 'max:255'],
            'name_en' => ['required', 'string', 'max:255'],
            'expiry_date' => ['required', 'date'],
            'image' => ['required', 'file', 'mimes:jpg,jpeg,png,webp'],
            'link' => ['nullable', 'url', 'max:2048'],
        ]);

        if ($validator->fails()) {
            return redirect()
                ->route('stage-academy.banner')
                ->withErrors($validator)
                ->withInput()
                ->with('open_banner_create_modal', true);
        }

        $validated = $validator->validated();
        $validated['image'] = $request->file('image')->store('stage-academy/banners', 'public');

        StageAcademyBanner::query()->create($validated);

        return redirect()->route('stage-academy.banner')->with('success', __('common.saved_successfully'));
    }

    public function update(Request $request, int $id)
    {
        $banner = StageAcademyBanner::query()->findOrFail($id);

        $validator = Validator::make($request->all(), [
            'name_ar' => ['required', 'string', 'max:255'],
            'name_en' => ['required', 'string', 'max:255'],
            'expiry_date' => ['required', 'date'],
            'image' => ['nullable', 'file', 'mimes:jpg,jpeg,png,webp'],
            'link' => ['nullable', 'url', 'max:2048'],
        ]);

        if ($validator->fails()) {
            return redirect()
                ->route('stage-academy.banner')
                ->withErrors($validator)
                ->withInput()
                ->with('open_banner_edit_id', $banner->id);
        }

        $validated = $validator->validated();

        if ($request->hasFile('image')) {
            if ($banner->image) {
                Storage::disk('public')->delete($banner->image);
            }

            $validated['image'] = $request->file('image')->store('stage-academy/banners', 'public');
        }

        $banner->update($validated);

        return redirect()->route('stage-academy.banner')->with('success', __('common.saved_successfully'));
    }

    public function destroy(int $id)
    {
        $banner = StageAcademyBanner::query()->findOrFail($id);

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

        $banner->delete();

        return redirect()->route('stage-academy.banner')->with('success', __('common.deleted_successfully'));
    }
}
