package model

import "time"

// Project represents a managed CI/CD project
type Project struct {
	ID          uint      `gorm:"primaryKey" json:"id"`
	Name        string    `gorm:"uniqueIndex;not null" json:"name"`
	Description string    `json:"description"`
	Mode        string    `gorm:"default:'git'" json:"mode"` // "git" or "upload"

	// Git mode fields
	GitURL      string `json:"git_url"`
	GitBranch   string `gorm:"default:'master'" json:"git_branch"`
	GitUsername string `json:"git_username"`
	GitPassword string `json:"-"` // hidden from JSON responses

	// Build config
	BuildScript  string `json:"build_script"`
	OutputBinary string `json:"output_binary"` // relative path to built binary

	// Runtime config
	RunCommand string `json:"run_command"` // command to run the project
	EnvVars    string `json:"env_vars"`    // JSON key-value env vars
	Port       int    `json:"port"`        // target port

	// Status
	Status string `json:"status"` // "stopped", "running", "building", "error"
	PID    int    `json:"pid"`

	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

// BuildRecord represents a single build execution
type BuildRecord struct {
	ID         uint       `gorm:"primaryKey" json:"id"`
	ProjectID  uint       `gorm:"index;not null" json:"project_id"`
	Project    *Project   `gorm:"foreignKey:ProjectID" json:"project,omitempty"`
	Status     string     `gorm:"default:'running'" json:"status"` // "running", "success", "failed"
	LogPath    string     `json:"log_path"`                        // path to log file
	CommitHash string     `json:"commit_hash"`                     // git commit hash (if git mode)
	StartedAt  time.Time  `json:"started_at"`
	FinishedAt *time.Time `json:"finished_at"`
}

// DeployConfig holds SSH deployment configuration for a project.
type DeployConfig struct {
	ID        uint   `gorm:"primaryKey" json:"id"`
	ProjectID uint   `gorm:"uniqueIndex;not null" json:"project_id"`

	// SSH connection
	Host     string `json:"host"`
	Port     int    `gorm:"default:22" json:"port"`
	Username string `json:"username"`
	AuthMethod string `gorm:"default:'key'" json:"auth_method"` // "key" or "password"
	SSHKey      string `json:"-"` // hidden from JSON responses
	SSHPassword string `json:"-"` // hidden from JSON responses

	// Deployment settings
	DeployDir    string `json:"deploy_dir"` // remote target directory
	FileMappings string `json:"file_mappings"` // JSON: [{"local":"app","remote":"/opt/myapp/app"}]

	PreDeployCommand  string `json:"pre_deploy_command"`
	PostDeployCommand string `json:"post_deploy_command"`

	// Health check
	HealthCheckURL     string `json:"health_check_url"`
	HealthCheckTimeout int    `gorm:"default:30" json:"health_check_timeout"`

	// Auto-deploy after successful build
	AutoDeploy bool `gorm:"default:false" json:"auto_deploy"`

	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

// DeployRecord tracks a single deployment execution.
type DeployRecord struct {
	ID            uint       `gorm:"primaryKey" json:"id"`
	ProjectID     uint       `gorm:"index;not null" json:"project_id"`
	Project       *Project   `gorm:"foreignKey:ProjectID" json:"project,omitempty"`
	BuildRecordID *uint      `json:"build_record_id"` // optional link to the build that triggered this deploy
	Status        string     `gorm:"default:'running'" json:"status"` // "running", "success", "failed"
	LogPath       string     `json:"log_path"`
	StartedAt     time.Time  `json:"started_at"`
	FinishedAt    *time.Time `json:"finished_at"`
}
