// 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"
)

type licensePackage335 struct {
	ID              int64              `xorm:"pk autoincr"`
	OwnerID         int64              `xorm:"INDEX NOT NULL"`
	Name            string             `xorm:"NOT NULL"`
	Description     string             `xorm:"TEXT"`
	DurationDays    int                `xorm:"NOT NULL DEFAULT 0"`
	MaxSites        int                `xorm:"NOT NULL DEFAULT 0"`
	RepoScope       string             `xorm:"TEXT NOT NULL DEFAULT 'all'"`
	AllowedChannels string             `xorm:"TEXT"`
	IsActive        bool               `xorm:"NOT NULL DEFAULT true"`
	CreatedUnix     timeutil.TimeStamp `xorm:"INDEX CREATED"`
	UpdatedUnix     timeutil.TimeStamp `xorm:"UPDATED"`
}

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

type licenseKey335 struct {
	ID                int64              `xorm:"pk autoincr"`
	PackageID         int64              `xorm:"INDEX NOT NULL"`
	OwnerID           int64              `xorm:"INDEX NOT NULL"`
	KeyHash           string             `xorm:"UNIQUE NOT NULL"`
	KeyPrefix         string             `xorm:"NOT NULL"`
	LicenseeName      string             `xorm:""`
	LicenseeEmail     string             `xorm:""`
	DomainRestriction string             `xorm:"TEXT"`
	MaxSites          int                `xorm:"NOT NULL DEFAULT 0"`
	IsInternal        bool               `xorm:"NOT NULL DEFAULT false"`
	IsActive          bool               `xorm:"NOT NULL DEFAULT true"`
	StartsUnix        timeutil.TimeStamp `xorm:"NOT NULL DEFAULT 0"`
	ExpiresUnix       timeutil.TimeStamp `xorm:"NOT NULL DEFAULT 0"`
	CreatedUnix       timeutil.TimeStamp `xorm:"INDEX CREATED"`
	UpdatedUnix       timeutil.TimeStamp `xorm:"UPDATED"`
}

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

type licenseKeyUsage335 struct {
	ID          int64              `xorm:"pk autoincr"`
	KeyID       int64              `xorm:"INDEX NOT NULL"`
	RepoID      int64              `xorm:"INDEX NOT NULL"`
	Domain      string             `xorm:""`
	IPAddress   string             `xorm:""`
	UserAgent   string             `xorm:"TEXT"`
	VersionFrom string             `xorm:""`
	CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"`
}

func (licenseKeyUsage335) TableName() string {
	return "license_key_usage"
}

// AddLicenseKeyTables creates the license_package, license_key, and
// license_key_usage tables for the update server license system.
func AddLicenseKeyTables(x *xorm.Engine) error {
	return x.Sync(
		new(licensePackage335),
		new(licenseKey335),
		new(licenseKeyUsage335),
	)
}
