<?php

namespace App\Http\Controllers\API\v1\Nominee;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Util;
use App\Nominee;

class DeleteController extends Controller
{
    public function __invoke (Request $request, $id) 
    {
		$nominee = Nominee::findOrFail($id);
    	Util::deleteImage($nominee->image);
    	$nominee->delete();

        activity()
            ->withProperties([
                'nominee_id' => $nominee->id,
                'nominee_name' => $nominee->name,
                'election_id' => $nominee->election_id,
                'deleted_by_admin_id' => $request->user() ? (int) $request->user()->id : null,
                'ip' => $request->ip(),
                'user_agent' => $request->userAgent(),
            ])
            ->log("delete nominee: {$nominee->name}");

    	return response()->json([
    		'status' => 'success',
    		'message'=> 'Nominee deleted successfully'
    	]);
    }
}
