package models

import (
	"encoding/json"
	"time"
)

// ── Trigger Type Constants ───────────────────

const (
	TriggerTypeWebhook = "webhook"
	TriggerTypeEvent   = "event"
)

// ── Scheduled Task Run-As Constants ──────────

const (
	RunAsCreator = "creator"
	RunAsSystem  = "system"
)

// ── Trigger (extension-declared) ─────────────

// Trigger represents an extension-declared event or webhook trigger.
type Trigger struct {
	ID           string          `json:"id" db:"id"`
	PackageID    string          `json:"package_id" db:"package_id"`
	Type         string          `json:"type" db:"type"`
	Enabled      bool            `json:"enabled" db:"enabled"`
	Slug         string          `json:"slug,omitempty" db:"slug"`
	Secret       string          `json:"-" db:"secret"` // never serialized
	EventPattern string          `json:"event_pattern,omitempty" db:"event_pattern"`
	EntryPoint   string          `json:"entry_point" db:"entry_point"`
	Config       json.RawMessage `json:"config" db:"config"`
	FireCount    int             `json:"fire_count" db:"fire_count"`
	LastFireAt   *time.Time      `json:"last_fire_at,omitempty" db:"last_fire_at"`
	LastError    string          `json:"last_error,omitempty" db:"last_error"`
	CreatedAt    time.Time       `json:"created_at" db:"created_at"`
	UpdatedAt    time.Time       `json:"updated_at" db:"updated_at"`
}

// ── Scheduled Task (user-created) ────────────

// ScheduledTask represents a user-created cron-scheduled Starlark script.
type ScheduledTask struct {
	ID             string          `json:"id" db:"id"`
	Name           string          `json:"name" db:"name"`
	Description    string          `json:"description" db:"description"`
	CreatorID      string          `json:"creator_id" db:"creator_id"`
	RunAs          string          `json:"run_as" db:"run_as"`
	CronExpr       string          `json:"cron_expr" db:"cron_expr"`
	NextFireAt     *time.Time      `json:"next_fire_at,omitempty" db:"next_fire_at"`
	LastFireAt     *time.Time      `json:"last_fire_at,omitempty" db:"last_fire_at"`
	Enabled        bool            `json:"enabled" db:"enabled"`
	Script         string          `json:"script" db:"script"`
	TemplateID     string          `json:"template_id,omitempty" db:"template_id"`
	TemplateParams json.RawMessage `json:"template_params" db:"template_params"`
	FireCount      int             `json:"fire_count" db:"fire_count"`
	LastError      string          `json:"last_error,omitempty" db:"last_error"`
	LastDurationMs *int            `json:"last_duration_ms,omitempty" db:"last_duration_ms"`
	CreatedAt      time.Time       `json:"created_at" db:"created_at"`
	UpdatedAt      time.Time       `json:"updated_at" db:"updated_at"`
}

// ── Trigger Log (unified) ────────────────────

// TriggerLog records a single execution of a trigger or scheduled task.
type TriggerLog struct {
	ID              string `json:"id" db:"id"`
	TriggerID       string `json:"trigger_id,omitempty" db:"trigger_id"`
	ScheduledTaskID string `json:"scheduled_task_id,omitempty" db:"scheduled_task_id"`
	FiredAt         string `json:"fired_at" db:"fired_at"`
	DurationMs      *int   `json:"duration_ms,omitempty" db:"duration_ms"`
	Success         bool   `json:"success" db:"success"`
	Error           string `json:"error,omitempty" db:"error"`
	Output          string `json:"output,omitempty" db:"output"`
}
