<?php

namespace App\Http\Controllers\V1\ExpenseTracker;

use App\Http\Controllers\Controller;
use App\Imports\ProductUpload;
use App\Models\Expenses;
use App\Models\Inventory;
use App\Models\InventoryLog;
use App\Models\InvtLocation;
use App\Models\ProductLocationMapping;
use App\Models\Products;
use App\Models\ClientType;
use App\Models\Store;
use App\Models\StoreLocation;
use App\Models\Tickets;
use App\Models\TicketStatus;
use App\Models\PaymentStatus;
use App\Models\IndirectTax;
use App\Models\PaymentMode;
use App\Models\TermsAndConditions;
use App\Models\ClientAddress;
use App\Models\Client;
use App\Models\Quotation;
use App\Models\QuotationDetails;
use App\Models\BankDetails;
use App\Models\QuotInvoice;
use App\Models\Quotpayment;
use App\Models\ProductTags;
use App\Models\ClientMappingTag;
use App\Models\QuotCharges;
use App\Models\QuotStatus;
use App\Models\QuotOc;
use App\Models\DocumentType;
use App\Models\DocumentSeries;
use App\Models\PurchaseOrder;
use App\Models\RequestForQuotation;
use App\Models\RFQ_Details;
use App\Models\RFQSupplier;
use App\Models\RFQSupllierMappingProduct;
use App\Models\RFQPayment;
use App\Models\User;
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 ExpenseTrackerMetadataController extends Controller
{
    public function addExpense(Request $request): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
                'by_user_id' => 'required|numeric|exists:users,id',
                'amount' => 'required|integer',
                'bill_photo' => 'required|mimes:jpeg,png,jpg,gif,svg',
            ]);
            if ($validator->fails()) {
                return $this->sendError('Validation Error.', $validator->errors());
            }
            $newExpense = new Expenses();
            $newExpense->by_user_id = $request->by_user_id;
            $newExpense->amount = $request->amount;
            if ($request->hasFile('bill_photo')) {
                $newExpense->bill_photo = $this->billPhoto($request->file('bill_photo'));
            }
            $newExpense->name = $request->name;
            $newExpense->save();
            return $this->sendResponse([], 'Expense added successfully', true);
        } catch (Exception $e) {
            return $this->sendError('Something went wrong', $e->getMessage(), 413);
        }
    }
    function billPhoto($image)
    {

        $image_name = $image->getClientOriginalName() . '-' . time() . '.' . $image->getClientOriginalExtension();
        $destinationPath = public_path('billphoto/');
        $image->move($destinationPath, $image_name);
        $imageURL = env('BILLPHOTO') . 'billphoto/' . $image_name;
        return $imageURL;
    }
    public function editExpense(Request $request): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
                'by_user_id' => 'numeric|exists:users,id',
                'amount' => 'integer',
                'bill_photo' => 'mimes:jpeg,png,jpg,gif,svg',
            ]);
            if ($validator->fails()) {
                return $this->sendError('Validation Error.', $validator->errors());
            }
            $editExpense = Expenses::query()->where('id', $request->id)->first();
            if ($request->has('by_user_id')) {
                $editExpense->by_user_id = $request->by_user_id;
            }
            if ($request->has('amount')) {
                $editExpense->amount = $request->amount;
            }
            if ($request->has('name')) {
                $editExpense->name = $request->name;
            }
            if ($request->hasFile('bill_photo')) {
                $editExpense->bill_photo = $this->billPhoto($request->file('bill_photo'));
            }
            $editExpense->save();
            return $this->sendResponse([], 'Expense updated successfully');
        } catch (Exception $e) {
            return $this->sendError('Something Went Wrong', $e->getMessage(), 413);
        }
    }
    public function getAllExpense(Request $request): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
                'limit' => 'numeric',
                'pageNo' => 'numeric',
            ]);
            if ($validator->fails()) {
                return $this->sendError('Validation Error.', $validator->errors());
            }
            $query = Expenses::query()->with(['by_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);
            }
            $getExpenses = $query->orderBy('id', 'DESC')->get();
            if (count($getExpenses) > 0) {
                return $this->sendResponse(["expenses" => $getExpenses, 'count' => $count], 'Data fetch successfully');
            } else {
                return $this->sendResponse([], 'No Expenses are available', false);
            }
        } catch (Exception $e) {
            return $this->sendError('Something Went Wrong', $e->getMessage(), 413);
        }
    }
    public function deleteExpenseById(Request $request): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
                'id' => 'required|integer|exists:expenses,id',
            ]);
            if ($validator->fails()) {
                return $this->sendError('Validation Error.', $validator->errors());
            }
            $deleteExpense = Expenses::query()->where('id', $request->id)->first();
            $deleteExpense->delete();
            return $this->sendResponse([], 'Expense deleted successfully', true);
        } catch (Exception $e) {
            return $this->sendError('Something went wrong', $e->getMessage(), 413);
        }
    }
    public function getExpenseById(Request $request): JsonResponse
    {
        try {
            $validator = Validator::make($request->all(), [
                'id' => 'required|integer|exists:expenses,id',
            ]);
            if ($validator->fails()) {
                return $this->sendError('Validation Error.', $validator->errors());
            }
            $getExpense = Expenses::query()->where('id', $request->id)->first();

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