<?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\Http\Requests\Employee\ReactivateEmployeeRequest;
use Modules\Employee\Http\Requests\Employee\TerminateEmployeeRequest;
use Modules\Employee\Services\EmployeeService;
use Modules\Employee\Transformers\Employee\EmployeeInfo;
use Modules\Employee\Transformers\Employee\EmpTerminationResource;

class EmployeeTerminationController extends Controller
{
    use AuthorizesRequests;

    protected $employeeService;

    public function __construct(EmployeeService $employeeService)
    {
        $this->employeeService = $employeeService;
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(TerminateEmployeeRequest $request, $id)
    {
        $employee = $this->employeeService->find($id);

        $this->authorize('update', $employee);

        $response = $this->employeeService->terminate($employee, $request->validated());

        return (new EmployeeInfo($response))->additional([
            'status' => true,
            'message' => 'Employee terminated successfully.',
        ]);
    }

    /**
     * Show the specified resource.
     */
    public function show($id)
    {
        $employee = $this->employeeService->find($id);

        $this->authorize('update', $employee);

        $response = $this->employeeService->getCurrentTermination($employee);

        return ApiResponse::successResponse(
            message: $response['message'],
            data: $response['data'] ? new EmpTerminationResource($response['data']) : null
        );
    }

    /**
     * Update the specified resource in storage.
     */
    public function activate(ReactivateEmployeeRequest $request, $id)
    {
        $employee = $this->employeeService->find($id);

        $this->authorize('update', $employee);

        $response = $this->employeeService->reactivate($employee, $request->validated());

        return (new EmployeeInfo($response))->additional([
            'status' => true,
            'message' => 'Employee activated successfully.',
        ]);
    }

    public function update(TerminateEmployeeRequest $request, $id)
    {
        $employee = $this->employeeService->find($id);

        $this->authorize('update', $employee);

        $response = $this->employeeService->updateTermination($employee, $request->validated());
        
        return (new EmpTerminationResource($response))->additional([
            'status' => true,
            'message' => 'Termination information updated successfully.',
        ]);
    }
}
