<?php

namespace App\Http\Controllers\ticket;

use App\Models\Ticket;
use App\Helpers\Helper;
use App\Models\Comment;
use App\Models\Notification;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class TicketController extends Controller
{
    use Helper;

    public function __construct()
    {
        $this->model = new Ticket();
    }

    public function index()
    {


        try {
            $keyword = request()->input('keyword');
            $date_from = request()->input('date_from');
            $date_to = request()->input('date_to');
            $user = auth()->user();

            $data = $this->model->with('users:id,name')->orderBy('id', 'DESC')
                ->when(!$user->is_superadmin, function ($query) use ($user) {
                    $query->where('user_id', $user->id);
                })
                ->when($keyword, function ($query) use ($keyword) {
                    $query->where('title', 'Like', "%$keyword%");
                })
                ->when($date_from, function ($query) use ($date_from) {
                    $query->whereDate('created_at', '>=', $date_from);
                })
                ->when($date_to, function ($query) use ($date_to) {
                    $query->whereDate('created_at', '<=', $date_to);
                })
                ->paginate(request()->input('perPage'));

            return returnData(2000, $data);
        } catch (\Exception $exception) {
            return returnData(5000, $exception->getMessage(), $exception->getMessage());
        }
    }

    public function create()
    {
        //
    }

    public function store(Request $request)
    {

        try {
            $validate = $this->model->validate($request->all());

            if ($validate->fails()) {
                return returnData(3000, $validate->errors(), 'Validation Failed');
            }

            DB::beginTransaction();

            $this->model->fill($request->all());
            $this->model->user_id = auth()->user()->id;
            $this->model->ticket_id = rand(1000, 9999);
            $this->model->save();

            $notifications = [];
            $users = DB::table('users')->where('is_superadmin', 1)->get();

            foreach ($users as $user) {
                $notifications[] = [
                    'user_id' => auth()->user()->id,
                    'send_to' => $user->id,
                    'ticket_id' => $this->model->id,
                    'title' => getData(auth()->user()->id, 'name') . ' Added a ticket',
                    'notification' => $this->model->title,
                    'created_at' => date('Y-m-d H:i:s'),
                    'link' => "/admin/ticket_details/" . $this->model->id,
                ];
            }

            Notification::insert($notifications);

            DB::commit();

            return returnData(2000, null, 'Successfully Inserted');
        } catch (\Exception $exception) {
            return returnData(5000, $exception->getMessage(), $exception->getMessage());
        }
    }

    public function show($id)
    {
        try {
            $ticket = Ticket::with('users:id,name')->find($id);
            if (!$ticket) {
                return returnData(3000, $validate->errors(), 'Ticket not found');
            }

            return returnData(2000, $ticket);
        } catch (\Exception $exception) {
            return returnData(5000, $exception->getMessage(), $exception->getMessage());
        }
    }

    public function edit($id) {}


    public function update(Request $request, $id) {}

    public function destroy($id)
    {
        $ticket = Ticket::find($id);

        if ($ticket) {
            $ticket->notifications()->delete();
            $ticket->comments()->delete();
            $ticket->delete();

            return returnData(2000, null, 'Successfully Deleted');
        }

        return returnData(2000, [], 'Not found');
    }
}
