<?php

namespace App\Http\Controllers\Admin\Config\Setting;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Config;

class NotifyController extends Controller
{
    private Config $configModel;

    public function __construct(Config $config)
    {
        $this->configModel = $config;
    }

    /**
     * 알림
     * @method GET
     * @see /admin/config/setting/notify
     */
    public function index()
    {
        return view('admin.config.setting.notify', []);
    }

    /**
     * 알림 저장
     * @method POST
     * @see /admin/config/setting/notify
     */
    public function store(Request $request)
    {
        $rules = [
            'use_notification' => 'numeric|nullable|in:0,1',
            'notification_post_reply' => 'numeric|nullable|in:0,1',
            'notification_post_comment' => 'numeric|nullable|in:0,1',
            'notification_post_comment_reply' => 'numeric|nullable|in:0,1'
        ];

        $attributes = [
            'use_notification' => '알림 기능',
            'notification_post_reply' => '내 글에 답변글이 달렸을 때 알림',
            'notification_post_comment' => '내 글에 댓글이 달렸을 때 알림',
            'notification_post_comment_reply' => '내 댓글에 댓글이 달렸을 때 알림'
        ];

        $posts = $this->validate($request, $rules, [], $attributes);
        $posts['use_notification'] ??= '0';
        $posts['notification_post_reply'] ??= '0';
        $posts['notification_post_comment'] ??= '0';
        $posts['notification_post_comment_reply'] ??= '0';

        $this->configModel->save($posts, $attributes);

        $message = '알림 정보가 저장되었습니다.';
        return redirect()->route('admin.config.setting.notify.index')->with('message', $message);
    }
}
