<?php

namespace App\Domains\Admin\Controllers;

use App\Domains\Admin\Actions\UpdateTenantTax\UpdateTenantTaxAction;
use App\Domains\Admin\Actions\UpdateTenantTax\UpdateTenantTaxRequest;
use App\Enumerations\TaxExemptionReason;
use App\Enumerations\VatPricingMode;
use App\Scopes\CommonController;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ManagedTenantTaxUpdateController extends CommonController
{
    public function __invoke(string $slug, Request $request): JsonResponse
    {
        $tenant = $this->adminTenants->findBySlug($slug);

        $action = new UpdateTenantTaxAction(new UpdateTenantTaxRequest(
            tenant: $tenant,
            taxLiable: $request->boolean('tax_liable'),
            vatRate: (int)$request->input('vat_rate', 0),
            exemptionReason: TaxExemptionReason::where('slug', $request->input('tax_exemption_reason'))->first(),
            exemptionNote: $request->input('tax_exemption_note'),
            vatPricingMode: VatPricingMode::tryFrom((string) $request->input('vat_pricing_mode', 'inclusive')) ?? VatPricingMode::Inclusive,
        ));

        $response = $action->execute();

        return response()->json([
            'status' => $response->success ? 'success' : 'error',
            'message' => $response->message,
        ]);
    }
}
