<?php

/**
 * 文件功能：钓鱼事件后台管理控制器
 * 提供钓鱼事件的列表展示、创建、编辑、删除、启用/禁用功能
 *
 * @author   ChatRoom Laravel
 *
 * @version  1.0.0
 */

namespace App\Http\Controllers\Admin;

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

class FishingEventController extends Controller
{
    /**
     * 显示所有钓鱼事件列表
     */
    public function index(): View
    {
        $events = FishingEvent::orderBy('sort')->orderBy('id')->get();
        $totalWeight = $events->where('is_active', true)->sum('weight');

        return view('admin.fishing.index', compact('events', 'totalWeight'));
    }

    /**
     * 创建新钓鱼事件
     */
    public function store(Request $request): RedirectResponse
    {
        $data = $request->validate([
            'emoji' => 'required|string|max:10',
            'name' => 'required|string|max:100',
            'message' => 'required|string|max:255',
            'exp' => 'required|integer',
            'jjb' => 'required|integer',
            'weight' => 'required|integer|min:1|max:9999',
            'sort' => 'required|integer|min:0',
            'is_active' => 'boolean',
        ]);

        $data['is_active'] = $request->boolean('is_active', true);
        FishingEvent::create($data);

        return redirect()->route('admin.fishing.index')->with('success', '钓鱼事件已添加！');
    }

    /**
     * 更新钓鱼事件
     *
     * @param  FishingEvent  $fishing  路由模型绑定
     */
    public function update(Request $request, FishingEvent $fishing): RedirectResponse
    {
        $data = $request->validate([
            'emoji' => 'required|string|max:10',
            'name' => 'required|string|max:100',
            'message' => 'required|string|max:255',
            'exp' => 'required|integer',
            'jjb' => 'required|integer',
            'weight' => 'required|integer|min:1|max:9999',
            'sort' => 'required|integer|min:0',
            'is_active' => 'boolean',
        ]);

        $data['is_active'] = $request->boolean('is_active');
        $fishing->update($data);

        return redirect()->route('admin.fishing.index')->with('success', "事件「{$fishing->name}」已更新！");
    }

    /**
     * 切换事件启用/禁用状态（AJAX）
     *
     * @param  FishingEvent  $fishing  路由模型绑定
     */
    public function toggle(FishingEvent $fishing): JsonResponse
    {
        $fishing->update(['is_active' => ! $fishing->is_active]);

        return response()->json([
            'ok' => true,
            'is_active' => $fishing->is_active,
            'message' => $fishing->is_active ? "「{$fishing->name}」已启用" : "「{$fishing->name}」已禁用",
        ]);
    }

    /**
     * 删除钓鱼事件
     *
     * @param  FishingEvent  $fishing  路由模型绑定
     */
    public function destroy(FishingEvent $fishing): RedirectResponse
    {
        $name = $fishing->name;
        $fishing->delete();

        return redirect()->route('admin.fishing.index')->with('success', "事件「{$name}」已删除！");
    }
}
