<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\WithdrawMethod;
use App\Models\WithdrawType;
use App\Models\Setting;

class WithdrawSettingController extends Controller
{
    public function index()
    {
        return view('admin.withdraw.settings', [
            'methods' => WithdrawMethod::all(),
            'types' => WithdrawType::all(),
            'min' => Setting::getValue('withdraw_min_amount'),
            'max' => Setting::getValue('withdraw_max_amount'),
            'charge' => Setting::getValue('withdraw_charge_percent'),
            'notice_enabled' => (int) (Setting::getValue('withdraw_notice_enabled') ?? 0),
            'notice_title' => (string) (Setting::getValue('withdraw_notice_title') ?? ''),
            'notice_body' => (string) (Setting::getValue('withdraw_notice_body') ?? ''),
        ]);
    }

    public function storeMethod(Request $request)
    {
        $request->validate(['name' => 'required|string|unique:withdraw_methods,name']);
        WithdrawMethod::create(['name' => $request->name]);
        return back()->with('success', 'Withdraw method added!');
    }

    public function storeType(Request $request)
    {
        $request->validate(['name' => 'required|string|unique:withdraw_types,name']);
        WithdrawType::create(['name' => $request->name]);
        return back()->with('success', 'Withdraw type added!');
    }

    public function storeConfig(Request $request)
    {
        $request->validate([
            'min' => 'required|numeric|min:0',
            'max' => 'required|numeric|gt:min',
            'charge' => 'required|numeric|min:0|max:100',
        ]);

        Setting::updateOrCreate(['key' => 'withdraw_min_amount'], ['value' => $request->min]);
        Setting::updateOrCreate(['key' => 'withdraw_max_amount'], ['value' => $request->max]);
        Setting::updateOrCreate(['key' => 'withdraw_charge_percent'], ['value' => $request->charge]);

        return back()->with('success', 'Withdraw configuration updated!');
    }

    public function storeNotice(Request $request)
    {
        $request->validate([
            'notice_enabled' => 'required|in:0,1',
            'notice_title' => 'nullable|string|max:150',
            'notice_body' => 'nullable|string|max:1000',
        ]);

        Setting::updateOrCreate(['key' => 'withdraw_notice_enabled'], ['value' => (int) $request->notice_enabled]);
        Setting::updateOrCreate(['key' => 'withdraw_notice_title'], ['value' => (string) $request->notice_title]);
        Setting::updateOrCreate(['key' => 'withdraw_notice_body'], ['value' => (string) $request->notice_body]);

        return back()->with('success', 'Withdraw notice updated!');
    }
}
