<?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\DTO\EmployeeSettingFilter;
use Modules\Employee\Http\Requests\Blood\StoreBloodRequest;
use Modules\Employee\Http\Requests\Blood\UpdateBloodRequest;
use Modules\Employee\Http\Requests\UpdateEmployeeSettingStatusRequest;
use Modules\Employee\Models\Blood;
use Modules\Employee\Models\EmployeeSetting;
use Modules\Employee\Services\BloodService;
use Modules\Employee\Transformers\BloodResource;

class BloodController extends Controller
{
    use AuthorizesRequests;

    protected $bloodService;

    public function __construct(BloodService $bloodService) {
        $this->bloodService = $bloodService;
    }

    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $this->authorize('employeeSettingView', EmployeeSetting::class);

        $filter = EmployeeSettingFilter::fromRequest($request);
        $data = $this->bloodService->list($filter);

        return(BloodResource::collection($data))->additional([
            'status' => true,
            'message' => 'Bloods retrieved successfully.'
        ]);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreBloodRequest $request)
    {
        $this->authorize('employeeSettingManage', EmployeeSetting::class);

        $blood = $this->bloodService->store($request->validated());

        return (new BloodResource($blood))->additional([
            'status' => true,
            'message' => 'Blood created successfully.'
        ]);
    }

    /**
     * Show the specified resource.
     */
    public function show($id)
    {
        $this->authorize('employeeSettingView', EmployeeSetting::class);
        return (new BloodResource($this->bloodService->show($id)))->additional([
            'status' => true,
            'message' => 'Blood retrieved successfully.'
        ]);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateBloodRequest $request, $id)
    {
        $this->authorize('employeeSettingManage', EmployeeSetting::class);

        $blood = Blood::findOrFail($id);

        $blood = $this->bloodService->update(
            $blood,
            $request->validated()
        );

        return (new BloodResource($blood))->additional([
            'status' => true,
            'message' => 'Blood updated successfully.'
        ]);
    }


    public function updateStatus(UpdateEmployeeSettingStatusRequest $request, $id)
    {
       $this->authorize('employeeSettingManage', EmployeeSetting::class);

        $blood = Blood::findOrFail($id);

        $this->bloodService->updateStatus(
            $blood,
            $request->validated()
        );
        return ApiResponse::successResponse('Blood status updated successfully.');
    }
}
