#!/bin/bash

# HA Bulk Light Operation Script
# Steps: 1) Set lights to 3500K @ 50%, 2) Wait 10s, 3) Turn off all lights, 4) Turn off switches

HA_API="${HA_URL}/api"
HEADERS=(-H "Authorization: Bearer ${HA_TOKEN}" -H "Content-Type: application/json")

echo "=== HA Bulk Light Operation ==="
echo "HA URL: $HA_URL"
echo ""

# Step 1: Get all light entities and filter
echo "Step 1: Fetching all light entities..."
LIGHTS_JSON=$(curl -s "${HA_API}/states" "${HEADERS[@]}" | jq -r '[.[] | select(.entity_id | startswith("light.")) | .entity_id]')

# Count total lights
TOTAL_LIGHTS=$(echo "$LIGHTS_JSON" | jq 'length')
echo "Found $TOTAL_LIGHTS light entities"

# Filter out excluded lights
# Exclusions: screens, athom, server LEDs, coffee/cooker/microwave/steam, browser_mod
FILTERED_LIGHTS=$(echo "$LIGHTS_JSON" | jq -r '[.[] | 
  select(
    (. | contains("screen") | not) and
    (. | contains("athom") | not) and
    (. | contains("server") | not) and
    (. | contains("coffee") | not) and
    (. | contains("cooker") | not) and
    (. | contains("microwave") | not) and
    (. | contains("steam") | not) and
    (. | contains("browser_mod") | not)
  )
]')

FILTERED_COUNT=$(echo "$FILTERED_LIGHTS" | jq 'length')
echo "After filtering: $FILTERED_COUNT lights to control"
echo ""

# Convert to array for processing
LIGHTS_ARRAY=($(echo "$FILTERED_LIGHTS" | jq -r '.[]'))

echo "Step 1a: Setting $FILTERED_COUNT lights to 3500K @ 50% brightness..."

# Call light.turn_on for each light with color_temp=286 (3500K), brightness=128 (50%)
for light in "${LIGHTS_ARRAY[@]}"; do
  curl -s -X POST "${HA_API}/services/light/turn_on" "${HEADERS[@]}" \
    -d "{\"entity_id\":\"${light}\",\"brightness\":128,\"color_temp\":286}" > /dev/null
  echo "  → $light"
done

echo ""
echo "Step 2: Waiting 10 seconds..."
sleep 10

echo ""
echo "Step 3: Turning off ALL lights..."

# Get fresh list of all lights for turn_off (no filtering needed for off)
ALL_LIGHTS=$(curl -s "${HA_API}/states" "${HEADERS[@]}" | jq -r '[.[] | select(.entity_id | startswith("light.")) | .entity_id]')
ALL_LIGHTS_ARRAY=($(echo "$ALL_LIGHTS" | jq -r '.[]'))

for light in "${ALL_LIGHTS_ARRAY[@]}"; do
  curl -s -X POST "${HA_API}/services/light/turn_off" "${HEADERS[@]}" \
    -d "{\"entity_id\":\"${light}\"}" > /dev/null
  echo "  → $light"
done

echo ""
echo "Step 4: Turning off light-related switches..."

SWITCHES=(
  "switch.foyer_chandelier"
  "switch.mcloset_cans"
  "switch.mcloset_chandelier"
  "switch.breakfast_chandelier"
  "switch.upper_hall"
  "switch.alcove_switch"
  "switch.gameroom"
  "switch.bath3"
  "switch.garage"
  "switch.mbath_wall_switch"
  "switch.luggage"
  "switch.mb_rotonda"
  "switch.laundry"
  "switch.laundryu"
  "switch.hallu_1"
  "switch.hallu_2"
  "switch.hallu_3"
  "switch.attic_laundry_attic_laundry_switch"
  "switch.courtyard_sconce_right"
  "switch.courtyard_sconce_left"
  "switch.back_sconces"
  "switch.outdoor_hall_sconces"
  "switch.bath1_lights"
  "switch.mbed_pendants"
  "switch.main_lanai"
)

for switch in "${SWITCHES[@]}"; do
  curl -s -X POST "${HA_API}/services/switch/turn_off" "${HEADERS[@]}" \
    -d "{\"entity_id\":\"${switch}\"}" > /dev/null
  echo "  → $switch"
done

echo ""
echo "=== Operation Complete ==="
echo "Summary:"
echo "  - Set $FILTERED_COUNT lights to 3500K @ 50%"
echo "  - Waited 10 seconds"
echo "  - Turned off ${#ALL_LIGHTS_ARRAY[@]} lights"
echo "  - Turned off ${#SWITCHES[@]} switches"
