<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\RoomType;
use Illuminate\Support\Facades\DB;

class RoomTypeController extends Controller
{
    public function __construct()
    {
        $this->middleware('permission:view-rooms')->only(['index', 'show']);
        $this->middleware('permission:create-rooms')->only(['create', 'store']);
        $this->middleware('permission:edit-rooms')->only(['edit', 'update']);
        $this->middleware('permission:delete-rooms')->only(['destroy']);
    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $roomTypes = RoomType::withCount('rooms')->paginate(10);
        return view('backend.room-types.index', compact('roomTypes'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('backend.room-types.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'room_type' => 'required|string|max:100|unique:room_types,room_type',
            'price' => 'required|numeric|min:0',
        ]);

        DB::table('room_types')->insert([
            'room_type' => $request->room_type,
            'price' => $request->price,
        ]);

        return redirect()->route('room-types.index')->with('success', 'Room type created successfully!');
    }

    /**
     * Display the specified resource.
     */
    public function show(RoomType $roomType)
    {
        $roomType->load('rooms');
        return view('backend.room-types.show', compact('roomType'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(RoomType $roomType)
    {
        return view('backend.room-types.edit', compact('roomType'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, RoomType $roomType)
    {
        $request->validate([
            'room_type' => 'required|string|max:100|unique:room_types,room_type,' . $roomType->room_type_id . ',room_type_id',
            'price' => 'required|numeric|min:0',
        ]);

        DB::table('room_types')
            ->where('room_type_id', $roomType->room_type_id)
            ->update([
                'room_type' => $request->room_type,
                'price' => $request->price,
            ]);

        return redirect()->route('room-types.index')->with('success', 'Room type updated successfully!');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(RoomType $roomType)
    {
        // Check if room type is being used
        $roomCount = DB::table('rooms')->where('room_type_id', $roomType->room_type_id)->count();
        
        if ($roomCount > 0) {
            return back()->with('error', 'Cannot delete room type. It is being used by ' . $roomCount . ' room(s).');
        }

        DB::table('room_types')->where('room_type_id', $roomType->room_type_id)->delete();
        
        return redirect()->route('room-types.index')->with('success', 'Room type deleted successfully!');
    }
}
