<?php

namespace App\Command\spider;

use App\Helpers\AppHelper;
use App\Model\AppBrand;
use Hyperf\Command\Annotation\Command;
use Hyperf\Coroutine\Coroutine;
use Hyperf\DbConnection\Db;
use Hyperf\Logger\LoggerFactory;
use Psr\Container\ContainerInterface;
use Swoole\ExitException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function Swoole\Coroutine\run;

#[Command]
class FashionSnapCommand extends BaseSpider
{
    protected const PLATFORM = 'fashionsnap';

    protected string $baseUrl = 'https://www.fashionsnap.com';

    public function __construct(protected ContainerInterface $container, LoggerFactory $loggerFactory)
    {
        parent::__construct('spider:fashionsnap');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('自动采集fashionsnap.com');
        $this->addOption('brandId', 'b', InputOption::VALUE_OPTIONAL, '指定的品牌id', false);
    }

    private function _getTask($brand): \Generator
    {
        $query = Db::table('app_brands');

        if ($brand) {
            $query->whereIn('id', explode(',', $brand));
        } else {
            $query->where('spider_origin', '=', 'fashionsnap')->orderBy('id');
        }

        foreach ($query->cursor() as $row) {
            if (!$row) {
                throw new ExitException('END.');
            }
            yield $row;
        }
    }
    public function execute(InputInterface $input, OutputInterface $output): int
    {
        $brand = $input->getOption('brandId');

        run(function () use ($brand) {
            foreach ($this->_getTask($brand) as $task) {
                list($result, $httpCode) = $this->request($this->getBaseUrl() . "/api/algolia/article/?blogIds=4&brandName={$task->name}&limit=50");
                echo $task->name . '--' . $httpCode . PHP_EOL;
                if ($httpCode == 200) {
                    $isSuccess = false;
                    $result = json_decode($result, true);
                    if ($result['totalCount'] == 0 || $result['totalCount'] > 200) {
                        continue;
                    }

                    foreach ($result['articles'] ?? [] as $item) {
                        $model = $this->getArticleModel(['title' => $item['mainCategory']['name'], 'platform' => static::PLATFORM, 'brand' => $task->id]);
                        $model->title = $item['mainCategory']['name'];
                        $model->year = AppHelper::getYear($model->title);
                        $model->brand = $task->id;
                        $model->module = 0;
                        $model->platform = self::getPlatform();

                        $saveImages = [];
                        foreach ($item['mainGalleryImages'] as $image) {
                            $saveImages[] = [
                                'src' => 'https://fashionsnap-assets.com/asset/width=4096' . $image
                            ];
                        }
                        $model->images = json_encode($saveImages);
                        $model->cover = $saveImages[0]['src'] ?? '';
                        // permalink
                        $model->source_url = 'https://fashionsnap.com' . $item['permalink'];
                        if ($model->cover) {
                            $isSuccess = $model->save();
                        }
                    }

                    if ($isSuccess) {
                        $brandModel = AppBrand::find($task->id);
                        $brandModel->spider_origin = self::getPlatform();
                        $brandModel->save();
                    }
                }
            }
        });

        return 0;
    }
}