<?php

namespace Wave\Http\Controllers\CMS;

use App\Http\Controllers\Controller;
use App\Models\UserNotification;

class DashboardController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application cms dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('theme::cms.index');
    }

    public function bookkeeping()
    {
        return view('theme::cms.bookkeeping');
    }

    public function company_secretary()
    {
        return view('theme::cms.company-secretary');
    }

    public function getNotifications()
    {
        $offset = request('offset') ? request('offset') : 0;
        $limit = request('limit') ? request('limit') : 5;
        $hasNew = UserNotification::where('is_admin', true)->where('is_read', false)->exists();

        $notifications = UserNotification::where('is_admin', true)
            ->latest()
            ->offset($offset)
            ->limit($limit)
            ->get()
            ->map(function ($item) {
                $item->category_url = $item->category_url;
                $item->url = $item->url;
                $item->image_url = $item->image_url;

                return $item;
            });
        
        UserNotification::where('is_admin', true)
            ->latest()
            ->offset($offset)
            ->limit($limit)
            ->update(['is_read' => true]);
        
        return response()->json([
            'status' => true,
            'notifications' => $notifications,
            'has_new' => $hasNew,
        ]);
    }
}
