<?php

namespace Modules\Employee\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Modules\Core\Helpers\ApiResponse;
use Modules\Employee\DTO\EmployeeSettingFilter;
use Modules\Employee\Http\Requests\Designation\StoreDesignationRequest;
use Modules\Employee\Http\Requests\Designation\UpdateDesignationRequest;
use Modules\Employee\Http\Requests\UpdateEmployeeSettingStatusRequest;
use Modules\Employee\Models\Designation;
use Modules\Employee\Models\EmployeeSetting;
use Modules\Employee\Services\DesignationService;
use Modules\Employee\Transformers\DesignationResource;

class DesignationController extends Controller
{
    use AuthorizesRequests;

    protected $designationService;

    public function __construct(DesignationService $designationService) {
        $this->designationService = $designationService;
    }

    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $this->authorize('employeeSettingView', EmployeeSetting::class);

        $filter = EmployeeSettingFilter::fromRequest($request);
        $data = $this->designationService->list($filter);
        return(DesignationResource::collection($data))->additional([
            'status' => true,
            'message' => 'Designations retrieved successfully.'
        ]);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreDesignationRequest $request)
    {
        $this->authorize('employeeSettingManage', EmployeeSetting::class);

        $designation = $this->designationService->store($request->validated());

        return (new DesignationResource($designation->load('salaryGrade:id,name','staffType:id,name')))->additional([
            'status' => true,
            'message' => 'Designation created successfully.'
        ]);
    }

    /**
     * Show the specified resource.
     */
    public function show($id)
    {
        $this->authorize('employeeSettingView', EmployeeSetting::class);
        return (new DesignationResource($this->designationService->show($id)->load('salaryGrade:id,name','staffType:id,name')))->additional([
            'status' => true,
            'message' => 'Designation retrieved successfully.'
        ]);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateDesignationRequest $request, $id)
    {
        $this->authorize('employeeSettingManage', EmployeeSetting::class);

        $designation = Designation::findOrFail($id);

        $designation = $this->designationService->update(
            $designation,
            $request->validated()
        );

        return (new DesignationResource($designation->load('salaryGrade:id,name','staffType:id,name')))->additional([
            'status' => true,
            'message' => 'Designation updated successfully.'
        ]);
    }


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

        $designation = Designation::findOrFail($id);

        $designation = $this->designationService->updateStatus(
            $designation,
            $request->validated()
        );
        return ApiResponse::successResponse('Designation status updated successfully.');
    }
}
