<?php

namespace Modules\Product\Controllers\Admin;

/** Ajax Request */

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Maatwebsite\Excel\Facades\Excel;
use Modules\Canteen\Services\CanteenService;
use Modules\Category\Services\CategoryService;
use Modules\Product\Enums\ProductStockStatusEnum;
use Modules\Product\Enums\ReportEnum;
use Modules\Product\Requests\Admin\ProductStockRequest;
use Modules\Product\Requests\Admin\ReportRequest;
use Modules\Product\Services\ProductService;

class ProductStockController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/stocks';
    /** View folder */
    private $viewDirectory= 'admin.stocks.';
    //

    public function __construct(
        private ProductService $productService,
        private CanteenService $canteenService,
        private CategoryService $categoryService
    ){}

    /**
     * Display a listing of the products.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('stocks.head');
        $statuses = ProductStockStatusEnum::values();
        $canteens = $this->canteenService->get();
        $categories = $this->categoryService->get();
        return view($this->viewDirectory.'index', get_defined_vars());
    }

    /**
     * return data of the products.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $request->merge(['paginate'=>true]);
        $stocks = $this->productService->getStock($request->all());

        return view($this->viewDirectory.'grid',get_defined_vars());
    }

    public function show($id)
    {
        $head = __('stocks.show');
        $stock = $this->productService->getStockItem($id);
        if (!$stock)
        {
            abort(404);
        }

        return view($this->viewDirectory.'show',get_defined_vars());
    }

    public function fetchCategories(Request $request)
    {
        $categories = $this->categoryService->get($request->all());
        return view($this->viewDirectory.'categories',get_defined_vars());
    }

    public function fetchProducts(Request $request)
    {
        $products = $this->productService->get($request->all());
        return view($this->viewDirectory.'products',get_defined_vars());
    }

    public function create(Request $request)
    {
        $head = __('stocks.new');
        $action = route('stocks.store');
        $statuses = ProductStockStatusEnum::values();
        $canteens = $this->canteenService->get();
        return view($this->viewDirectory.'form',get_defined_vars());
    }

    public function store(ProductStockRequest $request)
    {
        $data = $request->validated();
        $this->productService->createStock($data);
        $product = $this->productService->getItem($request->product_id);
        if ($request->status == ProductStockStatusEnum::MINUS->value)
        {
            $count = $product->count - $request->count;
        }
        else
        {
            $count = $product->count + $request->count;
        }
        $this->productService->updateMain(['count' => $count], $request->product_id);
        session()->flash('message', __('dashboard.saveDone'));
        $response =
        [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('stocks.index'),
        ];
        return ResponseService::response($response);
    }

    public function report(Request $request) :View
    {
        $head = __('stocks.report');
        $canteens = $this->canteenService->get();
        $statuses = ReportEnum::values();
        $action = route('stocks.reporting');
        return view($this->viewDirectory.'report',get_defined_vars());
    }

    public function reporting(ReportRequest $request)
    {
        $data = $request->validated();
        $response = $this->productService->report($data);
        return Excel::download($response['reportClass'], $response['name'].'.xlsx');

    }
}
