<?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\Employee\Services\EmployeeService;
use Modules\HR\DTO\HrSettingFilter;
use Modules\HR\Enums\SalaryRevisionStatusEnum;
use Modules\HR\Http\Requests\SalaryRevision\ApproveSalaryRevisionRequest;
use Modules\HR\Http\Requests\SalaryRevision\RejectSalaryRevisionRequest;
use Modules\HR\Models\HrSettings;
use Modules\HR\Services\SalaryRevisionService;
use Modules\HR\Transformers\SalaryRevisionResource;
use Modules\HR\Http\Requests\SalaryRevision\StoreSalaryRevisionRequest;
use Modules\HR\Http\Requests\UpdateSalaryRevisionStatusRequest;
use Modules\HR\Models\SalaryRevision;

class SalaryRevisionController extends Controller
{
    use AuthorizesRequests;

    public function __construct(
        protected EmployeeService $employeeService,
        protected SalaryRevisionService $salaryRevisionService
    ) {}

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

        $filter = HrSettingFilter::fromRequest($request);

        $revisions = $this->salaryRevisionService->list($filter);

        return SalaryRevisionResource::collection($revisions)->additional([
            'status' => true,
            'message' => 'Salary revisions retrieved successfully.'
        ]);
    }

    public function store(StoreSalaryRevisionRequest $request)
    {

        $this->authorize('hrSettingManage', HrSettings::class);
        $revision = $this->salaryRevisionService->store(
            $request->validated()
        );

        return (new SalaryRevisionResource($revision))->additional([
            'status' => true,
            'message' => 'Salary revision created successfully.'
        ]);
    }

    /**
     * Show the specified resource.
     */
    public function show($id)
    {
        $this->authorize('hrSettingView', HrSettings::class);

        return (new SalaryRevisionResource($this->salaryRevisionService->show($id)))->additional([
            'status' => true,
            'message' => 'Salary revision retrieved successfully.'
        ]);
    }

    public function approve(ApproveSalaryRevisionRequest $data, SalaryRevision $salaryRevision)
    {
        $this->authorize('approved', $salaryRevision);

        $this->salaryRevisionService->approve(
            $salaryRevision,
            $data->validated()
        );
        return ApiResponse::successResponse('Salary revision approved successfully.');
    }

    public function reject(RejectSalaryRevisionRequest $data, SalaryRevision $salaryRevision)
    {
        $this->authorize('approved', $salaryRevision);

        $this->salaryRevisionService->reject(
            $salaryRevision,
            $data->validated()
        );
        return ApiResponse::successResponse('Salary revision rejected successfully.');
    }


    public function apply(SalaryRevision $salaryRevision)
    {
        $this->authorize('applied', $salaryRevision);

        $this->salaryRevisionService->apply($salaryRevision);

        return ApiResponse::successResponse('Salary revision applied successfully.');
    }


}
