<?php

use Kirby\Cms\File;
use Kirby\Cms\Page;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;
use tobimori\Seo\Field\AltTextField;
use tobimori\Seo\Seo;

return [
	'system.loadPlugins:after' => function () {
		if (class_exists('tobimori\Queues\Queues')) {
			\tobimori\Queues\Queues::register([
				\tobimori\Seo\Jobs\GenerateAltTextJob::class,
				\tobimori\Seo\Jobs\IndexNowBatchJob::class,
			]);
		}
	},
	'file.create:after' => function (File $file) {
		if ($file->type() !== 'image') {
			return;
		}

		if (class_exists('tobimori\Queues\Queues')) {
			\tobimori\Queues\Queues::push('seo:generate-alt-text', [
				'fileId' => $file->id(),
			]);

			return $file;
		}

		return AltTextField::generateForFile($file);
	},
	'page.update:after' => function (Page $newPage, Page $oldPage) {
		// only inject blueprint defaults if the seo tab is present
		if ($newPage->blueprint()->tab('seo')) {
			$updates = A::reduce(
				$newPage->kirby()->option('tobimori.seo.robots.types'),
				function ($carry, $robots) use ($newPage) {
					$upper = Str::ucfirst($robots);

					if ($newPage->content()->get("robots{$upper}")->value() === '') {
						$carry["robots{$upper}"] = 'default';
					}

					return $carry;
				},
				[]
			);

			if (A::count($updates)) {
				$newPage = $newPage->update($updates, $newPage->kirby()->languageCode());
			}
		}

		if (Seo::option('indexnow.enabled')) {
			$indexNow = new (Seo::option('components.indexnow'))($newPage);
			$indexNow->dispatch();
		}

		return $newPage;
	},
	'page.changeStatus:after' => function (Page $newPage, Page $oldPage) {
		if (Seo::option('indexnow.enabled')) {
			$indexNow = new (Seo::option('components.indexnow'))($newPage);
			$indexNow->dispatch();
		}
	},
	'page.changeSlug:after' => function (Page $newPage, Page $oldPage) {
		if (Seo::option('indexnow.enabled')) {
			$indexNow = new (Seo::option('components.indexnow'))($newPage);
			$indexNow->dispatch();
		}
	},
	'page.render:before' => function (string $contentType, array $data, Page $page) {
		if (!class_exists('Spatie\SchemaOrg\Schema')) {
			return;
		}

		if (option('tobimori.seo.generateSchema')) {
			$page->schema('WebSite')
				->url($page->metadata()->canonicalUrl())
				->copyrightYear(date('Y'))
				->description($page->metadata()->metaDescription())
				->name($page->metadata()->metaTitle())
				->headline($page->metadata()->title());
		}
	},
];
