<?php

namespace Botble\Ecommerce\Http\Controllers\Admin;

use Botble\Ecommerce\Models\StoreLocator;
use Botble\Ecommerce\Models\Tax;
use Illuminate\Http\Request;
use Botble\Base\Http\Responses\BaseHttpResponse;

class VendorTaxController
{
    public function getTax(Request $request, $storeId, BaseHttpResponse $response)
    {
        // Find the store locator by ID
        $store = StoreLocator::find($storeId);

        if (! $store) {
            return $response
                ->setError()
                ->setMessage('Store not found');
        }

        // If store has tax_id, fetch it
        $tax = null;
        if ($store->tax_id) {
            $tax = Tax::find($store->tax_id);
        }

        if (! $tax) {
            return $response
                ->setError()
                ->setMessage('No tax class assigned to this store');
        }

        return $response
            ->setData([
                'id' => $tax->id,
                'name' => $tax->title,
                'percentage' => $tax->percentage,
                'title_with_percentage' => $tax->title_with_percentage,
            ])
            ->setMessage('Tax class found');
    }
}
