<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use App\Models\AboutUs;

class AboutUsController extends Controller
{
    public function store(Request $request)
    {
        $request->validate([
            'heading' => 'required|string|max:255',
            'subheading' => 'required|string|max:255',
            'paragraph1' => 'required|string',
            'paragraph2' => 'required|string',
            'image' => 'required|image|mimes:jpeg,png,jpg,webp|max:2048|dimensions:min_width=600,min_height=786',
        ]);

        $imagePath = null;
        if ($request->hasFile('image')) {
            $imagePath = $request->file('image')->store('aboutus', 'public');
        }

        $aboutus = AboutUs::create([
            'heading' => $request->heading,
            'subheading' => $request->subheading,
            'paragraph1' => $request->paragraph1,
            'paragraph2' => $request->paragraph2,
            'image' => $imagePath,
        ]);

        return redirect()
            ->route('about-us.index')
            ->with('success', 'About Us section created successfully.');
    }

    public function index()
    {
        $aboutusSection = AboutUs::latest()->get();

        return view('aboutus.index', compact('aboutusSection'));
    }

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

    public function show($id)
    {
        $aboutus = AboutUs::findOrFail($id);

        return view('aboutus.show', compact('aboutus'));
    }

    public function edit($id)
    {
        $aboutus = AboutUs::findOrFail($id);

        return view('aboutus.edit', compact('aboutus'));
    }

    public function destroy($id)
    {
        $aboutus = AboutUs::findOrFail($id);

        if (!empty($aboutus->image) && Storage::disk('public')->exists($aboutus->image)) {
            Storage::disk('public')->delete($aboutus->image);
        }

        $aboutus->delete();

        return redirect()
            ->route('about-us.index')
            ->with('success', 'About Us section deleted successfully.');
    }

    public function update(Request $request, $id)
    {
        $aboutus = AboutUs::findOrFail($id);

        $request->validate([
            'heading' => 'required|string|max:255',
            'subheading' => 'required|string|max:255',
            'paragraph1' => 'required|string',
            'paragraph2' => 'required|string',
            'image' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:2048|dimensions:min_width=600,min_height=786',
        ]);

        $imagePath = $aboutus->image;

        if ($request->hasFile('image')) {
            if (!empty($aboutus->image) && Storage::disk('public')->exists($aboutus->image)) {
                Storage::disk('public')->delete($aboutus->image);
            }

            $imagePath = $request->file('image')->store('aboutus', 'public');
        }

        $aboutus->update([
            'heading' => $request->heading,
            'subheading' => $request->subheading,
            'paragraph1' => $request->paragraph1,
            'paragraph2' => $request->paragraph2,
            'image' => $imagePath,
        ]);

        return redirect()
            ->route('about-us.index')
            ->with('success', 'About Us section updated successfully.');
    }
}
