# A. GENERATE PRODUCTION DATA

## Set seed for reproducibility
set.seed(123)

## Define base output
BaseOutput <- 50

## Create empty dataframe
production_data <- data.frame(
  Sol = integer(),
  Station = integer(),
  Team = integer(),
  Output = numeric()
)

## Nested loops to generate data
for (sol in 1:30) {
  for (station in 1:6) {
    for (team in 1:4) {
      
      ### Generate random noise between -5 and +5
      RandomNoise <- sample(-5:5, 1)
      
      ### Calculate output
      OutputValue <- BaseOutput + (station * 3) + (team * 2) + RandomNoise
      
      ### Append row to dataframe
      production_data <- rbind(
        production_data,
        data.frame(
          Sol = sol,
          Station = station,
          Team = team,
          Output = OutputValue
        )
      )
    }
  }
}


## Display first 10 rows
head(production_data, 10) 





# B. TOTAL PRODUCTION PER STATION

## Create empty dataframe
station_totals <- data.frame(
  Station = 1:6,
  TotalOutput = numeric(6)
)

## Loop to calculate totals
for (station in 1:6) {
  
  total_sum <- 0
  
  for (i in 1:nrow(production_data)) {
    if (production_data$Station[i] == station) {
      total_sum <- total_sum + production_data$Output[i]
    }
  }
  
  station_totals$TotalOutput[station] <- total_sum
}

## Display station totals
station_totals



# C. DETECT PRODUCTION ANOMALIES

## Create empty dataframe
anomalies <- data.frame(
  Sol = integer(),
  Station = integer(),
  Team = integer(),
  Output = numeric(),
  Anomaly = logical()
)

## Loop through sols and stations
for (sol in 1:30) {
  for (station in 1:6) {
    
    ### Calculate total for the day (for 4 teams)
    daily_total <- 0
    
    for (i in 1:nrow(production_data)) {
      if (production_data$Sol[i] == sol &&
          production_data$Station[i] == station) {
        daily_total <- daily_total + production_data$Output[i]
      }
    }
    
    ### Daily average
    daily_average <- daily_total / 4
    
    ### Check each team for anomaly
    for (i in 1:nrow(production_data)) {
      if (production_data$Sol[i] == sol &&
          production_data$Station[i] == station) {
        
        output_value <- production_data$Output[i]
        
        ### 10% deviation check
        if (abs(output_value - daily_average) > 0.10 * daily_average) {
          anomaly_flag <- TRUE
        } else {
          anomaly_flag <- FALSE
        }
        
        ### Append to anomalies dataframe
        anomalies <- rbind(
          anomalies,
          data.frame(
            Sol = sol,
            Station = station,
            Team = production_data$Team[i],
            Output = output_value,
            Anomaly = anomaly_flag
          )
        )
      }
    }
  }
}

## Display first 10 detected anomalies (TRUE only)
head(anomalies[anomalies$Anomaly == TRUE, ], 10)



# D. IDENTIFY PEAK PRODUCTIVITY DAYS

## Create empty dataframe
peak_days <- data.frame(
  Station = integer(),
  Sol = integer(),
  TotalOutput = numeric()
)

## Loop through stations
for (station in 1:6) {
  
  max_output <- -Inf
  max_sol <- 0
  
  for (sol in 1:30) {
    
    daily_total <- 0
    
    for (i in 1:nrow(production_data)) {
      if (production_data$Station[i] == station &&
          production_data$Sol[i] == sol) {
        daily_total <- daily_total + production_data$Output[i]
      }
    }
    
    ### Check if current day is maximum
    if (daily_total > max_output) {
      max_output <- daily_total
      max_sol <- sol
    }
  }
  
  ### Store result
  peak_days <- rbind(
    peak_days,
    data.frame(
      Station = station,
      Sol = max_sol,
      TotalOutput = max_output
    )
  )
}

## Display peak productivity days
peak_days



# E. CALCULATE EFFICIENCY SCORE

## Add new column
production_data$EfficiencyScore <- numeric(nrow(production_data))

for (i in 1:nrow(production_data)) {
  production_data$EfficiencyScore[i] <-
    production_data$Output[i] /
    (production_data$Station[i] + production_data$Team[i])
}

## Display first 10 rows
head(production_data, 10)

