// Package model provides TUI data structures and state management
package model

import (
	"fmt"

	"github.com/charmbracelet/lipgloss"
)

// JobStatus represents the status of a job
type JobStatus string

// JobStatus constants represent different job states
const (
	StatusPending  JobStatus = "pending"  // Job is pending
	StatusQueued   JobStatus = "queued"   // Job is queued
	StatusRunning  JobStatus = "running"  // Job is running
	StatusFinished JobStatus = "finished" // Job is finished
	StatusFailed   JobStatus = "failed"   // Job is failed
)

// Job represents a job in the TUI
type Job struct {
	Name     string
	Status   JobStatus
	TaskID   string
	Priority int64
	// Narrative fields for research context
	Hypothesis      string
	Context         string
	Intent          string
	ExpectedOutcome string
	ActualOutcome   string
	OutcomeStatus   string // validated, invalidated, inconclusive
	// GPU allocation tracking
	GPUDeviceID    int   // -1 if not assigned
	GPUUtilization int   // 0-100%
	GPUMemoryUsed  int64 // MB
}

// Title returns the job title for display
func (j Job) Title() string { return j.Name }

// Description returns a formatted description with status icon and GPU info
func (j Job) Description() string {
	icon := map[JobStatus]string{
		StatusPending:  "⏸",
		StatusQueued:   "⏳",
		StatusRunning:  "▶",
		StatusFinished: "✓",
		StatusFailed:   "✗",
	}[j.Status]
	pri := ""
	if j.Priority > 0 {
		pri = fmt.Sprintf(" [P%d]", j.Priority)
	}
	gpu := ""
	if j.GPUDeviceID >= 0 {
		gpu = fmt.Sprintf(" [GPU:%d %d%%]", j.GPUDeviceID, j.GPUUtilization)
	}

	// Apply status color to the status text
	statusStyle := lipgloss.NewStyle().Foreground(StatusColor(j.Status))
	coloredStatus := statusStyle.Render(string(j.Status))

	return fmt.Sprintf("%s %s%s%s", icon, coloredStatus, pri, gpu)
}

// FilterValue returns the value used for filtering
func (j Job) FilterValue() string { return j.Name }
