<?php
/**
 * Migration: create `affilun_product_urls` table.
 *
 * @package Affilun\Infrastructure\Persistence\Migrations
 */

declare(strict_types=1);

namespace Affilun\Infrastructure\Persistence\Migrations;

use Affilun\Infrastructure\Persistence\SchemaVersion;

/**
 * Creates child URL rows for affilun-owned product IDs.
 */
final class CreateProductUrlsTable extends AbstractCreateTableMigration {

	public const TABLE_LOGICAL_NAME = 'affilun_product_urls';

	/**
	 * Return the schema version introduced by this migration.
	 */
	public function targetVersion(): SchemaVersion {
		return SchemaVersion::of( 9 );
	}

	/**
	 * Return the migration description.
	 */
	public function description(): string {
		return 'create affilun_product_urls table';
	}

	/**
	 * Return the logical table name for this migration.
	 */
	protected function logicalTableName(): string {
		return self::TABLE_LOGICAL_NAME;
	}

	/**
	 * @return list<string>
	 */
	protected function columnDefinitions(): array {
		return array(
			'id bigint unsigned NOT NULL AUTO_INCREMENT',
			'product_id bigint unsigned NOT NULL',
			'provider_type varchar(32) NOT NULL',
			'provider_item_id varchar(191) NULL',
			'url text NOT NULL',
			'url_hash char(64) NOT NULL',
			"source varchar(32) NOT NULL DEFAULT 'user'",
			'created_at datetime NOT NULL',
			'updated_at datetime NOT NULL',
		);
	}

	/**
	 * @return list<string>
	 */
	protected function indexDefinitions(): array {
		return array(
			'PRIMARY KEY  (id)',
			'UNIQUE KEY uq_product_provider_url (product_id, provider_type, url_hash)',
			'KEY idx_product_id (product_id)',
			'KEY idx_provider_type (provider_type)',
			'KEY idx_provider_item (provider_type, provider_item_id)',
			'KEY idx_created_at (created_at)',
		);
	}
}
