<?php

namespace App\Http\Controllers\Admin;

use App\DataTables\CourtDataTable;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Court\StoreCourtRequest;
use App\Http\Requests\Admin\Court\UpdateCourtRequest;
use App\Models\Court;
use App\Models\Game;
use App\Repositories\CourtRepository;
use App\DataTables\SlotDataTable;
use Illuminate\Http\Request;

class CourtController extends Controller
{
    public function __construct(protected CourtRepository $repository) {}

    public function index(CourtDataTable $dataTable)
    {
        $games = Game::active()->orderBy('name')->get();
        return view('admin.courts.index', [
            'dataTable' => $dataTable->html(),
            'games' => $games,
        ]);
    }

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

    public function create()
    {
        $games = Game::active()->orderBy('name')->get();
        return view('admin.courts.create', compact('games'));
    }

    public function store(StoreCourtRequest $request)
    {
        $this->repository->create($request->validated());
        return response()->json([
            'message' => __('Court created successfully'),
            'redirect_url' => route('admin.courts.index')
        ]);
    }

    public function show(Court $court, SlotDataTable $dataTable)
    {
        $court->load('game');
        $dataTable->withFilters(['court_id' => $court->id]);
        return view('admin.courts.show', compact('court'))
            ->with('dataTable', $dataTable->html());
    }

    public function edit(Court $court)
    {
        $games = Game::active()->orderBy('name')->get();
        return view('admin.courts.edit', compact('court', 'games'));
    }

    public function update(UpdateCourtRequest $request, Court $court)
    {
        $this->repository->update($court, $request->validated());
        return response()->json([
            'message' => __('Court updated successfully'),
            'redirect_url' => route('admin.courts.index')
        ]);
    }

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

    public function updateStatus(Court $court)
    {
        $result = $this->repository->statusToggle($court, '', 'is_active');
        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, 'is_active');
        return response()->json([
            'message' => __('dashboard.status_updated'),
            'success' => true,
            'data' => $result
        ]);
    }
}
