// Copyright 2026 MarketAlly. All rights reserved.
// SPDX-License-Identifier: MIT

package structs

import "time"

// BandwidthInfo holds network bandwidth test results
type BandwidthInfo struct {
	DownloadMbps float64   `json:"download_mbps"`
	UploadMbps   float64   `json:"upload_mbps,omitempty"`
	Latency      float64   `json:"latency_ms,omitempty"`
	TestedAt     time.Time `json:"tested_at"`
}

// DiskInfo holds disk space information for a runner
type DiskInfo struct {
	Total       uint64  `json:"total_bytes"`
	Free        uint64  `json:"free_bytes"`
	Used        uint64  `json:"used_bytes"`
	UsedPercent float64 `json:"used_percent"`
}

// DistroInfo holds Linux distribution information
type DistroInfo struct {
	ID         string `json:"id,omitempty"`          // e.g., "ubuntu", "debian", "fedora"
	VersionID  string `json:"version_id,omitempty"`  // e.g., "24.04", "12"
	PrettyName string `json:"pretty_name,omitempty"` // e.g., "Ubuntu 24.04 LTS"
}

// XcodeInfo holds Xcode-specific information for macOS runners
type XcodeInfo struct {
	Version    string   `json:"version,omitempty"`
	Build      string   `json:"build,omitempty"`
	SDKs       []string `json:"sdks,omitempty"`
	Simulators []string `json:"simulators,omitempty"`
}

// RunnerCapability represents the detailed capabilities of a runner
type RunnerCapability struct {
	OS               string              `json:"os"`
	Arch             string              `json:"arch"`
	Distro           *DistroInfo         `json:"distro,omitempty"`
	Xcode            *XcodeInfo          `json:"xcode,omitempty"`
	Docker           bool                `json:"docker"`
	DockerCompose    bool                `json:"docker_compose"`
	ContainerRuntime string              `json:"container_runtime,omitempty"`
	Shell            []string            `json:"shell,omitempty"`
	Tools            map[string][]string `json:"tools,omitempty"`
	BuildTools       []string            `json:"build_tools,omitempty"`
	PackageManagers  []string            `json:"package_managers,omitempty"`
	Features         *CapabilityFeatures `json:"features,omitempty"`
	Limitations      []string            `json:"limitations,omitempty"`
	Disk             *DiskInfo           `json:"disk,omitempty"`
	Bandwidth        *BandwidthInfo      `json:"bandwidth,omitempty"`
	SuggestedLabels  []string            `json:"suggested_labels,omitempty"`
}

// CapabilityFeatures represents feature support flags
type CapabilityFeatures struct {
	ArtifactsV4      bool `json:"artifacts_v4"`
	Cache            bool `json:"cache"`
	Services         bool `json:"services"`
	CompositeActions bool `json:"composite_actions"`
}

// ActionSupport represents version support for an action
type ActionSupport struct {
	Versions []string `json:"versions"`
	Notes    string   `json:"notes,omitempty"`
}

// PlatformInfo represents Gitea platform capabilities
type PlatformInfo struct {
	Type                string                   `json:"type"`
	Version             string                   `json:"version"`
	ActionsVersion      string                   `json:"actions_version,omitempty"`
	DefaultActionsURL   string                   `json:"default_actions_url"`
	SupportedActions    map[string]ActionSupport `json:"supported_actions,omitempty"`
	UnsupportedFeatures []string                 `json:"unsupported_features,omitempty"`
}

// WorkflowHints provides hints for AI workflow generation
type WorkflowHints struct {
	PreferredCheckout         string `json:"preferred_checkout,omitempty"`
	ArtifactUploadAlternative string `json:"artifact_upload_alternative,omitempty"`
	SecretAccess              string `json:"secret_access,omitempty"`
}

// RunnerWithCapabilities represents a runner with its capabilities for API response
type RunnerWithCapabilities struct {
	ID           int64             `json:"id"`
	Name         string            `json:"name"`
	Status       string            `json:"status"`
	Labels       []string          `json:"labels"`
	Capabilities *RunnerCapability `json:"capabilities,omitempty"`
}

// ActionsCapabilitiesResponse is the response for the capabilities endpoint
type ActionsCapabilitiesResponse struct {
	Runners       []*RunnerWithCapabilities `json:"runners"`
	Platform      *PlatformInfo             `json:"platform"`
	WorkflowHints *WorkflowHints            `json:"workflow_hints,omitempty"`
}

// WorkflowValidationRequest is the request for workflow validation
type WorkflowValidationRequest struct {
	Content string `json:"content" binding:"Required"`
}

// WorkflowValidationWarning represents a validation warning
type WorkflowValidationWarning struct {
	Line       int    `json:"line,omitempty"`
	Action     string `json:"action,omitempty"`
	Severity   string `json:"severity"`
	Message    string `json:"message"`
	Suggestion string `json:"suggestion,omitempty"`
}

// RunnerMatch represents job-to-runner matching result
type RunnerMatch struct {
	Job             string   `json:"job"`
	RunsOn          []string `json:"runs_on"`
	MatchedRunners  []string `json:"matched_runners,omitempty"`
	CapabilitiesMet bool     `json:"capabilities_met"`
}

// WorkflowValidationResponse is the response for workflow validation
type WorkflowValidationResponse struct {
	Valid       bool                         `json:"valid"`
	Warnings    []*WorkflowValidationWarning `json:"warnings,omitempty"`
	RunnerMatch []*RunnerMatch               `json:"runner_match,omitempty"`
}
