<?php

namespace App\Http\Controllers\V1\RaiseTicket;

use App\Http\Controllers\Controller;
use App\Models\Tickets;
use App\Models\TicketStatus;
use App\SchemaMetadata;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use DB;
use Image;

class RaiseTicketMetadataController extends Controller
{
    public function addTicketStatus(Request $request): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
                'name' => 'required|string|unique:ticket_status',
            ]);
            if ($validator->fails()) {
                return $this->sendError('Validation Error.', $validator->errors());
            }
            $newTicketStatus = new TicketStatus();
            $newTicketStatus->name = $request->name;
            $newTicketStatus->save();
            return $this->sendResponse([], 'Ticket status added successfully', true);
        } catch (Exception $e) {
            return $this->sendError('Something went wrong', $e->getTrace(), 413);
        }
    }

    public function editTicketStatus(Request $request): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
                'id' => 'required|integer|exists:ticket_status,id',
                'name' => 'string|unique:ticket_status',
            ]);
            if ($validator->fails()) {
                return $this->sendError('Validation Error.', $validator->errors());
            }
            $editTicketStatus = TicketStatus::query()->where('id', $request->id)->first();
            if ($request->has('name')) {
                $editTicketStatus->name = $request->name;
            }
            $editTicketStatus->save();
            return $this->sendResponse([], 'Ticket status updated successfully');
        } catch (Exception $e) {
            return $this->sendError('Something Went Wrong', $e->getMessage(), 413);
        }
    }
    public function getAllTicketStatus(Request $request): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
                'limit' => 'numeric',
                'pageNo' => 'numeric',
            ]);
            if ($validator->fails()) {
                return $this->sendError('Validation Error.', $validator->errors());
            }
            $query = TicketStatus::query();
            if ($request->has('pageNo') && $request->has('limit')) {
                $limit = $request->limit;
                $pageNo = $request->pageNo;
                $skip = $limit * $pageNo;
                $query = $query->skip($skip)->limit($limit);
            }
            $count = $query->count();
            $getTicketStatus = $query->orderBy('id', 'DESC')->get();
            if (count($getTicketStatus) > 0) {
                return $this->sendResponse(["ticket_status" => $getTicketStatus, 'count' => $count], 'Data fetch successfully');
            } else {
                return $this->sendResponse([], 'No ticket status are available', false);
            }
        } catch (Exception $e) {
            return $this->sendError('Something Went Wrong', $e->getMessage(), 413);
        }
    }
    public function deleteTicketStatusById(Request $request): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
                'id' => 'required|integer|exists:ticket_status,id',
            ]);
            if ($validator->fails()) {
                return $this->sendError('Validation Error.', $validator->errors());
            }
            $deleteTicketStatus = TicketStatus::query()->where('id', $request->id)->first();
            $deleteTicketStatus->delete();
            return $this->sendResponse([], 'Ticket status deleted successfully', true);
        } catch (Exception $e) {
            return $this->sendError('Something went wrong', $e->getMessage(), 413);
        }
    }
    public function getTicketStatusById(Request $request): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
                'id' => 'required|integer|exists:ticket_status,id',
            ]);
            if ($validator->fails()) {
                return $this->sendError('Validation Error.', $validator->errors());
            }
            $getTicketStatus = TicketStatus::query()->where('id', $request->id)->first();
            return $this->sendResponse($getTicketStatus, 'Data fetch successfully', true);
        } catch (Exception $e) {
            return $this->sendError('Something Went Wrong', $e->getMessage(), 413);
        }
    }
    public function addTicket(Request $request): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
                'customer_name' => 'required|string',
                'description' => 'required|string',
                'ticket_status_id' => 'required|numeric|exists:ticket_status,id',
                'assign_to_user_id' => 'required|numeric|exists:users,id',
                'customer_mobile_no' => 'required|digits:10',
                'customer_email' => 'required|string|email|unique:ticket',
            ]);
            if ($validator->fails()) {
                return $this->sendError('Validation Error.', $validator->errors());
            }
            $newTickets = new Tickets();
            $newTickets->customer_name = $request->customer_name;
            $newTickets->description = $request->description;
            $newTickets->ticket_status_id = $request->ticket_status_id;
            $newTickets->assign_to_user_id = $request->assign_to_user_id;
            $newTickets->customer_mobile_no = $request->customer_mobile_no;
            $newTickets->customer_email = $request->customer_email;
            $newTickets->save();
            return $this->sendResponse([], 'Ticket added successfully', true);
        } catch (Exception $e) {
            return $this->sendError('Something went wrong', $e->getMessage(), 413);
        }
    }
    public function editTicket(Request $request): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
                'customer_name' => 'required|string',
                'description' => 'required|string',
                'ticket_status_id' => 'required|numeric|exists:ticket_status,id',
                'assign_to_user_id' => 'required|numeric|exists:users,id',
                'customer_mobile_no' => 'required|digits:10',
                'customer_email' => 'required|string|email|unique:ticket',
            ]);
            if ($validator->fails()) {
                return $this->sendError('Validation Error.', $validator->errors());
            }
            $editTickets = Tickets::query()->where('id', $request->id)->first();

            if ($request->has('customer_name')) {
                $editTickets->customer_name = $request->customer_name;
            }
            if ($request->has('description')) {
                $editTickets->description = $request->description;
            }
            if ($request->has('ticket_status_id')) {
                $editTickets->ticket_status_id = $request->ticket_status_id;
            }
            if ($request->has('assign_to_user_id')) {
                $editTickets->assign_to_user_id = $request->assign_to_user_id;
            }
            if ($request->has('customer_mobile_no')) {
                $editTickets->customer_mobile_no = $request->customer_mobile_no;
            }
            if ($request->has('customer_email')) {
                $editTickets->customer_email = $request->customer_email;
            }
            $editTickets->save();
            return $this->sendResponse([], 'Ticket updated successfully');
        } catch (Exception $e) {
            return $this->sendError('Something Went Wrong', $e->getMessage(), 413);
        }
    }
    public function getAllTicket(Request $request): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
                'limit' => 'numeric',
                'pageNo' => 'numeric',
            ]);
            if ($validator->fails()) {
                return $this->sendError('Validation Error.', $validator->errors());
            }
            $query = Tickets::query()->with(['ticket_status', 'assign_to_user']);
            $count = $query->count();
            if ($request->has('pageNo') && $request->has('limit')) {
                $limit = $request->limit;
                $pageNo = $request->pageNo;
                $skip = $limit * $pageNo;
                $query = $query->skip($skip)->limit($limit);
            }

            $getTickets = $query->orderBy('id', 'DESC')->get();
            if (count($getTickets) > 0) {
                return $this->sendResponse(["tickets" => $getTickets, 'count' => $count], 'Data fetch successfully');
            } else {
                return $this->sendResponse([], 'No Tickets are available', false);
            }
        } catch (Exception $e) {
            return $this->sendError('Something Went Wrong', $e->getMessage(), 413);
        }
    }
    public function deleteTicketById(Request $request): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
                'id' => 'required|integer|exists:ticket,id',
            ]);
            if ($validator->fails()) {
                return $this->sendError('Validation Error.', $validator->errors());
            }
            $deleteTickets = Tickets::query()->where('id', $request->id)->first();
            $deleteTickets->delete();
            return $this->sendResponse([], 'Tickets deleted successfully', true);
        } catch (Exception $e) {
            return $this->sendError('Something went wrong', $e->getMessage(), 413);
        }
    }
    public function getTicketById(Request $request): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
                'id' => 'required|integer|exists:ticket,id',
            ]);
            if ($validator->fails()) {
                return $this->sendError('Validation Error.', $validator->errors());
            }
            $getTickets = Tickets::query()->where('id', $request->id)->first();

            return $this->sendResponse($getTickets, 'Data fetch successfully', true);
        } catch (Exception $e) {
            return $this->sendError('Something Went Wrong', $e->getMessage(), 413);
        }
    }
}
