<?php

namespace App\Http\Controllers\SM;

use App\Http\Controllers\Controller;
use App\Services\SM\FunctionService;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class FunctionController extends Controller
{
    /**
     * Display a listing of the resource.
     */

    public function __construct(private FunctionService $functionService)
    {
    }
    public function index()
    {
        return $this->functionService->dataTable();
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        try {
            DB::beginTransaction();
            $fnction = $this->functionService->create($request->all());
            DB::commit();
            return response()->json(['success' => true,'message' => 'Function created ','fnction' => $fnction]);
        } catch (Exception $e) {
            DB::rollBack();
            return response()->json(['success' => false,'message' => $e->getMessage()],500);
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        return response()->json(['success' => true,'function' => $this->functionService->detail($id)]);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        try {
            DB::beginTransaction();
            $fnction = $this->functionService->update($id,$request->all());
            DB::commit();
            return response()->json(['success' => true,'message' => 'Function updated ','fnction' => $fnction]);
        } catch (Exception $e) {
            DB::rollBack();
            return response()->json(['success' => false,'message' => $e->getMessage()],500);
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}
