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

package v1_27

import (
	"code.mokoconsulting.tech/MokoConsulting/MokoGitea/modules/timeutil"

	"xorm.io/xorm"
)

// licenseKey340 adds columns that were introduced after v335/v337.
type licenseKey340 struct {
	ID                int64              `xorm:"pk autoincr"`
	KeyRaw            string             `xorm:"TEXT"`
	PaymentRef        string             `xorm:""`
	LastHeartbeatUnix timeutil.TimeStamp `xorm:"NOT NULL DEFAULT 0"`
	FirstUsedUnix     timeutil.TimeStamp `xorm:"NOT NULL DEFAULT 0"`
}

func (licenseKey340) TableName() string {
	return "license_key"
}

// licensePackage340 adds the IsArchived and DomainLockHours columns.
type licensePackage340 struct {
	ID              int64 `xorm:"pk autoincr"`
	IsArchived      bool  `xorm:"NOT NULL DEFAULT false"`
	DomainLockHours int   `xorm:"NOT NULL DEFAULT 0"`
}

func (licensePackage340) TableName() string {
	return "license_package"
}

// updateStreamConfig340 adds licensing, gating, and extension metadata columns.
type updateStreamConfig340 struct {
	ID               int64  `xorm:"pk autoincr"`
	LicensingEnabled bool   `xorm:"NOT NULL DEFAULT false"`
	FeedVisibility   string `xorm:"VARCHAR(20) NOT NULL DEFAULT 'public'"`
	DownloadGating   string `xorm:"VARCHAR(20) NOT NULL DEFAULT 'none'"`
	SupportURL       string `xorm:"TEXT"`
	ExtensionName    string `xorm:"TEXT"`
	DisplayName      string `xorm:"TEXT"`
	Description      string `xorm:"TEXT"`
	ExtensionType    string `xorm:"VARCHAR(50)"`
	Maintainer       string `xorm:"TEXT"`
	MaintainerURL    string `xorm:"TEXT"`
	InfoURL          string `xorm:"TEXT"`
	TargetVersion    string `xorm:"TEXT"`
	PHPMinimum       string `xorm:"VARCHAR(20)"`
	KeyPrefix        string `xorm:"VARCHAR(20)"`
}

func (updateStreamConfig340) TableName() string {
	return "update_stream_config"
}

// releaseStreamMap340 creates the release-to-stream manual mapping table.
type releaseStreamMap340 struct {
	ID          int64              `xorm:"pk autoincr"`
	ReleaseID   int64              `xorm:"UNIQUE NOT NULL INDEX"`
	RepoID      int64              `xorm:"NOT NULL INDEX"`
	StreamName  string             `xorm:"NOT NULL"`
	CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"`
}

func (releaseStreamMap340) TableName() string {
	return "release_stream_map"
}

// SyncLicenseSystemColumns adds missing columns and creates new tables.
func SyncLicenseSystemColumns(x *xorm.Engine) error {
	return x.Sync(
		new(licenseKey340),
		new(licensePackage340),
		new(updateStreamConfig340),
		new(releaseStreamMap340),
	)
}
