<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

/**
 * 微信搜索服务类
 */
final class WechatSearchService
{
    /**
     * 构造函数
     */
    public function __construct(
        private readonly Container $app
    ) {}

    /**
     * 提交小程序页面索引
     */
    public function submitPages(array $pages, string $driver = 'miniprogram', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->search->submitPages($pages);

        return [
            'code' => 'SUCCESS',
            'message' => '提交小程序页面索引成功',
            'data' => $result
        ];
    }

    /**
     * 获取小程序页面收录情况
     */
    public function getPageStatus(array $pages, string $driver = 'miniprogram', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->search->getPageStatus($pages);

        return [
            'code' => 'SUCCESS',
            'message' => '获取小程序页面收录情况成功',
            'data' => $result
        ];
    }

    /**
     * 提交公众号文章索引
     */
    public function submitArticles(array $articles, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->search->submitArticles($articles);

        return [
            'code' => 'SUCCESS',
            'message' => '提交公众号文章索引成功',
            'data' => $result
        ];
    }

    /**
     * 获取公众号文章收录情况
     */
    public function getArticleStatus(array $articles, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->search->getArticleStatus($articles);

        return [
            'code' => 'SUCCESS',
            'message' => '获取公众号文章收录情况成功',
            'data' => $result
        ];
    }

    /**
     * 提交站点地图
     */
    public function submitSitemap(string $sitemapUrl, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->search->submitSitemap($sitemapUrl);

        return [
            'code' => 'SUCCESS',
            'message' => '提交站点地图成功',
            'data' => $result
        ];
    }

    /**
     * 获取站点地图状态
     */
    public function getSitemapStatus(string $sitemapUrl, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->search->getSitemapStatus($sitemapUrl);

        return [
            'code' => 'SUCCESS',
            'message' => '获取站点地图状态成功',
            'data' => $result
        ];
    }

    /**
     * 获取搜索数据统计
     */
    public function getSearchStats(string $startDate, string $endDate, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->search->getStats($startDate, $endDate);

        return [
            'code' => 'SUCCESS',
            'message' => '获取搜索数据统计成功',
            'data' => $result
        ];
    }

    /**
     * 获取搜索关键词排行
     */
    public function getSearchKeywords(string $startDate, string $endDate, int $limit = 10, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->search->getKeywords($startDate, $endDate, $limit);

        return [
            'code' => 'SUCCESS',
            'message' => '获取搜索关键词排行成功',
            'data' => $result
        ];
    }

    /**
     * 获取微信应用实例
     */
    protected function getApp(string $driver, string $name = 'default'): mixed
    {
        return $this->app->make('kylin.wechat')->driver($driver, $name);
    }
}
