<?php

namespace App\Http\Controllers\Admin\Room;

use App\Http\Controllers\Controller;
use App\Models\Room\Reservation;
use App\Models\Room\Room;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ReservationController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = Reservation::query()->with(['customer', 'room.roomType']);
        $query->when($request->customer_id, fn($q, $v) => $q->where('customer_id', $v));
        $query->when($request->room_id, fn($q, $v) => $q->where('room_id', $v));
        $query->when($request->status, fn($q, $v) => $q->where('status', $v));
        $query->when($request->check_in_date, fn($q, $v) => $q->whereDate('check_in_date', $v));

        return $this->paginate($query->latest()->paginate($request->input('per_page', 15)));
    }

    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'customer_id' => 'required|exists:customers,id',
            'contract_id' => 'nullable|exists:contracts,id',
            'room_id' => 'required|exists:rooms,id',
            'check_in_date' => 'required|date',
            'check_out_date' => 'required|date|after_or_equal:check_in_date',
            'remark' => 'nullable|string',
        ]);
        $validated['created_by'] = auth()->id();
        $validated['status'] = 0;

        $reservation = Reservation::create($validated);
        Room::find($validated['room_id'])->update(['status' => 2]);

        return $this->success($reservation);
    }

    public function show(Reservation $reservation): JsonResponse
    {
        return $this->success($reservation->load(['customer', 'room.roomType', 'contract', 'outings']));
    }

    public function update(Request $request, Reservation $reservation): JsonResponse
    {
        if ($reservation->status >= 2) {
            return $this->error('已退房或已取消的预定不可修改', 40001);
        }
        $validated = $request->validate([
            'room_id' => 'sometimes|exists:rooms,id',
            'check_in_date' => 'sometimes|date',
            'check_out_date' => 'sometimes|date|after_or_equal:check_in_date',
            'remark' => 'nullable|string',
        ]);
        $reservation->update($validated);
        return $this->success($reservation);
    }

    public function destroy(Reservation $reservation): JsonResponse
    {
        if ($reservation->status === 1) {
            return $this->error('已入住的预定不可删除', 40001);
        }
        $reservation->room->update(['status' => 1]);
        $reservation->delete();
        return $this->success(null);
    }

    public function checkIn(Reservation $reservation): JsonResponse
    {
        if ($reservation->status !== 0) {
            return $this->error('只有预定状态可以办理入住', 40001);
        }
        $reservation->update([
            'status' => 1,
            'actual_check_in' => now(),
        ]);
        $reservation->room->update(['status' => 3]);
        $reservation->customer->update(['status' => 3]);

        return $this->success($reservation);
    }

    public function checkOut(Reservation $reservation): JsonResponse
    {
        if ($reservation->status !== 1) {
            return $this->error('只有入住状态可以办理退房', 40001);
        }
        $reservation->update([
            'status' => 2,
            'actual_check_out' => now(),
        ]);
        $reservation->room->update(['status' => 5]);
        $reservation->customer->update(['status' => 4]);

        return $this->success($reservation);
    }

    public function cancel(Request $request, Reservation $reservation): JsonResponse
    {
        if ($reservation->status >= 2) {
            return $this->error('已退房或已取消的预定不可取消', 40001);
        }
        $reservation->update(['status' => 3, 'remark' => $request->input('remark', $reservation->remark)]);
        $reservation->room->update(['status' => 1]);

        return $this->success($reservation);
    }
}
