<?php

namespace App\Modules\Inventory\Services;

use App\Modules\Inventory\Models\Warehouse;
use App\Modules\Inventory\Repositories\WarehouseRepository;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;

final class InventoryWarehouseCatalogService
{
    public function __construct(
        private readonly WarehouseRepository $warehouses,
    ) {}

    /**
     * @return Collection<int, Warehouse>
     */
    public function listOrdered(): Collection
    {
        return $this->warehouses->listWithSectionsOrdered($this->tenantId());
    }

    public function paginateForIndex(Request $request): LengthAwarePaginator
    {
        return $this->warehouses->paginateForIndex($this->tenantId(), $request);
    }

    private function tenantId(): int
    {
        $id = auth()->user()?->tenant_id;
        if ($id === null) {
            abort(403);
        }

        return (int) $id;
    }
}
