<?php

namespace Modules\Project\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Modules\Area\Services\AreaService;
use Modules\Category\Services\CategoryService;
use Modules\City\Services\CityService;
use Modules\Project\Requests\Admin\ProjectRequest;
use Modules\Project\Services\ProjectService;

class ProjectController extends Controller
{
    private string $viewDirectory = 'admin.projects.';

    public function __construct(
        private ProjectService $projectService,
        private CategoryService $categoryService,
        private CityService $cityService,
        private AreaService $areaService
    ) {
    }

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

    public function grid(Request $request): View
    {
        $request->merge(['paginate' => true]);
        $projects = $this->projectService->get($request->all());

        return view($this->viewDirectory . 'grid', get_defined_vars());
    }

    public function create(): View
    {
        $action = route('projects.store');
        $head = __('projects.new');
        $project = null;
        $categories = $this->categoryService->get([]);
        $cities = $this->cityService->get([]);
        $areas = $this->areaService->get([]);

        return view($this->viewDirectory . 'form', get_defined_vars());
    }

    public function store(ProjectRequest $request): JsonResponse
    {
        $data = $request->validated();
        if ($request->hasFile('photos')) {
            $data['photos'] = $request->file('photos');
        }

        $this->projectService->create($data);
        session()->flash('message', __('dashboard.saveDone'));

        return ResponseService::response([
            'status' => 200,
            'message' => __('web.success'),
            'data' => route('projects.index'),
        ]);
    }

    public function edit(int $id): View
    {
        $project = $this->projectService->getItem($id);
        if (! $project) {
            abort(404);
        }

        $action = route('projects.update', $id);
        $head = __('projects.edit');
        $categories = $this->categoryService->get([]);
        $cities = $this->cityService->get([]);
        $areas = $this->areaService->get([]);

        return view($this->viewDirectory . 'form', get_defined_vars());
    }

    public function update(ProjectRequest $request, int $id): JsonResponse
    {
        $data = $request->validated();
        if ($request->hasFile('photos')) {
            $data['photos'] = $request->file('photos');
        }
        if ($request->has('photos_to_delete')) {
            $data['photos_to_delete'] = $request->input('photos_to_delete');
        }

        $project = $this->projectService->update($data, $id);
        if (! $project) {
            abort(404);
        }

        session()->flash('message', __('dashboard.saveDone'));

        return ResponseService::response([
            'status' => 200,
            'message' => __('web.success'),
            'data' => route('projects.index'),
        ]);
    }

    public function destroy(int $id): JsonResponse
    {
        $this->projectService->destroy($id);

        return ResponseService::response([
            'status' => 200,
            'message' => __('web.success'),
            'data' => route('projects.index'),
        ]);
    }

    public function destroyAll(Request $request): JsonResponse
    {
        $this->projectService->destroyAll([
            'ids' => $request->ids,
             'status' => 1,
        ]);

        return ResponseService::response([
            'status' => 200,
            'message' => __('web.success'),
            'data' => route('projects.index'),
        ]);
    }

    public function restore(int $id): JsonResponse
    {
        $this->projectService->destroy($id);

        return ResponseService::response([
            'status' => 200,
            'message' => __('web.success'),
            'data' => route('projects.index'),
        ]);
    }

    public function restoreAll(Request $request): JsonResponse
    {
        $this->projectService->destroyAll([
            'ids' => $request->ids,
            'status' => 0,
        ]);

        return ResponseService::response([
            'status' => 200,
            'message' => __('web.success'),
            'data' => route('projects.index'),
        ]);
    }
}
