<?php

namespace App\Http\Controllers\Admin\System;

use App\Enums\Permission;
use App\Http\Controllers\Concerns\FlashesEntityMessage;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\System\Setting\HomepageSettingRequest;
use App\Http\Requests\Admin\System\Setting\HrSettingRequest;
use App\Http\Requests\Admin\System\Setting\MarketplaceRequest;
use App\Http\Requests\Admin\System\Setting\SocialMediaRequest;
use App\Http\Requests\Admin\System\Setting\SystemRequest;
use App\Services\Manage\OwnerVerificationService;
use App\Services\System\Setting\HomepageSettingService;
use App\Services\System\Setting\HrSettingService;
use App\Services\System\Setting\MarketplaceService;
use App\Services\System\Setting\SocialMediaService;
use App\Services\System\Setting\SystemService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class SettingController extends Controller
{
    use FlashesEntityMessage;

    public function __construct(
        private readonly SystemService $systemService,
        private readonly SocialMediaService $socialMediaService,
        private readonly MarketplaceService $marketplaceService,
        private readonly HrSettingService $hrSettingService,
        private readonly HomepageSettingService $homepageSettingService,
        private readonly OwnerVerificationService $ownerVerificationService,
    ) {}

    public function index(): Response
    {
        return Inertia::render('admin/system/setting/Index', [
            'system' => $this->systemService->systemData(),
            'socialMedia' => $this->socialMediaService->socialMediaData(),
            'marketplace' => $this->marketplaceService->marketplaceData(),
            'hr' => $this->hrSettingService->hrData(),
            'homepage' => $this->homepageSettingService->homepageData(),
            'hasPendingMarketplaceVerification' => $this->ownerVerificationService->hasPendingMarketplaceVerification(),
        ]);
    }

    public function updateSystem(SystemRequest $request): RedirectResponse
    {
        if (! $request->user()->can(Permission::OWNER_VERIFICATIONS_VERIFY->value)) {
            abort(403, 'Aksi ini tidak diizinkan.');
        }

        $this->systemService->updateSystem($request->validated());

        $this->flashSuccess('Pengaturan sistem berhasil disimpan.');

        return redirect()->route('admin.system.settings.index');
    }

    public function updateSocialMedia(SocialMediaRequest $request): RedirectResponse
    {
        if (! $request->user()->can(Permission::OWNER_VERIFICATIONS_VERIFY->value)) {
            abort(403, 'Aksi ini tidak diizinkan.');
        }

        $this->socialMediaService->updateSocialMedia($request->validated());

        $this->flashSuccess('Pengaturan media sosial berhasil disimpan.');

        return redirect()->route('admin.system.settings.index');
    }

    public function updateMarketplace(MarketplaceRequest $request): RedirectResponse
    {
        $this->marketplaceService->updateMarketplace($request->validated(), $request->user());

        if ($this->ownerVerificationService->hasPendingMarketplaceVerification()) {
            $this->flashSuccess('Perubahan pengaturan marketplace berhasil diajukan dan menunggu verifikasi owner.');
        } else {
            $this->flashSuccess('Pengaturan marketplace berhasil disimpan.');
        }

        return redirect()->route('admin.system.settings.index');
    }

    public function updateHr(HrSettingRequest $request): RedirectResponse
    {
        if (! $request->user()->can(Permission::OWNER_VERIFICATIONS_VERIFY->value)) {
            abort(403, 'Aksi ini tidak diizinkan.');
        }

        $this->hrSettingService->updateHr($request->validated());

        $this->flashSuccess('Pengaturan HR berhasil disimpan.');

        return redirect()->route('admin.system.settings.index');
    }

    public function updateHomepage(HomepageSettingRequest $request): RedirectResponse
    {
        if (! $request->user()->can(Permission::OWNER_VERIFICATIONS_VERIFY->value)) {
            abort(403, 'Aksi ini tidak diizinkan.');
        }

        $this->homepageSettingService->updateHomepage($request->validated());

        $this->flashSuccess('Pengaturan homepage berhasil disimpan.');

        return redirect()->route('admin.system.settings.index');
    }
}
