<?php

namespace App\Http\Controllers\Admin\User\Dormant;

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

class ConfigController extends Controller
{
    private Config $configModel;

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

    /**
     * 휴면계정 - 환경설정
     * @method GET
     * @see /admin/user/point
     */
    public function index()
    {
        return view('admin.user.dormant.config', []);
    }

    /**
     * 휴면계정 - 환경설정 저장
     * @method POST
     * @see /admin/user/point
     */
    public function store(Request $request)
    {
        $rules = [
            'user_dormant_use' => 'required|numeric|in:0,1',
            'user_auto_withdraw_day' => 'required|numeric|in:3,5,7,14,30',
            'user_withdraw_shop_review_delete' => 'numeric|nullable',
            'user_withdraw_shop_qna_delete' => 'numeric|nullable',
            'user_withdraw_shop_order_delete' => 'numeric|nullable',
            'user_withdraw_board_qna_delete' => 'numeric|nullable',
            'user_withdraw_shop_wish_list_delete' => 'numeric|nullable',
            'user_withdraw_shop_cart_delete' => 'numeric|nullable'
        ];

        $attributes = [
            'user_dormant_use' => '휴면계정 전환 여부',
            'user_auto_withdraw_day' => '탈퇴 요청회원 자동삭제 기한',
            'user_withdraw_shop_review_delete' => '탈퇴 요청회원 자동삭제 자료 - 구매후기',
            'user_withdraw_shop_qna_delete' => '탈퇴 요청회원 자동삭제 자료 - 상품문의',
            'user_withdraw_shop_order_delete' => '탈퇴 요청회원 자동삭제 자료 - 주문내역',
            'user_withdraw_board_qna_delete' => '탈퇴 요청회원 자동삭제 자료 - 1:1 상담',
            'user_withdraw_shop_wish_list_delete' => '탈퇴 요청회원 자동삭제 자료 - 관심상품',
            'user_withdraw_shop_cart_delete' => '탈퇴 요청회원 자동삭제 자료 - 장바구니'
        ];

        $posts = $this->validate($request, $rules, [], $attributes);
        $posts['user_withdraw_shop_review_delete'] ??= 0;
        $posts['user_withdraw_shop_qna_delete'] ??= 0;
        $posts['user_withdraw_shop_order_delete'] ??= 0;
        $posts['user_withdraw_board_qna_delete'] ??= 0;
        $posts['user_withdraw_shop_wish_list_delete'] ??= 0;
        $posts['user_withdraw_shop_cart_delete'] ??= 0;

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

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