package model

import (
	"time"

	"gorm.io/gorm"
)

// PackageSeries 套餐系列模型
// 套餐的分组,用于一次性分佣规则配置
type PackageSeries struct {
	gorm.Model
	BaseModel   `gorm:"embedded"`
	SeriesCode  string `gorm:"column:series_code;type:varchar(100);uniqueIndex:idx_package_series_code,where:deleted_at IS NULL;not null;comment:系列编码" json:"series_code"`
	SeriesName  string `gorm:"column:series_name;type:varchar(255);not null;comment:系列名称" json:"series_name"`
	Description string `gorm:"column:description;type:text;comment:描述" json:"description"`
	Status      int    `gorm:"column:status;type:int;default:1;not null;comment:状态 1-启用 2-禁用" json:"status"`
}

// TableName 指定表名
func (PackageSeries) TableName() string {
	return "tb_package_series"
}

// Package 套餐模型
// 只适用于 IoT 卡,支持真流量/虚流量共存机制
type Package struct {
	gorm.Model
	BaseModel            `gorm:"embedded"`
	PackageCode          string `gorm:"column:package_code;type:varchar(100);uniqueIndex:idx_package_code,where:deleted_at IS NULL;not null;comment:套餐编码" json:"package_code"`
	PackageName          string `gorm:"column:package_name;type:varchar(255);not null;comment:套餐名称" json:"package_name"`
	SeriesID             uint   `gorm:"column:series_id;index;comment:套餐系列ID" json:"series_id"`
	PackageType          string `gorm:"column:package_type;type:varchar(50);not null;comment:套餐类型 formal-正式套餐 addon-附加套餐" json:"package_type"`
	DurationMonths       int    `gorm:"column:duration_months;type:int;not null;comment:套餐时长(月数) 1-月套餐 12-年套餐" json:"duration_months"`
	DataType             string `gorm:"column:data_type;type:varchar(20);comment:流量类型 real-真流量 virtual-虚流量" json:"data_type"`
	RealDataMB           int64  `gorm:"column:real_data_mb;type:bigint;default:0;comment:真流量额度(MB)" json:"real_data_mb"`
	VirtualDataMB        int64  `gorm:"column:virtual_data_mb;type:bigint;default:0;comment:虚流量额度(MB,用于停机判断)" json:"virtual_data_mb"`
	DataAmountMB         int64  `gorm:"column:data_amount_mb;type:bigint;default:0;comment:总流量额度(MB)" json:"data_amount_mb"`
	Price                int64  `gorm:"column:price;type:bigint;not null;comment:套餐价格(分为单位)" json:"price"`
	Status               int    `gorm:"column:status;type:int;default:1;not null;comment:状态 1-启用 2-禁用" json:"status"`
	SuggestedCostPrice   int64  `gorm:"column:suggested_cost_price;type:bigint;default:0;comment:建议成本价(分为单位)" json:"suggested_cost_price"`
	SuggestedRetailPrice int64  `gorm:"column:suggested_retail_price;type:bigint;default:0;comment:建议售价(分为单位)" json:"suggested_retail_price"`
	ShelfStatus          int    `gorm:"column:shelf_status;type:int;default:2;not null;comment:上架状态 1-上架 2-下架" json:"shelf_status"`
}

// TableName 指定表名
func (Package) TableName() string {
	return "tb_package"
}

// PackageUsage 套餐使用情况模型
// 跟踪单卡套餐和设备级套餐的流量使用
type PackageUsage struct {
	gorm.Model
	BaseModel          `gorm:"embedded"`
	OrderID            uint       `gorm:"column:order_id;index;not null;comment:订单ID" json:"order_id"`
	PackageID          uint       `gorm:"column:package_id;index;not null;comment:套餐ID" json:"package_id"`
	UsageType          string     `gorm:"column:usage_type;type:varchar(20);not null;comment:使用类型 single_card-单卡套餐 device-设备级套餐" json:"usage_type"`
	IotCardID          uint       `gorm:"column:iot_card_id;index;comment:IoT卡ID(单卡套餐时有值)" json:"iot_card_id"`
	DeviceID           uint       `gorm:"column:device_id;index;comment:设备ID(设备级套餐时有值)" json:"device_id"`
	DataLimitMB        int64      `gorm:"column:data_limit_mb;type:bigint;not null;comment:流量限额(MB)" json:"data_limit_mb"`
	DataUsageMB        int64      `gorm:"column:data_usage_mb;type:bigint;default:0;comment:已使用流量(MB)" json:"data_usage_mb"`
	RealDataUsageMB    int64      `gorm:"column:real_data_usage_mb;type:bigint;default:0;comment:真流量使用(MB)" json:"real_data_usage_mb"`
	VirtualDataUsageMB int64      `gorm:"column:virtual_data_usage_mb;type:bigint;default:0;comment:虚流量使用(MB)" json:"virtual_data_usage_mb"`
	ActivatedAt        time.Time  `gorm:"column:activated_at;not null;comment:套餐生效时间" json:"activated_at"`
	ExpiresAt          time.Time  `gorm:"column:expires_at;not null;comment:套餐过期时间" json:"expires_at"`
	Status             int        `gorm:"column:status;type:int;default:1;not null;comment:状态 1-生效中 2-已用完 3-已过期" json:"status"`
	LastPackageCheckAt *time.Time `gorm:"column:last_package_check_at;comment:最后一次套餐流量检查时间" json:"last_package_check_at"`
}

// TableName 指定表名
func (PackageUsage) TableName() string {
	return "tb_package_usage"
}
