<?php

namespace App\Modules\Payroll\Exceptions;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class PayrollPeriodLockedException extends \Exception
{
    public function __construct(
        public readonly int $tenantId,
        ?string $message = null,
        public readonly string $errorCode = 'payroll_period_locked',
    ) {
        parent::__construct($message ?? __('payroll.protection.period_locked_message'));
    }

    public function render(Request $request): JsonResponse|\Illuminate\Http\RedirectResponse
    {
        if ($request->expectsJson()) {
            return response()->json([
                'message' => $this->getMessage(),
                'code' => $this->errorCode,
            ], Response::HTTP_LOCKED);
        }

        return redirect()->back()
            ->withInput()
            ->with('error', $this->getMessage());
    }
}
