<?php

/**
 * 文件功能：后台房间管理控制器
 * 管理员可查看、编辑房间信息（名称、介绍、公告等）
 *
 * @author  ChatRoom Laravel
 *
 * @version 1.0.0
 */

namespace App\Http\Controllers\Admin;

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

class RoomManagerController extends Controller
{
    /**
     * 显示所有房间列表
     */
    public function index(): View
    {
        $rooms = Room::orderBy('id')->get();

        return view('admin.rooms.index', compact('rooms'));
    }

    /**
     * 更新房间信息
     */
    public function update(Request $request, int $id): RedirectResponse
    {
        $room = Room::findOrFail($id);

        $data = $request->validate([
            'room_name' => 'required|string|max:100',
            'room_des' => 'nullable|string|max:500',
            'announcement' => 'nullable|string|max:500',
            'room_owner' => 'nullable|string|max:50',
            'permit_level' => 'required|integer|min:0|max:15',
            'door_open' => 'required|boolean',
        ]);

        $room->update($data);

        return redirect()->route('admin.rooms.index')->with('success', "房间 [{$room->room_name}] 信息已更新！");
    }

    /**
     * 删除房间（非系统房间）
     */
    public function destroy(int $id): RedirectResponse
    {
        $room = Room::findOrFail($id);

        if ($room->room_keep) {
            return redirect()->route('admin.rooms.index')->with('error', '系统房间不允许删除！');
        }

        $room->delete();

        return redirect()->route('admin.rooms.index')->with('success', "房间 [{$room->room_name}] 已删除！");
    }
}
