package iace

import (
	"encoding/json"
	"time"

	"github.com/google/uuid"
)

// ============================================================================
// Production Line Entities
// ============================================================================

// ProductionLine represents a chain of IACE projects forming a production line
type ProductionLine struct {
	ID          uuid.UUID       `json:"id"`
	TenantID    uuid.UUID       `json:"tenant_id"`
	Name        string          `json:"name"`
	Description string          `json:"description,omitempty"`
	Layout      json.RawMessage `json:"layout,omitempty"`
	CreatedAt   time.Time       `json:"created_at"`
	UpdatedAt   time.Time       `json:"updated_at"`
}

// ProductionLineStation links a production line to an IACE project as a station
type ProductionLineStation struct {
	ID           uuid.UUID       `json:"id"`
	LineID       uuid.UUID       `json:"line_id"`
	ProjectID    uuid.UUID       `json:"project_id"`
	StationType  string          `json:"station_type"`
	StationLabel string          `json:"station_label,omitempty"`
	SortOrder    int             `json:"sort_order"`
	PositionX    float64         `json:"position_x"`
	PositionY    float64         `json:"position_y"`
	Metadata     json.RawMessage `json:"metadata,omitempty"`
	CreatedAt    time.Time       `json:"created_at"`
}

// ============================================================================
// Dashboard Aggregation Types
// ============================================================================

// StationDashboard contains the per-station risk and compliance summary
type StationDashboard struct {
	Station         ProductionLineStation `json:"station"`
	ProjectName     string                `json:"project_name"`
	MachineType     string                `json:"machine_type"`
	Status          string                `json:"status"`
	RiskSummary     map[string]int        `json:"risk_summary"`
	HazardCount     int                   `json:"hazard_count"`
	MitigationCount int                   `json:"mitigation_count"`
	CompletenessPct int                   `json:"completeness_pct"`
	SILMax          string                `json:"sil_max"`
	PLMax           string                `json:"pl_max"`
}

// LineDashboard aggregates all stations for a production line overview
type LineDashboard struct {
	Line      ProductionLine     `json:"line"`
	Stations  []StationDashboard `json:"stations"`
	Transfers []TransferInfo     `json:"transfers"`
	Aggregate map[string]int     `json:"aggregate"`
}

// TransferInfo describes a material/data flow between adjacent stations
type TransferInfo struct {
	FromStation int    `json:"from_station"`
	ToStation   int    `json:"to_station"`
	Type        string `json:"type"`
	Label       string `json:"label"`
}

// ============================================================================
// API Request/Response Types
// ============================================================================

// CreateProductionLineRequest is the API request for creating a production line
type CreateProductionLineRequest struct {
	Name        string `json:"name" binding:"required"`
	Description string `json:"description,omitempty"`
}

// AddStationRequest is the API request for adding a station to a production line
type AddStationRequest struct {
	ProjectID    uuid.UUID `json:"project_id" binding:"required"`
	StationType  string    `json:"station_type,omitempty"`
	StationLabel string    `json:"station_label,omitempty"`
	SortOrder    int       `json:"sort_order"`
}

// ProductionLineListResponse is the API response for listing production lines
type ProductionLineListResponse struct {
	Lines []ProductionLine `json:"lines"`
	Total int              `json:"total"`
}
