<?php

namespace App\Http\Controllers;

use App\Models\Certificate;
use Illuminate\Http\Request;

class CertificateController extends Controller
{
    public function index()
    {
        $certificates = Certificate::all();
        return view('admin.certificate.index', compact('certificates'));
    }

    public function create()
    {
        return view('admin.certificate.form');
    }
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            'description' => 'nullable|string',
            'link' => 'nullable|url',
            'type' => 'required|string|max:255',
            'date' => 'nullable|date',
            'renew' => 'nullable|integer',
        ]);

        $data = $request->all();

        if ($request->hasFile('image')) {
            $imageName = time() . '.' . $request->image->extension();
            $request->image->move(public_path('uploads/certificates'), $imageName);
            $data['image'] = 'uploads/certificates/' . $imageName;
        }

        Certificate::create($data);

        return redirect()->route('certificate.index')->with('success', 'Certificate created successfully.');
    }

    public function edit(Certificate $certificate)
    {
        return view('admin.certificate.form', compact('certificate'));
    }

    public function update(Request $request, Certificate $certificate)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'image' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            'description' => 'nullable|string',
            'link' => 'nullable|url',
            'type' => 'required|string|max:255',
            'date' => 'nullable|date',
            'renew' => 'nullable|integer',
        ]);

        $data = $request->all();

        if ($request->hasFile('image')) {
            // Delete old image if exists
            if ($certificate->image && file_exists(public_path($certificate->image))) {
                unlink(public_path($certificate->image));
            }
            $imageName = time() . '.' . $request->image->extension();
            $request->image->move(public_path('uploads/certificates'), $imageName);
            $data['image'] = 'uploads/certificates/' . $imageName;
        } else {
            unset($data['image']); // Prevent overwriting the image field if no new image is uploaded
        }

        $certificate->update($data);

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

    public function destroy(Certificate $certificate)
    {
        // Delete the image file
        if ($certificate->image && file_exists(public_path($certificate->image))) {
            unlink(public_path($certificate->image));
        }
        $certificate->delete();
        return redirect()->route('certificate.index')->with('success', 'Certificate deleted successfully.');
    }
}
