<?php

namespace ZenoChatbot\Services\Indexing\Strategies;

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

class SiteMapIndex implements IndexStrategyInterface, JobStrategyInterface
{
    /** @var string */
    private $mode;
    /** @var string */
    private $manualPath;
    /** @var int|null */
    private $chunkSize;

    /** @param array<string,mixed> $source */
    public function __construct(array $source = [])
    {
        $this->mode = isset($source['mode']) ? (string) $source['mode'] : 'auto';
        $this->manualPath = isset($source['manual_path']) ? (string) $source['manual_path'] : '';
        $chunk = isset($source['chunk_size']) ? (int) $source['chunk_size'] : 50;
        $this->chunkSize = $chunk > 0 ? $chunk : 50;
    }

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

    public function index(): array
    {
        return SitemapContentIndexEngine::buildIndex($this->chunkSize);
    }

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

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

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

    public function startJob(): array
    {
        return SitemapContentIndexEngine::startBuildJob($this->mode, $this->manualPath, $this->chunkSize);
    }

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

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

    public function sync(): array
    {
        return SitemapContentIndexEngine::syncIfChanged($this->chunkSize);
    }
}
