<?php

namespace ZenoChatbot\Services\Indexing\Strategies;

use ZenoChatbot\Services\Indexing\Contracts\IndexStrategyInterface;
use ZenoChatbot\Services\Indexing\Contracts\JobStrategyInterface;
use ZenoChatbot\Services\Indexing\Engine\ProductContentIndexEngine;

class ProductIndex implements IndexStrategyInterface, JobStrategyInterface
{
    /** @var array<int,string> */
    private $fields;
    /** @var int|null */
    private $chunkSize;

    /** @param array<string,mixed> $items */
    public function __construct(array $items = [])
    {
        $this->fields = isset($items['fields']) && is_array($items['fields']) ? $items['fields'] : [];
        $chunk = isset($items['chunk_size']) ? (int) $items['chunk_size'] : 30;
        $this->chunkSize = $chunk > 0 ? $chunk : 30;
    }

    public function key(): string
    {
        return 'product';
    }

    public function index(): array
    {
        return ProductContentIndexEngine::startBuildJob($this->fields, $this->chunkSize);
    }

    public function get(): array
    {
        return [
            'success' => true,
            'data' => ProductContentIndexEngine::getAll(),
        ];
    }

    public function clear(): array
    {
        return ProductContentIndexEngine::clearIndex();
    }

    public function stats(): array
    {
        return ProductContentIndexEngine::getStats();
    }

    public function startJob(): array
    {
        return ProductContentIndexEngine::startBuildJob($this->fields, $this->chunkSize);
    }

    public function processJob(string $jobId): array
    {
        return ProductContentIndexEngine::processBuildJob($jobId);
    }

    public function getJobStatus(string $jobId): array
    {
        return ProductContentIndexEngine::getBuildJobStatus($jobId);
    }

    public function sync(): array
    {
        return [
            'success' => true,
            'updated' => false,
            'message' => 'No sync operation for product index.',
            'data' => ProductContentIndexEngine::getStats(),
        ];
    }
}
