// Copyright 2026 Moko Consulting <hello@mokoconsulting.tech>
// SPDX-License-Identifier: GPL-3.0-or-later

package v1_27

import "xorm.io/xorm"

// MigrateUpdateServerFieldsToManifest copies extension metadata from
// update_stream_config into repo_manifest where the manifest fields are empty.
// This consolidates the source of truth into repo_manifest.
func MigrateUpdateServerFieldsToManifest(x *xorm.Engine) error {
	// Copy display_name from config to manifest where manifest is empty
	_, err := x.Exec(`
		UPDATE repo_manifest m
		INNER JOIN update_stream_config c ON m.repo_id = c.repo_id
		SET m.display_name = c.display_name
		WHERE (m.display_name IS NULL OR m.display_name = '') AND c.display_name != ''
	`)
	if err != nil {
		return err
	}

	// Copy extension_name → element_name
	_, err = x.Exec(`
		UPDATE repo_manifest m
		INNER JOIN update_stream_config c ON m.repo_id = c.repo_id
		SET m.element_name = c.extension_name
		WHERE (m.element_name IS NULL OR m.element_name = '') AND c.extension_name != ''
	`)
	if err != nil {
		return err
	}

	// Copy extension_type → package_type
	_, err = x.Exec(`
		UPDATE repo_manifest m
		INNER JOIN update_stream_config c ON m.repo_id = c.repo_id
		SET m.package_type = c.extension_type
		WHERE (m.package_type IS NULL OR m.package_type = '') AND c.extension_type != ''
	`)
	if err != nil {
		return err
	}

	// Copy target_version
	_, err = x.Exec(`
		UPDATE repo_manifest m
		INNER JOIN update_stream_config c ON m.repo_id = c.repo_id
		SET m.target_version = c.target_version
		WHERE (m.target_version IS NULL OR m.target_version = '') AND c.target_version != ''
	`)
	if err != nil {
		return err
	}

	// Copy maintainer
	_, err = x.Exec(`
		UPDATE repo_manifest m
		INNER JOIN update_stream_config c ON m.repo_id = c.repo_id
		SET m.maintainer = c.maintainer
		WHERE (m.maintainer IS NULL OR m.maintainer = '') AND c.maintainer != ''
	`)
	if err != nil {
		return err
	}

	// Copy maintainer_url
	_, err = x.Exec(`
		UPDATE repo_manifest m
		INNER JOIN update_stream_config c ON m.repo_id = c.repo_id
		SET m.maintainer_url = c.maintainer_url
		WHERE (m.maintainer_url IS NULL OR m.maintainer_url = '') AND c.maintainer_url != ''
	`)
	if err != nil {
		return err
	}

	// Copy info_url
	_, err = x.Exec(`
		UPDATE repo_manifest m
		INNER JOIN update_stream_config c ON m.repo_id = c.repo_id
		SET m.info_url = c.info_url
		WHERE (m.info_url IS NULL OR m.info_url = '') AND c.info_url != ''
	`)
	if err != nil {
		return err
	}

	// Copy php_minimum
	_, err = x.Exec(`
		UPDATE repo_manifest m
		INNER JOIN update_stream_config c ON m.repo_id = c.repo_id
		SET m.php_minimum = c.php_minimum
		WHERE (m.php_minimum IS NULL OR m.php_minimum = '') AND c.php_minimum != ''
	`)
	if err != nil {
		return err
	}

	// Copy platform from config to manifest where manifest platform is empty
	_, err = x.Exec(`
		UPDATE repo_manifest m
		INNER JOIN update_stream_config c ON m.repo_id = c.repo_id
		SET m.platform = c.platform
		WHERE (m.platform IS NULL OR m.platform = '') AND c.platform != ''
	`)
	return err
}
