<?php

namespace App\Http\Controllers\Admin;

use App\DataTables\BookingDataTable;
use App\Http\Controllers\Controller;
use App\Models\Booking;
use App\Repositories\BookingRepository;
use Illuminate\Http\Request;

class BookingController extends Controller
{
    public function __construct(protected BookingRepository $repository) {}

    public function index(BookingDataTable $dataTable)
    {
        return view('admin.bookings.index', [
            'dataTable' => $dataTable->html(),
        ]);
    }

    public function datatable(BookingDataTable $dataTable)
    {
        return $dataTable->ajax();
    }

    public function show(Booking $booking)
    {
        $booking->load(['member', 'game', 'court', 'participants.member', 'participants.dependent']);
        return view('admin.bookings.show', compact('booking'));
    }

    public function destroy(Booking $booking)
    {
        $result = $this->repository->delete($booking, ['participants']);
        return response()->json([
            'message' => $result['success'] ? __('Booking deleted successfully') : __('dashboard.cannot_delete_has_relations'),
            'success' => $result['success'],
            'relations' => $result['relations']
        ]);
    }

    public function updateStatus(Booking $booking)
    {
        $result = $this->repository->statusToggle($booking);
        return response()->json([
            'message' => $result['message'],
            'success' => $result['success']
        ]);
    }

    public function bulkDelete(Request $request)
    {
        $request->validate(['ids' => 'required|array']);
        $result = $this->repository->bulkDelete($request->ids);
        return response()->json([
            'message' => __('dashboard.deleted_success'),
            'success' => true,
            'data' => $result
        ]);
    }

    public function bulkStatus(Request $request)
    {
        $request->validate(['ids' => 'required|array', 'status' => 'required|boolean']);
        $result = $this->repository->bulkStatus($request->ids, $request->status);
        return response()->json([
            'message' => __('dashboard.status_updated'),
            'success' => true,
            'data' => $result
        ]);
    }
}
