<?php

namespace Modules\HR\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Modules\Core\Helpers\ApiResponse;
use Modules\Core\Http\Requests\UpdateStatusRequest;
use Modules\HR\DTO\HrSettingFilter;
use Modules\HR\Http\Requests\SalaryStructureItem\StoreSalaryStructureItemRequest;
use Modules\HR\Http\Requests\SalaryStructureItem\UpdateSalaryStructureItemRequest;
use Modules\HR\Models\HrSettings;
use Modules\HR\Transformers\SalaryStructureItemResource;
use Modules\HR\Services\SalaryStructureItemService;

class SalaryStructureItemController extends Controller
{
    use AuthorizesRequests;
    /**
     * Display a listing of the resource.
     */
    protected $salaryStructureItemService;

    public function __construct(SalaryStructureItemService $salaryStructureItemService)
    {
        $this->salaryStructureItemService = $salaryStructureItemService;
    }

    public function index(Request $request)
    {
        $this->authorize('hrSettingView', HrSettings::class);
        $filter = HrSettingFilter::fromRequest($request);
        $response = $this->salaryStructureItemService->list($filter);

        return (SalaryStructureItemResource::collection($response))->additional([
            'status' => true,
            'message' => 'Salary structure item retrieved successfully.'
        ]);
    }

    public function store(StoreSalaryStructureItemRequest $request)
    {
        $this->authorize('hrSettingManage', HrSettings::class);
        $salaryStructureItem = $this->salaryStructureItemService->store($request->validated());

        return (new SalaryStructureItemResource($salaryStructureItem))->additional([
            'status' => true,
            'message' => 'Salary structure item created successfully.'
        ]);
    }

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

        $salaryStructureItem = $this->salaryStructureItemService->show($id);

        return (new SalaryStructureItemResource($salaryStructureItem))->additional([
            'status' => true,
            'message' => 'Salary structure item retrieved successfully.'
        ]);
    }

    public function update(UpdateSalaryStructureItemRequest $request, $id)
    {
        $this->authorize('hrSettingManage', HrSettings::class);

        $salaryStructureItem = $this->salaryStructureItemService->show($id);

        $updated = $this->salaryStructureItemService->update(
            $salaryStructureItem,
            $request->validated()
        );

        return (new SalaryStructureItemResource($updated))->additional([
            'status' => true,
            'message' => 'Salary structure item updated successfully.'
        ]);
    }

    public function updateStatus(UpdateStatusRequest $request, $id)
    {
        $this->authorize('hrSettingManage', HrSettings::class);
        $this->salaryStructureItemService->updateStatus(
            $this->salaryStructureItemService->show($id),
            $request->validated()
        );

        return ApiResponse::successResponse('Salary structure item status updated successfully.');
    }
}
