<?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\System\PushNotificationService;
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 Illuminate\Http\Request;
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 PushNotificationService $pushNotificationService,
    ) {}

    public function index(Request $request): Response
    {
        $user = $request->user();

        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(),
            'canUpdateSystem' => $user->can(Permission::SETTINGS_UPDATE_SYSTEM->value),
            'canUpdateSocialMedia' => $user->can(Permission::SETTINGS_UPDATE_SOCIAL_MEDIA->value),
            'canUpdateMarketplace' => $user->can(Permission::SETTINGS_UPDATE_MARKETPLACE->value),
            'canUpdateHr' => $user->can(Permission::SETTINGS_UPDATE_HR->value),
            'canUpdateHomepage' => $user->can(Permission::SETTINGS_UPDATE_HOMEPAGE->value),
        ]);
    }

    public function updateSystem(SystemRequest $request): RedirectResponse
    {
        $this->systemService->updateSystem($request->validated());

        $this->notifyOwnerIfAdmin($request->user(), 'Sistem', 'Pengaturan sistem');

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

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

    public function updateSocialMedia(SocialMediaRequest $request): RedirectResponse
    {
        $this->socialMediaService->updateSocialMedia($request->validated());

        $this->notifyOwnerIfAdmin($request->user(), 'Media Sosial', 'Pengaturan media sosial');

        $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());

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

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

    public function updateHr(HrSettingRequest $request): RedirectResponse
    {
        $this->hrSettingService->updateHr($request->validated());

        $this->notifyOwnerIfAdmin($request->user(), 'HR', 'Pengaturan HR');

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

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

    public function updateHomepage(HomepageSettingRequest $request): RedirectResponse
    {
        $this->homepageSettingService->updateHomepage($request->validated());

        $this->notifyOwnerIfAdmin($request->user(), 'Homepage', 'Pengaturan homepage');

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

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

    private function notifyOwnerIfAdmin($user, string $sectionLabel, string $settingLabel): void
    {
        if ($user->can(Permission::OWNER_VERIFICATIONS_VERIFY->value)) {
            return;
        }

        $this->pushNotificationService->sendToRoles(
            "⚙️ {$sectionLabel} Diubah",
            "{$settingLabel} telah diubah oleh '{$user->username}'.",
            ['owner', 'developer'],
            route('admin.system.settings.index'),
        );
    }
}
