<?php

namespace App\Http\Controllers\Notification;

use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Models\ExpireNotification;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Modules\Notify\Entities\Notification;
use App\Helpers\CoreApp\Traits\ApiReturnFormatTrait;

class NotificationController extends Controller
{
    use ApiReturnFormatTrait;

    public function index(Request $request)
    {
        $data['title'] = _trans('common.Notification');

        $data["notifications"] = Notification::where(['receiver_id' => auth()->user()->id])
        ->where('company_id', userCompanies())
        ->where('branch_id', userBranch())
        ->when($request->search, fn ($query) => $query->where('message', 'like', '%' . $request->search . '%'))
        ->latest('id')
        ->paginate(50);

        return view('backend.notification.index', compact('data'));
    }

    //readNotification
    public function readNotification(Request $request)
    {
        try {
            $notification = auth()->user()->unreadNotifications->where('id', $request->id)->first();
            if ($notification->read_at == null) {
                $notification->markAsRead();
            }
            $action_url= $notification->data['actionURL']['web'];
            $data=[];
            $data['action_url'] = $action_url;
            $data['notification'] = $notification;
            return response()->json($data);
        } catch (\Throwable $th) {
            return response()->json(['error' => $th->getMessage()]);
        }
    }

    // expire notification
    public function expireIndex()
    {
        $data['title'] = _trans('common.Expire Notification');
        $data['notifications'] = ExpireNotification::with('user.designation','user.department')->where(['receiver_id' => auth()->user()->id, 'is_read' => 0])->get();
        return view('backend.notification.expire_notification', compact('data'));
    }


    public function readMyNotification($id){
        $notification = Notification::findOrFail($id);
        $notification->seen = 1 ;
        $notification->seen_at = Carbon::now();
        $notification->save();

        if($notification->web_redirect_url){
            return redirect($notification->web_redirect_url);
        }else{
            return redirect()->back();
        }
    }

    public function bulkSeen(Request $request)
    {
        try {
            Notification::where('company_id', userCompanies())
            ->where('branch_id', userBranch())
            ->whereIn('id', $request->ids)
            ->where('receiver_id', auth()->user()->id)
            ->where('seen', 0)
            ->update(["seen" => 1, "seen_at" => Carbon::now()]);

            return redirect()->back();
        } catch (\Throwable $th) {
            info($th->getMessage());
        }
    }

    public function delete(Request $request)
    {
        try {
            Notification::where('company_id', userCompanies())
            ->where('branch_id', userBranch())
            ->where('receiver_id', auth()->user()->id)
            ->whereIn('id', $request->ids)
            ->delete();

            return redirect()->back();
        } catch (\Throwable $th) {
            info($th->getMessage());
        }
    }
}
