// Copyright 2026 The MokoGitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package v1_27

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

	"xorm.io/xorm"
)

type aiOrgSetting struct {
	ID                   int64  `xorm:"pk autoincr"`
	OrgID                int64  `xorm:"UNIQUE NOT NULL"`
	Enabled              bool   `xorm:"NOT NULL DEFAULT true"`
	APIKeyEncrypted      string `xorm:"TEXT"`
	Model                string `xorm:"VARCHAR(50) NOT NULL DEFAULT 'claude-sonnet-4-6'"`
	RateLimitRequests    int    `xorm:"NOT NULL DEFAULT 100"`
	RateLimitTokensMonth int64  `xorm:"NOT NULL DEFAULT 5000000"`
	CreatedUnix          timeutil.TimeStamp
	UpdatedUnix          timeutil.TimeStamp
}

func (aiOrgSetting) TableName() string {
	return "ai_org_setting"
}

type aiRepoSetting struct {
	ID             int64  `xorm:"pk autoincr"`
	RepoID         int64  `xorm:"UNIQUE NOT NULL"`
	Enabled        bool   `xorm:"NOT NULL DEFAULT true"`
	AutoReview     bool   `xorm:"NOT NULL DEFAULT true"`
	Strictness     string `xorm:"VARCHAR(20) NOT NULL DEFAULT 'standard'"`
	IgnorePatterns string `xorm:"TEXT"`
	CreatedUnix    timeutil.TimeStamp
	UpdatedUnix    timeutil.TimeStamp
}

func (aiRepoSetting) TableName() string {
	return "ai_repo_setting"
}

type aiUsageLog struct {
	ID            int64  `xorm:"pk autoincr"`
	OrgID         int64  `xorm:"INDEX NOT NULL"`
	RepoID        int64  `xorm:"INDEX NOT NULL"`
	TriggeredByID int64
	ActionType    string `xorm:"VARCHAR(20) NOT NULL"`
	Model         string `xorm:"VARCHAR(50)"`
	TokensInput   int64
	TokensOutput  int64
	DurationMs    int64
	CreatedUnix   timeutil.TimeStamp `xorm:"INDEX"`
}

func (aiUsageLog) TableName() string {
	return "ai_usage_log"
}

func AddAITables(x *xorm.Engine) error {
	return x.Sync(new(aiOrgSetting), new(aiRepoSetting), new(aiUsageLog))
}
