<?php

namespace Koneko\VuexyWarehouse\Services;

use Illuminate\Support\Facades\DB;

class WarehouseCatalogService
{
    protected $catalogs = [
        'warehouses' => [
            'table' => 'warehouses',
            'key' => 'id',
            'value' => "CONCAT(code, ' - ', name) as item",
            'search_columns' => ['code', 'name'],
            'order_by' => 'name',
            'limit' => 15,
        ],
        'store_warehouses' => [
            'table' => 'warehouses',
            'key' => 'id',
            'value' => "CONCAT(code, ' - ', name) as item",
            'search_columns' => ['code', 'name'],
            'extra_conditions' => ['store_id'],
            'order_by' => 'name',
            'limit' => 15,
        ],
        'product_categories' => [
            'table' => 'product_categories',
            'key' => 'id',
            'value' => "CONCAT(name, ' - ', slug) as item",
            'search_columns' => ['name', 'slug'],
            'order_by' => 'name',
            'limit' => 20,
        ],
        'products' => [
            'table' => 'products',
            'key' => 'id',
            'value' => "CONCAT(no_identificacion, ' - ', descripcion) as item",
            'search_columns' => ['no_identificacion', 'descripcion'],
            'order_by' => 'descripcion',
            'limit' => 20,
        ],
        'inventory_levels' => [
            'table' => 'inventory_stock_levels',
            'key' => 'id',
            'value' => "CONCAT(product_id, ' - ', quantity) as item",
            'search_columns' => ['product_id', 'quantity'],
            'extra_conditions' => ['warehouse_id'],
            'order_by' => 'quantity',
            'limit' => 10,
        ],
    ];

    /**
     * Busca registros en los catálogos del módulo de almacenes.
     *
     * @param string $catalog
     * @param string|null $searchTerm
     * @param array $options
     * @return array
     */
    public function searchCatalog(string $catalog, ?string $searchTerm = '', array $options = []): array
    {
        if (!isset($this->catalogs[$catalog])) {
            return [];
        }

        $config = $this->catalogs[$catalog];
        $query = DB::table($config['table']);

        // Selección de columnas
        $query->selectRaw("{$config['key']}, {$config['value']}");

        // Aplicar condiciones extra
        if (!empty($config['extra_conditions'])) {
            foreach ($config['extra_conditions'] as $column => $value) {
                if (isset($options[$column])) {
                    $query->where($column, $options[$column]);
                }
            }
        }

        // Búsqueda
        if ($searchTerm) {
            $query->where(function ($subQ) use ($config, $searchTerm) {
                foreach ($config['search_columns'] as $column) {
                    $subQ->orWhere($column, 'LIKE', "%{$searchTerm}%");
                }
            });
        }

        // Orden y límite
        $query->orderBy($config['order_by'] ?? $config['key'], 'asc');
        $query->limit($options['limit'] ?? $config['limit'] ?? 20);

        return $query->pluck('item', $config['key'])->toArray();
    }
}