<?php

namespace App\Modules\HR\Overtime\Services;

use App\Models\User;
use App\Modules\HR\Overtime\Models\OvertimeRequest;
use App\Modules\HR\Overtime\Repositories\OvertimeRequestRepository;
use App\Modules\HR\Overtime\Support\OvertimeRequestStatus;
use Illuminate\Support\Facades\DB;

class OvertimeReviewService
{
    public function __construct(
        private readonly OvertimeRequestRepository $requests,
    ) {}

    public function approve(OvertimeRequest $request, User $reviewer): OvertimeRequest
    {
        return $this->decide($request, $reviewer, OvertimeRequestStatus::APPROVED);
    }

    public function reject(OvertimeRequest $request, User $reviewer): OvertimeRequest
    {
        return $this->decide($request, $reviewer, OvertimeRequestStatus::REJECTED);
    }

    private function decide(OvertimeRequest $request, User $reviewer, string $status): OvertimeRequest
    {
        return DB::transaction(function () use ($request, $reviewer, $status): OvertimeRequest {
            $locked = $this->requests->lockForTenant((int) $request->tenant_id, (int) $request->id);
            if ($locked === null) {
                abort(404);
            }
            if ($locked->status !== OvertimeRequestStatus::PENDING) {
                return $locked;
            }

            return $this->requests->update($locked, [
                'status' => $status,
                'reviewed_by' => $reviewer->id,
                'reviewed_at' => now(),
            ]);
        });
    }
}
