<?php

namespace App\Services\Admin;

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

class PageService extends BaseService
{
    public function pagesList()
    {
        $usersList = Page::with('parent');
        $datatable = Datatables::of($usersList);
        return collect($datatable->make(true)->getData());
    }

    public function getPageDetail($id)
    {
        return Page::findOrFail($id);
    }

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

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