<?php

namespace App\Services\Admin;

use App\Models\Category;
use App\Services\BaseService;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Yajra\Datatables\Datatables;

class CategoryService extends BaseService
{
    public function categoriesList()
    {
        $usersList = Category::query();
        $datatable = Datatables::of($usersList);
        return collect($datatable->make(true)->getData());
    }

    public function getCategoryDetail($id)
    {
        return Category::findOrFail($id);
    }

    public function createCategory(array $data)
    {
        $attr = new Category();
        $attr->title = $data['title'] ?? null;
        $attr->slug = Str::slug($data['title'], '-');
        $attr->full_desc = $data['full_desc'] ?? null;
        $attr->meta_title = $data['title'] ?? null;
        $attr->meta_keywords = $data['title'] ?? null;
        $attr->meta_desc = $data['title'] ?? null;
        $attr->added_by = Auth::id();
        $attr->save();
    }

    public function updateCategory($data, $id)
    {
        $attr = Category::findOrFail($id);
        $attr->title = $data['title'] ?? null;
        $attr->slug = Str::slug($data['title'], '-');
        $attr->full_desc = $data['full_desc'] ?? null;
        $attr->meta_title = $data['meta_title'] ?? null;
        $attr->meta_keywords = $data['meta_keywords'] ?? null;
        $attr->meta_desc = $data['meta_desc'] ?? null;
        $attr->updated_by = Auth::id();
        $attr->save();
    }
}
