<?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\DTO\HrSettingFilter;
use Modules\HR\Http\Requests\WeekendPolicy\StoreWeekendPolicyRequest;
use Modules\HR\Http\Requests\WeekendPolicy\UpdateWeekendPolicyRequest;
use Modules\HR\Models\HrSettings;
use Modules\HR\Services\WeekendPolicyService;
use Modules\HR\Transformers\WeekendPolicyResource;

class WeekendPolicyController extends Controller
{
    use AuthorizesRequests;
    /**
     * Display a listing of the resource.
     */
    protected $weekendPolicyService;

    public function __construct(WeekendPolicyService $weekendPolicyService)
    {
        $this->weekendPolicyService = $weekendPolicyService;
    }

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

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

        return (WeekendPolicyResource::collection($response))->additional([
            'status' => true,
            'message' => 'Weekend policy retrieved successfully.'
        ]);
    }

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

        $weekendPolicy = $this->weekendPolicyService->store($request->all());

        return (new WeekendPolicyResource($weekendPolicy))->additional([
            'status' => true,
            'message' => 'Weekend policy created successfully.'
        ]);
    }

    /**
     * Show the specified resource.
     */
    public function show($id)
    {
        $this->authorize('hrSettingView', HrSettings::class);

        return (new WeekendPolicyResource($this->weekendPolicyService->show($id)))->additional([
            'status' => true,
            'message' => 'Weekend policy retrieved successfully.'
        ]);
    }

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

        return (new WeekendPolicyResource($weekendPolicy))->additional([
            'status' => true,
            'message' => 'Weekend policy updated successfully.'
        ]);
    }

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

        $this->weekendPolicyService->updateStatus(
            $this->weekendPolicyService->show($id),
            $request->validated()
        );

        return ApiResponse::successResponse('Weekend policy status updated successfully.');
    }
}
