<?php

namespace App\Services\SM;

use App\Models\SM\Application;
use App\Traits\DataTable;
use Exception;

class AppService
{
    /**
     * Create a new class instance.
     */

    use DataTable;
    public function __construct(private Application $model)
    {
        //
    }

    public function dataTable()
    {
        return $this->generate($this->model->query());
    }

    public function create($data)
    {
        return $this->model->create([
            'name' => $data['name'],
            'code' => strtoupper($data['code']),
            'base_url' => @$data['base_url']
        ]);
    }

    public function detail($id)
    {
        return $this->model->findOr($id, fn () => throw new Exception("Function not found"));
    }

    public function update($id,$data)
    {
        return $this->model->findOr($id,fn() => throw new Exception("App not found"))->update($data);
    }
}
