<?php

namespace App\Modules\Settings\Onboarding\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Admin\Tenancy\Services\ModuleAccessService;
use App\Modules\Settings\Localization\Services\LanguageService;
use App\Modules\Settings\Onboarding\Requests\AckOnboardingModulesRequest;
use App\Modules\Settings\Onboarding\Requests\SaveOnboardingCompanyRequest;
use App\Modules\Settings\Onboarding\Requests\SaveOnboardingLocaleRequest;
use App\Modules\Settings\Onboarding\Requests\SaveOnboardingTimezoneRequest;
use App\Modules\Settings\Onboarding\Services\TenantOnboardingService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class OnboardingWizardController extends Controller
{
    public function show(Request $request, LanguageService $languages, ModuleAccessService $modules): View
    {
        $tenant = $request->user()?->tenant;
        abort_if($tenant === null, 404);

        return view('pages.onboarding.wizard', [
            'tenant' => $tenant,
            'languages' => $languages->allActive(),
            'moduleSlugs' => $modules->activeModuleSlugsFor($tenant),
            'timezones' => config('onboarding.timezones', []),
        ]);
    }

    public function storeCompany(SaveOnboardingCompanyRequest $request, TenantOnboardingService $onboarding): RedirectResponse
    {
        $tenant = $request->user()->tenant;
        abort_if($tenant === null, 404);
        $onboarding->saveCompanyProfile($tenant, $request->user(), $request->validated());

        return redirect()->route('onboarding.wizard')->with('status', __('onboarding.saved'));
    }

    public function storeLocale(SaveOnboardingLocaleRequest $request, TenantOnboardingService $onboarding): RedirectResponse
    {
        $tenant = $request->user()->tenant;
        abort_if($tenant === null, 404);
        $onboarding->saveLocale($tenant, $request->user(), (string) $request->validated('locale'));

        return redirect()->route('onboarding.wizard')->with('status', __('onboarding.saved'));
    }

    public function storeTimezone(SaveOnboardingTimezoneRequest $request, TenantOnboardingService $onboarding): RedirectResponse
    {
        $tenant = $request->user()->tenant;
        abort_if($tenant === null, 404);
        $onboarding->saveTimezone($tenant, $request->user(), (string) $request->validated('timezone'));

        return redirect()->route('onboarding.wizard')->with('status', __('onboarding.saved'));
    }

    public function storeModulesAck(AckOnboardingModulesRequest $request, TenantOnboardingService $onboarding): RedirectResponse
    {
        $tenant = $request->user()->tenant;
        abort_if($tenant === null, 404);
        $onboarding->acknowledgeModulePreview($tenant, $request->user());

        return redirect()->route('onboarding.wizard')->with('status', __('onboarding.saved'));
    }

    public function complete(Request $request, TenantOnboardingService $onboarding): RedirectResponse
    {
        $tenant = $request->user()->tenant;
        abort_if($tenant === null, 404);
        $onboarding->complete($tenant, $request->user());

        return redirect()->route('dashboard.index')->with('status', __('onboarding.completed'));
    }
}
