<?php

namespace Modules\Product\Controllers\Canteen;

/** 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\Category\Services\CategoryService;
use Modules\Product\Enums\ProductStockStatusEnum;
use Modules\Product\Enums\ReportEnum;
use Modules\Product\Requests\Canteen\ProductStockRequest;
use Modules\Product\Requests\Canteen\ReportRequest;
use Modules\Product\Services\ProductService;

class ProductStockController extends Controller
{

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

    public function __construct(
        private ProductService $productService,
        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();
        $categories = $this->categoryService->get([
            'canteen_id'=>auth()->user()->canteen_id
        ]);
        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,'canteen_id'=>auth()->user()->canteen_id]);
        $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 || $stock->canteen_id != ( auth()->user()->canteen_id ?? false ) )
        {
            abort(404);
        }

        return view($this->viewDirectory.'show',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.create');
        $action = route('canteen.stocks.store');
        $statuses = ProductStockStatusEnum::values();
        $categories = $this->categoryService->get(['canteen_id'=>auth()->user()->canteen_id]);
        return view($this->viewDirectory.'form',get_defined_vars());
    }

    public function store(ProductStockRequest $request)
    {
        $data = $request->validated();
        $data['canteen_id'] = auth()->user()->canteen_id;
        $this->productService->createStock($data);

        session()->flash('message', __('dashboard.saveDone'));
        $response =
        [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('canteen.stocks.index'),
        ];
        return ResponseService::response($response);
    }

    public function report(Request $request) :View
    {
        $head = __('stocks.report');
        $categories = $this->categoryService->get(['canteen_id'=>auth()->user()->canteen_id]);
        $statuses = ReportEnum::values();
        $action = route('canteen.stocks.reporting');
        return view($this->viewDirectory.'report',get_defined_vars());
    }

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

    }
}
