<?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\HR\DTO\HrSettingFilter;
use Modules\HR\Http\Requests\FestivalBonusPolicy\StoreFestivalBonusPolicyRequest;
use Modules\HR\Http\Requests\FestivalBonusPolicy\UpdateFestivalBonusPolicyRequest;
use Modules\HR\Models\FestivalBonusPolicy;
use Modules\HR\Models\HrSettings;
use Modules\HR\Services\FestivalBonusPolicyService;
use Modules\HR\Transformers\FestivalBonusPolicyResource;

class FestivalBonusPolicyController extends Controller
{
    use AuthorizesRequests;

    public function __construct(
        protected FestivalBonusPolicyService $festivalBonusPolicyService
    ) {}

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

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

        return (FestivalBonusPolicyResource::collection($response))->additional([
            'status' => true,
            'message' => 'Festival bonus policies retrieved successfully.'
        ]);
    }

    public function store(StoreFestivalBonusPolicyRequest $request)
    {
        $this->authorize('hrSettingManage', HrSettings::class);
        $festivalBonusPolicy = $this->festivalBonusPolicyService->store($request->validated());

        return (new FestivalBonusPolicyResource($festivalBonusPolicy))->additional([
            'status' => true,
            'message' => 'Festival bonus policy created successfully.'
        ]);
    }

    public function show($id)
    {
        $this->authorize('hrSettingView', HrSettings::class);
        $festivalBonusPolicy = $this->festivalBonusPolicyService->show($id);
        return (new FestivalBonusPolicyResource($festivalBonusPolicy->load(['religion'])))->additional([
            'status' => true,
            'message' => 'Festival bonus policy retrieved successfully.'
        ]);
    }

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

        $festivalBonusPolicy = FestivalBonusPolicy::findOrFail($id);

        $festivalBonusPolicy = $this->festivalBonusPolicyService->update(
            $festivalBonusPolicy,
            $request->validated()
        );

        return (new FestivalBonusPolicyResource($festivalBonusPolicy))->additional([
            'status' => true,
            'message' => 'Festival bonus policy updated successfully.'
        ]);
    }

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

        $festivalBonusPolicy = FestivalBonusPolicy::findOrFail($id);

        $festivalBonusPolicy = $this->festivalBonusPolicyService->updateStatus(
            $festivalBonusPolicy,
            $request->only('status')
        );

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