<?php

namespace App\Http\Controllers\Bookings;

use App\Http\Controllers\Controller;
use App\Models\RoomAvailability;
use App\Services\RoomAvailabilityService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class RoomAvailabilityController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request, RoomAvailabilityService $service)
    {
        $validated = $request->validate([
            'room_id' => 'required',
            'start_date' => 'required',
            'end_date' => 'required',
            'status' => 'required',
            'message' => 'nullable',
        ]);

        if ($validated['status'] === 'blocked') {
            $service->createRecord(
                $validated['room_id'],
                null,
                $validated['start_date'],
                $validated['end_date'],
                'blocked',
                'admin',
                $validated['message'],
                Auth::id()
            );
        }

        if ($validated['status'] === 'booked') {
            $service->createRecord(
                $validated['room_id'],
                null,
                $validated['start_date'],
                $validated['end_date'],
                'booked',
                'admin',
                $validated['message'],
                Auth::id()
            );
        }

        return back()->with('success', 'Saved successfully');
    }
    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, RoomAvailability $room_availability, RoomAvailabilityService $service)
    {



        $validated = $request->validate([
            'room_id' => 'required',
            'start_date' => 'required|date',
            'end_date' => 'required|date|after_or_equal:start_date',
            'status' => 'required|in:blocked,booked,pending',
            'message' => 'nullable|string',
        ]);

        DB::beginTransaction();

        try {

            $service->updateAvailability(
                $room_availability->id,
                $validated['room_id'],
                $validated['start_date'],
                $validated['end_date'],
                $validated['status'],
                $validated['message'] ?? null,
                Auth::id()
            );

            DB::commit();

            return back()->with('success',  'Availability updated successfully');
        } catch (\Exception $e) {
            DB::rollBack();

            return back()->with('error',   $e->getMessage());
        }
    }
    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id, RoomAvailabilityService $service)
    {
        try {

            $deleted = $service->delete((int) $id);

            if (!$deleted) {
                return back()->withErrors(['general' => 'Room availability not found or already deleted.']);
            }

            return back()->with('success', 'Room availability deleted successfully.');
        } catch (\Throwable $e) {
            report($e);
            return back()->withErrors(['general' => 'Something went wrong while deleting availability.']);
        }
    }
}
