<?php

namespace App\Http\Controllers\Api\cms;

use Illuminate\Http\Request;
use App\Models\cms\Document;
use Illuminate\Support\Facades\Storage;


class DocumentController extends Controller
{

    public function deleteDocument(Request $request)
    {
        $document = Document::find($request->id);
        if($document){
            // public_path(Storage::delete($document->attachment));
            if(file_exists(public_path($document->attachment))){
                // delete the file
                unlink(public_path($document->attachment));
            }
            $document->delete();
            return response()->json(['success' => 'Document deleted successfully']);
        } else {
            return response()->json(['error' => 'Document not found']);
        }
    }
}
