<?php

namespace Modules\Team\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Modules\Team\Requests\Admin\TeamRequest;
use Modules\Team\Services\TeamService;

class TeamController extends Controller
{
    private $redirectSuccessPath = '/admin/teams';
    private $viewDirectory = 'admin.teams.';

    public function __construct(private TeamService $teamService)
    {
    }

    public function index(Request $request): View
    {
        $title = __('teams.head');
        return view($this->viewDirectory . 'index', get_defined_vars());
    }

    public function grid(Request $request): View
    {
        $request->merge(['paginate' => true]);
        $teams = $this->teamService->get($request->all());
        return view($this->viewDirectory . 'grid', get_defined_vars());
    }

    public function create(): View
    {
        $action = route('teams.store');
        $head = __('teams.new');
        return view($this->viewDirectory . 'form', get_defined_vars());
    }

    public function store(TeamRequest $request): JsonResponse
    {
        $this->teamService->create($request->validated());
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => route('teams.index'),
        ];
        return ResponseService::response($response);
    }

    public function edit(int $id): View
    {
        $team = $this->teamService->getItem($id);
        if (!$team) {
            abort(404);
        }
        $action = route('teams.update', $team->id);
        $head = __('teams.edit');
        return view($this->viewDirectory . 'form', get_defined_vars());
    }

    public function update(TeamRequest $request, int $id): JsonResponse
    {
        $team = $this->teamService->checkItem($id);
        if (!$team) {
            abort(404);
        }
        $this->teamService->update($request->validated(), $id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => route('teams.index'),
        ];
        return ResponseService::response($response);
    }

    public function destroy(int $id): JsonResponse
    {
        $this->teamService->destroy($id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => route('teams.index'),
        ];
        return ResponseService::response($response);
    }

    public function destroyAll(Request $request): JsonResponse
    {
        $this->teamService->destroyAll([
            'ids' => $request->ids,
            'status' => 1,
        ]);
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => route('teams.index'),
        ];
        return ResponseService::response($response);
    }

    public function restore(int $id): JsonResponse
    {
        $this->teamService->restore($id);
        session()->flash('message', __('dashboard.restoreDone'));
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => route('teams.index'),
        ];
        return ResponseService::response($response);
    }

    public function forceDelete(int $id): JsonResponse
    {
        $this->teamService->forceDelete($id);
        session()->flash('message', __('dashboard.deleteDone'));
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => route('teams.index'),
        ];
        return ResponseService::response($response);
    }

    public function forceDeleteAll(Request $request): JsonResponse
    {
        $this->teamService->forceDeleteAll($request->ids);
        session()->flash('message', __('dashboard.deleteDone'));
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => route('teams.index'),
        ];
        return ResponseService::response($response);
    }

    public function restoreAll(Request $request): JsonResponse
    {
        $this->teamService->destroyAll([
            'ids' => $request->ids,
            'status' => 0,
        ]);
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => route('teams.index'),
        ];
        return ResponseService::response($response);
    }
}

