<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Rooms;

class RoomsController extends Controller
{
    public function index()
    {
        try {
            $rom = Rooms::with(['propertyType.property', 'user'])
                ->orderBy('created_at', 'desc')
                ->orderBy('id', 'desc')
                ->paginate(10);
                
            return response()->json([
                'status' => 'success',
                'rooms' => $rom
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to retrieve rooms.',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    public function update(Request $request, $id)
    {
        try {
            $request->validate([
                'kode_kamar' => 'nullable|string',
                'id_user' => 'nullable|nullable|exists:users,id',
                'status' => 'nullable|in:available,booked,occupied,maintenance',
                'lantai' => 'nullable|integer',
            ]);

            $room = Rooms::findOrFail($id);
            
            $room->update([
                'kode_kamar' => $request->kode_kamar,
                'id_user' => $request->id_user,
                'status' => $request->status,
                'lantai' => $request->lantai,
            ]);

            return response()->json([
                'status' => 'success',
                'message' => 'Room updated successfully',
                'room' => $room->load(['propertyType.property', 'user'])
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to update room.',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    public function destroy($id)
    {
        try {
            $room = Rooms::findOrFail($id);
            $room->delete(); // Soft delete

            return response()->json([
                'status' => 'success',
                'message' => 'Room deleted successfully'
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to delete room.',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    public function forceDelete($id)
    {
        try {
            $room = Rooms::withTrashed()->findOrFail($id);

            // ❗ Cek apakah room ini digunakan dalam booking
            $hasBooking = \App\Models\Booking::where('id_room', $id)->exists();
            if ($hasBooking) {
                return response()->json([
                    'success' => false,
                    'message' => 'Room tidak dapat dihapus karena masih digunakan dalam booking.'
                ], 422);
            }

            $room->forceDelete();

            return response()->json([
                'success' => true,
                'message' => 'Room permanently deleted'
            ], 200);

        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to permanently delete room.',
                'error' => $e->getMessage()
            ], 500);
        }
    }


    public function trashed()
    {
        try {
            $trashedRooms = Rooms::onlyTrashed()
                ->with(['propertyType.property', 'user'])
                ->orderBy('created_at', 'desc')
                ->orderBy('id', 'desc')
                ->paginate(10);

            return response()->json([
                'status' => 'success',
                'trashed_rooms' => $trashedRooms
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to retrieve trashed rooms.',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    public function restore($id)
    {
        try {
            $room = Rooms::onlyTrashed()->findOrFail($id);
            $room->restore();

            return response()->json([
                'status' => 'success',
                'message' => 'Room restored successfully',
                'room' => $room->load(['propertyType.property', 'user'])
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to restore room.',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    public function restoreAll()
    {
        try {
            $restoredCount = Rooms::onlyTrashed()->restore();

            return response()->json([
                'status' => 'success',
                'message' => 'All trashed rooms restored successfully',
                'restored_count' => $restoredCount
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to restore all trashed rooms.',
                'error' => $e->getMessage()
            ], 500);
        }
    }
}
