<?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\Http\Requests\UpdateEmployeeSettingStatusRequest;
use Modules\HR\Models\HrSettings;
use Modules\HR\DTO\HrSettingFilter;
use Modules\HR\Http\Requests\HolidayGroup\StoreHolidayGroupRequest;
use Modules\HR\Http\Requests\HolidayGroup\UpdateHolidayGroupRequest;
use Modules\HR\Models\HolidayGroup;
use Modules\HR\Services\HolidayGroupService;
use Modules\HR\Transformers\HolidayGroupResource;

class HolidayGroupController extends Controller
{
    use AuthorizesRequests;

    public function __construct(protected HolidayGroupService $holidayGroupService) {}

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

        $filter = HrSettingFilter::fromRequest($request);
        $response = $this->holidayGroupService->list($filter);

        return (HolidayGroupResource::collection($response))->additional([
            'status' => true,
            'message' => 'Holiday groups retrieved successfully.'
        ]);
    }

    public function store(StoreHolidayGroupRequest $request)
    {
        $this->authorize('hrSettingManage', HrSettings::class);

        $holidayGroup = $this->holidayGroupService->store($request->validated());

        return (new HolidayGroupResource($holidayGroup))->additional([
            'status' => true,
            'message' => 'Holiday group created successfully.'
        ]);
    }




    public function show($id)
    {
        $this->authorize('hrSettingView', HrSettings::class);
        $holidayGroup = HolidayGroup::findOrFail($id);
        return (new HolidayGroupResource($holidayGroup->load('holidays')))->additional([
            'status' => true,
            'message' => 'Holiday group retrieved successfully.'
        ]);
    }


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

        $holidayGroup = HolidayGroup::findOrFail($id);

        $holidayGroup = $this->holidayGroupService->update(
            $holidayGroup,
            $request->validated()
        );

        return (new HolidayGroupResource($holidayGroup))->additional([
            'status' => true,
            'message' => 'Holiday group updated successfully.'
        ]);
    }

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

        $holidayGroup = HolidayGroup::findOrFail($id);

        $this->holidayGroupService->updateStatus(
            $holidayGroup,
            $request->validated()
        );

        return ApiResponse::successResponse('Holiday group status updated successfully.');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        //

        return response()->json([]);
    }
}
