package worktree

import (
	"time"
)

// ID is the branch name of a worktree
type ID string

// Name is the human-readable name (same as branch name)
type Name string

// Identifier can be either ID or Name (both are branch names)
type Identifier string

// ConsistencyStatus represents the consistency state of a worktree
type ConsistencyStatus int

const (
	// StatusConsistent indicates both folder and git worktree exist
	StatusConsistent ConsistencyStatus = iota
	// StatusFolderMissing indicates git worktree exists but folder is missing
	StatusFolderMissing
	// StatusWorktreeMissing indicates folder exists but git worktree is missing
	StatusWorktreeMissing
	// StatusOrphaned indicates both folder and git worktree are missing
	StatusOrphaned
)

// String returns the string representation of ConsistencyStatus
func (s ConsistencyStatus) String() string {
	switch s {
	case StatusConsistent:
		return "consistent"
	case StatusFolderMissing:
		return "folder-missing"
	case StatusWorktreeMissing:
		return "worktree-missing"
	case StatusOrphaned:
		return "orphaned"
	default:
		return "unknown"
	}
}

// MarshalJSON implements json.Marshaler for ConsistencyStatus
func (s ConsistencyStatus) MarshalJSON() ([]byte, error) {
	return []byte(`"` + s.String() + `"`), nil
}

// UnmarshalJSON implements json.Unmarshaler for ConsistencyStatus
func (s *ConsistencyStatus) UnmarshalJSON(data []byte) error {
	str := string(data)
	// Remove quotes
	if len(str) >= 2 && str[0] == '"' && str[len(str)-1] == '"' {
		str = str[1 : len(str)-1]
	}

	switch str {
	case "consistent":
		*s = StatusConsistent
	case "folder-missing":
		*s = StatusFolderMissing
	case "worktree-missing":
		*s = StatusWorktreeMissing
	case "orphaned":
		*s = StatusOrphaned
	default:
		*s = StatusConsistent // Default to consistent if unknown
	}
	return nil
}

// Worktree represents an isolated development environment
type Worktree struct {
	Branch      string    `yaml:"branch" json:"branch"` // Branch name (used as ID)
	BaseBranch  string    `yaml:"baseBranch" json:"baseBranch"`
	Path        string    `yaml:"path" json:"path"` // Path to the worktree
	Description string    `yaml:"description,omitempty" json:"description,omitempty"`
	CreatedAt   time.Time `yaml:"createdAt" json:"createdAt"`
	UpdatedAt   time.Time `yaml:"-" json:"updatedAt"` // Dynamically populated from filesystem
	AutoCreated bool      `yaml:"autoCreated,omitempty" json:"autoCreated,omitempty"`

	// Consistency status fields (not persisted)
	PathExists     bool              `yaml:"-" json:"pathExists"`
	WorktreeExists bool              `yaml:"-" json:"worktreeExists"`
	Status         ConsistencyStatus `yaml:"-" json:"status"`
}

// BranchMode specifies how to handle branch creation/checkout
type BranchMode int

const (
	// BranchModeCreate creates a new branch (default)
	BranchModeCreate BranchMode = iota
	// BranchModeCheckout uses an existing branch
	BranchModeCheckout
)

// CreateOptions represents options for creating a new worktree
type CreateOptions struct {
	Name        string
	BaseBranch  string
	Branch      string     // Branch name (either new or existing)
	BranchMode  BranchMode // How to handle the branch (default: BranchModeCreate)
	Description string
	AutoCreated bool // Internal: whether worktree was auto-created by session
	NoHooks     bool // Skip hook execution
}

// ListOptions represents options for listing worktrees
type ListOptions struct {
	// Reserved for future filtering options
}

// RemoveOptions represents options for removing a worktree
type RemoveOptions struct {
	NoHooks         bool   // Skip hook execution
	CurrentDir      string // Current working directory (for safety check)
	SkipSafetyCheck bool   // Skip current directory safety check
}

// CleanupOptions represents options for cleaning up old worktrees
type CleanupOptions struct {
	Days   int
	DryRun bool
}
