package model

import (
	"encoding/json"
	"time"

	"gorm.io/gorm"
)

// AutomationRule represents an automation rule for inbox actions.
// Reference: Chatwoot AutomationRule model + P2B M6 spec
type AutomationRule struct {
	ID          uint           `gorm:"primaryKey" json:"id"`
	AccountID   uint           `gorm:"not null;index" json:"account_id"`
	InboxID     *uint          `gorm:"index" json:"inbox_id,omitempty"`
	Name        string         `gorm:"size:255;not null" json:"name"`
	Description string         `gorm:"type:text" json:"description"`
	EventType   string         `gorm:"size:100" json:"event_type"` // conversation_created/conversation_updated/message_created
	Conditions  json.RawMessage `gorm:"type:jsonb;not null" json:"conditions"`
	Active      bool           `gorm:"default:true" json:"active"`
	CreatedAt   time.Time      `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt   time.Time      `gorm:"autoUpdateTime" json:"updated_at"`
	DeletedAt   gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`

	Account   Account             `gorm:"foreignKey:AccountID" json:"account,omitempty"`
	Inbox     *Inbox              `gorm:"foreignKey:InboxID" json:"inbox,omitempty"`
	Actions   []AutomationAction  `gorm:"foreignKey:AutomationRuleID" json:"actions,omitempty"`
}

func (AutomationRule) TableName() string { return "automation_rules" }