<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\NotificationSetting\BulkUpdateSettingRequest;
use App\Http\Requests\NotificationSetting\UpdateGroupedSettingRequest;
use App\Http\Requests\NotificationSetting\UpdateSettingRequest;
use App\Services\NotificationSettingService;
use Illuminate\Http\JsonResponse;

class NotificationSettingController extends Controller
{
    public function __construct(
        private readonly NotificationSettingService $service
    ) {}

    /**
     * 알림 설정 조회
     */
    public function index(): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->service->getSettings(),
            __('message.fetched')
        );
    }

    /**
     * 알림 설정 업데이트 (단일)
     */
    public function update(UpdateSettingRequest $request): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->service->updateSetting($request->validated()),
            __('message.updated')
        );
    }

    /**
     * 알림 설정 일괄 업데이트
     */
    public function bulkUpdate(BulkUpdateSettingRequest $request): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->service->bulkUpdateSettings($request->validated()['settings']),
            __('message.bulk_upsert')
        );
    }

    /**
     * 그룹 기반 알림 설정 조회 (React 호환)
     */
    public function indexGrouped(): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->service->getGroupedSettings(),
            __('message.fetched')
        );
    }

    /**
     * 그룹 기반 알림 설정 업데이트 (React 호환)
     */
    public function updateGrouped(UpdateGroupedSettingRequest $request): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->service->updateGroupedSettings($request->validated()),
            __('message.updated')
        );
    }
}
