<?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\TaxConfiguration\StoreTaxConfigurationRequest;
use Modules\HR\Http\Requests\TaxConfiguration\UpdateTaxConfigurationRequest;
use Modules\HR\Models\HrSettings;
use Modules\HR\Models\TaxConfiguration;
use Modules\HR\Services\TaxConfigurationService;
use Modules\HR\Transformers\TaxConfigurationResource;

class TaxConfigurationController extends Controller
{
    use AuthorizesRequests;

    public function __construct(
        protected TaxConfigurationService $taxConfigurationService
    ) {}

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

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

        return (TaxConfigurationResource::collection($response))->additional([
            'status' => true,
            'message' => 'Tax configurations retrieved successfully.'
        ]);
    }


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


        $taxConfiguration = $this->taxConfigurationService->store($request->validated());

        return (new TaxConfigurationResource($taxConfiguration))->additional([
            'status' => true,
            'message' => 'Tax configuration created successfully.'
        ]);
    }

    public function show($id)
    {
        $this->authorize('hrSettingView', HrSettings::class);

        return (new TaxConfigurationResource($this->taxConfigurationService->show($id)))->additional([
            'status' => true,
            'message' => 'Tax configuration retrieved successfully.'
        ]);
    }

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

        $taxConfiguration = TaxConfiguration::findOrFail($id);

        $taxConfiguration = $this->taxConfigurationService->update(
            $taxConfiguration,
            $request->validated()
        );

        return (new TaxConfigurationResource($taxConfiguration))->additional([
            'status' => true,
            'message' => 'Tax configuration updated successfully.'
        ]);
    }

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

        $taxConfiguration = TaxConfiguration::findOrFail($id);

        $taxConfiguration = $this->taxConfigurationService->updateStatus(
            $taxConfiguration,
            $request->only('status')
        );

        return ApiResponse::successResponse('Tax configuration status updated successfully.');
    }
}
