<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Push\RegisterTokenRequest;
use App\Http\Requests\Push\UpdateSettingsRequest;
use App\Services\PushNotificationService;
use Illuminate\Http\Request;

class PushNotificationController extends Controller
{
    public function __construct(
        private readonly PushNotificationService $service
    ) {}

    /**
     * FCM 토큰 등록
     */
    public function registerToken(RegisterTokenRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->registerToken($request->validated());
        }, __('message.push.token_registered'));
    }

    /**
     * FCM 토큰 해제
     */
    public function unregisterToken(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $token = $request->input('token');
            if (! $token) {
                throw new \InvalidArgumentException(__('error.push.token_required'));
            }

            return ['unregistered' => $this->service->unregisterToken($token)];
        }, __('message.push.token_unregistered'));
    }

    /**
     * 사용자의 등록된 디바이스 토큰 목록
     */
    public function getTokens()
    {
        return ApiResponse::handle(function () {
            return $this->service->getUserTokens();
        });
    }

    /**
     * 알림 설정 조회
     */
    public function getSettings()
    {
        return ApiResponse::handle(function () {
            return $this->service->getSettings();
        });
    }

    /**
     * 알림 설정 업데이트
     */
    public function updateSettings(UpdateSettingsRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->updateSettings($request->validated()['settings']);
        }, __('message.push.settings_updated'));
    }

    /**
     * 알림 유형 목록 조회
     */
    public function getNotificationTypes()
    {
        return ApiResponse::handle(function () {
            return [
                'types' => \App\Models\PushNotificationSetting::getAllTypes(),
                'sounds' => \App\Models\PushNotificationSetting::getAllSounds(),
            ];
        });
    }
}
