<?php

namespace Modules\Setting\Controllers\Admin;

/** Ajax Request */

use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Modules\Setting\Enums\SettingOnHandCostValidEnum;
use Modules\Setting\Enums\SettingTaxValidEnum;
use Modules\Setting\Requests\Admin\ModuleRequest;
use Modules\Setting\Requests\Admin\SettingRequest;
use Modules\Setting\Services\SettingService;
use Modules\User\Services\ModuleService;

class SettingController extends Controller
{

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

    public function __construct(SettingService $settingService,ModuleService $moduleService)
    {
        $this->settingService = $settingService;
        $this->moduleService = $moduleService;
    }

    /**
     * Display a listing of the settings.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('settings.head');
        $head = metaFields('settings', 'add_new', getCurrentLocale()) ?? __('settings.new');

        $settingsAction= route('settings.store');
        $modulesAction= route('settings.Modules');
        $settings = $this->settingService->get($request);
        $modules = $this->moduleService->get($request);

        $tax_valid_statuses = SettingTaxValidEnum::values();
        $on_hand_cost_valid_statuses = SettingOnHandCostValidEnum::values();

        if (count($settings))
        {
            $head = metaFields('settings', 'edit', getCurrentLocale()) ?? __('settings.edit');
        }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param SettingRequest $settingRequest
     * @return RedirectResponse|Redirector
     */
    public function store(SettingRequest $settingRequest) :RedirectResponse
    {
        $this->settingService->save($settingRequest);
        return redirect($this->redirectSuccessPath)->with('message', __('dashboard.saveDone'));
    }

    /**
     * Store Modules metas in Modules table.
     *
     * @param ModuleRequest $moduleRequest
     * @return RedirectResponse|Redirector
     */
    public function modules(ModuleRequest $moduleRequest) :RedirectResponse
    {

        $this->settingService->modules($moduleRequest);

        return redirect($this->redirectSuccessPath)->with('message', __('dashboard.saveDone'));
    }
}
