package seatbilling

import "time"

const (
	PlanKeyTeam       = "team"
	PlanKeyEnterprise = "enterprise"
	PlanKeyMax        = "max"

	BillingPeriodMonth   = "month"
	BillingPeriodQuarter = "quarter"
	BillingPeriodYear    = "year"

	PlanStatusActive   = "active"
	PlanStatusInactive = "inactive"

	SubscriptionStatusActive    = "active"
	SubscriptionStatusExpired   = "expired"
	SubscriptionStatusSuspended = "suspended"
	SubscriptionStatusCancelled = "cancelled"

	OrderTypeNewSubscription   = "new_subscription"
	OrderTypeAddSeats          = "add_seats"
	OrderTypeUpgradeTier       = "upgrade_tier"
	OrderTypeRenewSubscription = "renew_subscription"

	OrderStatusPending       = "pending"
	OrderStatusPaid          = "paid"
	OrderStatusCancelled     = "cancelled"
	OrderStatusExpired       = "expired"
	OrderStatusFailed        = "failed"
	OrderStatusPaidUnapplied = "paid_unapplied"

	PaymentProviderAlipay = "alipay"

	PaymentStatusPending    = "pending"
	PaymentStatusProcessing = "processing"
	PaymentStatusPaid       = "paid"
	PaymentStatusFailed     = "failed"
	PaymentStatusClosed     = "closed"

	CurrencyCNY = "CNY"

	FreeHumanSeatLimit           = int64(20)
	FreeAgentsPerHumanOwnerLimit = int64(10)

	SeatOrderPaymentTTL = 30 * time.Minute
)

type SeatPlan struct {
	ID                   string
	PlanKey              string
	Name                 string
	BillingPeriod        string
	UnitPriceCents       int64
	Currency             string
	ManagedAgentsPerSeat int64
	MinSeatQuantity      int
	MaxSeatQuantity      int
	SelfServiceEnabled   bool
	Status               string
	MetadataJSON         string
	CreatedAt            time.Time
	UpdatedAt            time.Time
}

type SeatSubscription struct {
	OrganizationID               string
	Status                       string
	PlanKey                      string
	BillingPeriod                string
	SeatQuantity                 int
	ManagedAgentsPerSeatSnapshot int64
	MaxHumanSeatsSnapshot        int
	UnitPriceCentsSnapshot       int64
	Currency                     string
	CurrentPeriodStart           time.Time
	CurrentPeriodEnd             time.Time
	CreatedAt                    time.Time
	UpdatedAt                    time.Time
}

type SeatEntitlement struct {
	OrganizationID         string
	PlanKey                string
	BillingPeriod          string
	HumanSeatLimit         int64
	FreeHumanSeatLimit     int64
	PaidHumanSeatLimit     int64
	GiftHumanSeatLimit     int64
	NextGrantExpiresAt     *time.Time
	OrganizationAgentLimit int64
	ManagedAgentsPerSeat   int64
	CurrentPeriodStart     *time.Time
	CurrentPeriodEnd       *time.Time
	Paid                   bool
	SubscriptionStatus     string
}

type SeatSummary struct {
	Entitlement         SeatEntitlement
	CurrentHumanSeats   int64
	CurrentAgents       int64
	HumanSeatsOverLimit bool
	AgentsOverLimit     bool
}

type SeatOrder struct {
	ID                           string
	OrganizationID               string
	OrderType                    string
	Status                       string
	FromPlanKey                  *string
	ToPlanKey                    *string
	BillingPeriod                string
	SeatQuantityBefore           int
	SeatQuantityAfter            int
	SeatDelta                    int
	CurrentPeriodStartSnapshot   *time.Time
	CurrentPeriodEndSnapshot     *time.Time
	UnitPriceCentsSnapshot       int64
	TargetUnitPriceCentsSnapshot int64
	BaseAmountCents              int64
	ProrationSnapshotJSON        string
	AmountCents                  int64
	Currency                     string
	EntitlementSnapshotJSON      string
	IdempotencyKey               string
	ExpiresAt                    time.Time
	CreatedByUserID              string
	CancelledByUserID            *string
	SettledAt                    *time.Time
	AppliedAt                    *time.Time
	CreatedAt                    time.Time
	UpdatedAt                    time.Time
}

type SeatPaymentOrder struct {
	ID              string
	OrganizationID  string
	SeatOrderID     string
	Provider        string
	ProviderOrderID string
	AmountCents     int64
	Currency        string
	Status          string
	PaidAt          *time.Time
	ClosedAt        *time.Time
	RawPayloadJSON  string
	CreatedAt       time.Time
	UpdatedAt       time.Time
}

type SeatOrderWithPayment struct {
	Order   SeatOrder
	Payment SeatPaymentOrder
}

type SeatPaymentHistoryRow struct {
	Order   SeatOrder
	Payment SeatPaymentOrder
}
