<?php

namespace App\Http\Controllers\Admin\Sms;

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

class ConfigController extends Controller
{
    private Config $configModel;
    private SmsLib $smsLib;
    private array $smsConfig;

    public function __construct(Config $config, SmsLib $smsLib)
    {
        $this->configModel = $config;
        $this->smsLib = $smsLib;
        $this->smsConfig = $this->smsLib->info();
    }

    /**
     * 환경 설정
     * @method GET
     * @see /admin/sms/config
     */
    public function index()
    {
        return view('admin.sms.config.index', [
            'icodeRet' => $this->smsConfig
        ]);
    }

    /**
     * 환경 설정 저장
     * @method POST
     * @see /admin/sms/config
     */
    public function store(Request $request)
    {
        $rules = [
            'use_sms' => 'numeric|in:0,1',
            'sms_icode_id' => 'string|nullable',
            'sms_icode_pw' => 'string|nullable',
            'sms_icode_call_num' => 'string|nullable',
            'sms_icode_host' => 'string|nullable',
            'sms_icode_port' => 'numeric|nullable',
            'sms_icode_token' => 'string|nullable|max:255'
        ];

        $attributes = [
            'use_sms' => 'SMS 기능 ',
            'sms_icode_id' => '아이코드 ID',
            'sms_icode_pw' => '아이코드 PW',
            'sms_icode_call_num' => '문자 발송 번호',
            'sms_icode_host' => '서버 IP',
            'sms_icode_port' => '서버 Port',
            'sms_icode_token' => '토큰키 (Token key)'
        ];

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

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

        $message = '환경 설정 정보가 저장되었습니다.';
        return redirect()->route('admin.sms.config.index')->with('message', $message);
    }
}
