package models

import "time"

// Consultation is the astrologer side's view of the `consultations` table. It
// is the same table the user side bills into (models/usermodel/consultation.go)
// — the duplication across the two stacks is the established pattern here.
//
// The astrologer side only ever reads it: what a session earned, and where
// that earning is in the settlement lifecycle. Nothing in this stack bills a
// session or moves a settlement status.
type Consultation struct {
	ID             uint   `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
	ConsultationNo string `gorm:"column:consultationNo" json:"consultationNo"`

	UserID       uint `gorm:"column:userId" json:"userId"`
	AstrologerID uint `gorm:"column:astrologerId" json:"astrologerId"`

	Medium    string `gorm:"column:medium" json:"medium"`
	Status    string `gorm:"column:status" json:"status"`
	EndReason string `gorm:"column:endReason" json:"endReason"`

	RatePerMinute float64 `gorm:"column:ratePerMinute" json:"ratePerMinute"`

	// The live-session columns. This side stays read-only — the astrologer
	// app's accept and reject go through the user-side consultation service,
	// which owns every write — but without these the astrologer app cannot
	// render an incoming request or a running countdown at all.
	ChannelName string `gorm:"column:channelName" json:"channelName"`

	FreeMinutes int `gorm:"column:freeMinutes" json:"freeMinutes"`
	FreeSeconds int `gorm:"column:freeSeconds" json:"freeSeconds"`

	RequestedAt *time.Time `gorm:"column:requestedAt" json:"requestedAt,omitempty"`
	AcceptedAt  *time.Time `gorm:"column:acceptedAt" json:"acceptedAt,omitempty"`
	StartedAt   *time.Time `gorm:"column:startedAt" json:"startedAt,omitempty"`
	EndedAt     *time.Time `gorm:"column:endedAt" json:"endedAt,omitempty"`

	LastTickAt *time.Time `gorm:"column:lastTickAt" json:"lastTickAt,omitempty"`
	WarnedAt   *time.Time `gorm:"column:warnedAt" json:"warnedAt,omitempty"`

	MaxBillableSeconds int `gorm:"column:maxBillableSeconds" json:"maxBillableSeconds"`

	DurationSeconds int `gorm:"column:durationSeconds" json:"durationSeconds"`
	BilledMinutes   int `gorm:"column:billedMinutes" json:"billedMinutes"`

	GrossAmount        float64 `gorm:"column:grossAmount" json:"grossAmount"`
	PlatformFeePercent float64 `gorm:"column:platformFeePercent" json:"platformFeePercent"`
	PlatformFeeAmount  float64 `gorm:"column:platformFeeAmount" json:"platformFeeAmount"`
	AstrologerEarning  float64 `gorm:"column:astrologerEarning" json:"astrologerEarning"`

	SettlementStatus string     `gorm:"column:settlementStatus" json:"settlementStatus"`
	SettlementID     *uint      `gorm:"column:settlementId" json:"settlementId,omitempty"`
	SettledAt        *time.Time `gorm:"column:settledAt" json:"settledAt,omitempty"`

	Name string `gorm:"column:name" json:"name"`

	IsDelete  bool       `gorm:"column:isDelete" json:"isDelete"`
	CreatedAt *time.Time `gorm:"column:created_at" json:"createdAt,omitempty"`
	UpdatedAt *time.Time `gorm:"column:updated_at" json:"updatedAt,omitempty"`
}

func (Consultation) TableName() string {
	return "consultations"
}
