<?php

namespace App\Http\Controllers\V1\Supplier;

use App\Http\Controllers\Controller;
use App\Models\Supplier;
use Carbon\Carbon;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class SupplierController extends Controller
{
    function createSupplier(Request $request): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
                'name' => 'required|string',
                'email' => 'required|email|string|unique:suppliers,email',
                'address' => 'required|string',
                'mobileNo' => 'required|string|unique:suppliers,mobileNo',
                'telephoneNo' => 'string|nullable',
                'pocName' => 'string|nullable',
                'description' => 'string|nullable',
            ]);
            if ($validator->fails()) {
                return $this->sendError('Validation Error.', $validator->errors());
            }
            $Supplier = new Supplier;
            $Supplier->name = $request->name;
            $Supplier->email = $request->email;
            $Supplier->address = $request->address;
            $Supplier->mobileNo = $request->mobileNo;
            $Supplier->telephoneNo = $request->telephoneNo;
            $Supplier->pocName = $request->pocName;
            $Supplier->description = $request->description;
            $Supplier->referenceNo = Carbon::now()->timestamp;
            $Supplier->save();

            return $this->sendResponse([], 'Added supplier successfully');
        } catch (Exception $e) {
            return $this->sendError('$e->getMessage()', $e->getTrace(), 500);
        }
    }

    function updateSupplier(Request $request, $id): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
                'name' => 'string',
                'email' => 'email|string|unique:suppliers,email',
                'address' => 'string',
                'mobileNo' => 'string|unique:suppliers,mobileNo',
                'telephoneNo' => 'string|nullable',
                'pocName' => 'string|nullable',
                'description' => 'string|nullable',
            ]);
            if ($validator->fails()) {
                return $this->sendError('Validation Error.', $validator->errors());
            }
            $Supplier = Supplier::find($id);
            if (!is_null($Supplier)) {
                if ($request->has('name')) {
                    $Supplier->name = $request->name;
                }
                if ($request->has('email')) {
                    $Supplier->email = $request->email;
                }
                if ($request->has('address')) {
                    $Supplier->address = $request->address;
                }
                if ($request->has('mobileNo')) {
                    $Supplier->mobileNo = $request->mobileNo;
                }
                if ($request->has('telephoneNo')) {
                    $Supplier->telephoneNo = $request->telephoneNo;
                }
                if ($request->has('pocName')) {
                    $Supplier->pocName = $request->pocName;
                }
                if ($request->has('description')) {
                    $Supplier->description = $request->description;
                }
                $Supplier->update();
            } else {
                return $this->sendResponse([], 'No Supplier found', false);
            }

            return $this->sendResponse([], 'Added supplier successfully');
        } catch (Exception $e) {
            return $this->sendError('$e->getMessage()', $e->getTrace(), 500);
        }
    }

    function getAllSuppliers(Request $request): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
                'pageNo' => 'numeric',
                'limit' => 'numeric',
            ]);
            if ($validator->fails()) {
                return $this->sendError('Validation Error.', $validator->errors());
            }
            $query = Supplier::query();
            $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);
            }
            $data = $query->get();
            if (count($data) > 0) {
                $response['data'] = $data;
                $response['count'] = $count;
                return $this->sendResponse($response, 'Data Fetched Successfully', true);
            } else {
                return $this->sendResponse('No Data Available', [], false);
            }

        } catch (Exception $e) {
            return $this->sendError('$e->getMessage()', $e->getTrace(), 500);
        }
    }


    function deleteSupplier(Request $request, $id): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
            ]);
            if ($validator->fails()) {
                return $this->sendError('Validation Error.', $validator->errors());
            }
            $Supplier = Supplier::find($id);
            if (!is_null($Supplier)) {
                if ($Supplier->delete()) {
                    return $this->sendResponse([], 'Supplier Deleted Successfully', true);
                } else {
                    return $this->sendResponse([], 'Supplier Deletion Failed', false);
                }
            } else {
                return $this->sendResponse([], 'No Supplier Found', false);
            }
        } catch (Exception $e) {
            return $this->sendError('$e->getMessage()', $e->getTrace(), 500);
        }
    }

}
