<?php
namespace Modules\Employee\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Modules\Core\Helpers\ApiResponse;
use Modules\Employee\DTO\EmployeeSettingFilter;
use Modules\Employee\Http\Requests\Project\StoreProjectRequest;
use Modules\Employee\Http\Requests\Project\UpdateProjectRequest;
use Modules\Employee\Http\Requests\UpdateEmployeeSettingStatusRequest;
use Modules\Employee\Models\Project;
use Modules\Employee\Services\ProjectService;
use Modules\Employee\Models\EmployeeSetting;
use Modules\Employee\Transformers\ProjectResource;

class ProjectController extends Controller
{
    use AuthorizesRequests;

    protected $projectService;

    public function __construct(ProjectService $projectService) {
        $this->projectService = $projectService;
    }

    public function index(Request $request)
    {
        $this->authorize('employeeSettingView', EmployeeSetting::class);

        $filter = EmployeeSettingFilter::fromRequest($request);
        $data = $this->projectService->list($filter);

        return(ProjectResource::collection($data))->additional([
            'status' => true,
            'message' => 'Projects retrieved successfully.'
        ]);
    }

    public function store(StoreProjectRequest $request)
    {
        $this->authorize('employeeSettingManage', EmployeeSetting::class);

        $project = $this->projectService->store($request->validated());

        return (new ProjectResource($project))->additional([
            'status' => true,
            'message' => 'Project created successfully.'
        ]);
    }

    public function show($id)
    {
        $this->authorize('employeeSettingView', EmployeeSetting::class);

        return (new ProjectResource($this->projectService->show($id)))->additional([
            'status' => true,
            'message' => 'Project retrieved successfully.'
        ]);
    }

    public function update(UpdateProjectRequest $request, $id)
    {
        $this->authorize('employeeSettingManage', EmployeeSetting::class);

        $project = Project::findOrFail($id);

        $project = $this->projectService->update(
            $project,
            $request->validated()
        );

        return (new ProjectResource($project))->additional([
            'status' => true,
            'message' => 'Project updated successfully.'
        ]);
    }

    public function updateStatus(UpdateEmployeeSettingStatusRequest $request, $id)
    {
        $this->authorize('employeeSettingManage', EmployeeSetting::class);

        $project = Project::findOrFail($id);

        $this->projectService->updateStatus(
            $project,
            $request->validated()
        );

        return ApiResponse::successResponse('Project status updated successfully.');
    }


}
