<?php

namespace App\Http\Controllers;

use App\Helpers\Helper;
use App\Models\Masking;
use App\Models\TicketCategory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MaskingController extends Controller
{
    use Helper;

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

    public function index()
    {
        if (!can('ticket_category_view')) {
            return $this->notPermitted();
        }

        try {
            $user = auth()->user();
            $user_id = input('user_id');
            $data = $this->model
                ->with('user')
                ->when(input('keyword'), function ($query) {
                    $keyword = input('keyword');
                    $query->where('name', 'Like', "%$keyword%");
                })
                ->when($user_id, function (Builder $query) use ($user_id) {
                    $query->where('user_id', $user_id);
                })
                ->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)
    {
        if (!can('ticket_category_add')) {
            return $this->notPermitted();
        }


        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->save();

            DB::commit();

            return returnData(2000, null, 'Successfully Inserted');

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

    }

    public function show($id)
    {

    }

    public function edit($id)
    {

    }


    public function update(Request $request, $id)
    {
        if (!can('ticket_category_update')) {
            return $this->notPermitted();
        }
        try {
            $validate = $this->model->validate($request->all());

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

            DB::beginTransaction();

            $data = $this->model->where('id', $request->input('id'))->first();

            if($data){
                $data->fill($request->all());
                $data->save();

                DB::commit();

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

            return returnData(5000, null, 'Information Not found');

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

    public function destroy($id)
    {
        if (!can('ticket_category_delete')) {
            return $this->notPermitted();
        }
        $data = $this->model->where('id', $id)->first();

        if($data){
            $data->delete();

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

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