<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ReferralSetting;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class ReferralSettingController extends Controller
{
    public function index(): View
    {
        return view('admin.referral-settings.index', [
            'settings' => ReferralSetting::current(),
        ]);
    }

    public function update(Request $request): RedirectResponse
    {
        $data = $request->validate([
            'referrals_enabled' => ['nullable', 'boolean'],
            'invite_3_reward' => ['nullable', 'string', 'max:255'],
            'invite_10_reward' => ['nullable', 'string', 'max:255'],
            'mentor_20_students_reward' => ['nullable', 'string', 'max:255'],
            'bonus_text' => ['nullable', 'string', 'max:5000'],
            'status' => ['required', 'in:active,inactive'],
        ]);

        $data['referrals_enabled'] = $request->boolean('referrals_enabled');
        ReferralSetting::current()->update($data);

        return back()->with('success', 'Referral settings updated.');
    }
}
