<?php

namespace App\Http\Controllers;

use App\Models\About;
use App\Http\Requests\StoreAboutRequest;
use App\Http\Requests\UpdateAboutRequest;
use Illuminate\Support\Facades\Log;

class AboutController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $about = About::first();
        return view('admin.pages.about', compact('about'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('admin.about.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreAboutRequest $request)
    {
        $data = $request->validated();

        $data['main_image'] = $this->uploadImage($request, 'main_image');
        $data['home_image'] = $this->uploadImage($request, 'home_image');

        About::create($data);
        return redirect()->route('abouts.index')->with('success', 'About created successfully.');
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(About $about)
    {
        return view('admin.about.edit', compact('about'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateAboutRequest $request, About $about)
    {
        $data = $request->validated();

        $data['main_image'] = $this->uploadImage($request, 'main_image', $about->main_image);
        $data['home_image'] = $this->uploadImage($request, 'home_image', $about->home_image);

        $about->update(array_filter($data));

        return redirect()->route('abouts.index')->with('success', 'About updated successfully.');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(About $about)
    {
        $this->deleteImage($about->main_image);
        $this->deleteImage($about->home_image);

        $about->delete();

        return redirect()->route('abouts.index')->with('success', 'About deleted successfully.');
    }

    /**
     * Handle image upload.
     */
    private function uploadImage($request, $fieldName, $oldImage = null)
    {
        if ($request->hasFile($fieldName)) {
            $this->deleteImage($oldImage);

            $file = $request->file($fieldName);
            $filename = time() . '_' . $file->getClientOriginalName();
            $file->move(public_path('uploads/abouts'), $filename);

            return $filename;
        }
        return $oldImage;
    }

    /**
     * Delete image if exists.
     */
    private function deleteImage($image)
    {
        if ($image) {
            $imagePath = public_path('uploads/abouts/' . $image);
            if (file_exists($imagePath)) {
                unlink($imagePath);
                Log::info('Image deleted successfully', ['imagePath' => $imagePath]);
            } else {
                Log::warning('File does not exist', ['imagePath' => $imagePath]);
            }
        }
    }
}