<?php

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

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

class GeneralController extends Controller
{
    private Config $configModel;

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

    /**
     * 기타
     * @method GET
     * @see /admin/config/setting/general
     */
    public function index()
    {
        return view('admin.config.setting.general', [
            'config' => $this->configModel->getAllMeta()
        ]);
    }

    /**
     * 기타 저장
     * @method POST
     * @see /admin/config/setting/general
     */
    public function store(Request $request)
    {
        $rules = [
            'g2a_order_bot_token' => 'nullable|string|max:255',
            'g2a_product_bot_token' => 'nullable|string|max:255',
            'g2a_error_bot_token' => 'nullable|string|max:255',
            'main_bot_token' => 'nullable|string|max:255'
        ];

        $attributes = [
            'g2a_order_bot_token' => 'G2A Order Bot Token',
            'g2a_product_bot_token' => 'G2A Product Bot Token',
            'g2a_error_bot_token' => 'G2A Error Bot Token',
            'main_bot_token' => 'Main Bot Token'
        ];

        $posts = $this->validate($request, $rules, [], $attributes);

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

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