#include <Arduino.h>
#line 1 "/media/sam/8294CD2994CD2111/Users/Dell/Documents/Arduino/voice_assistant/voice_assistant.ino"
#include <WiFi.h>
#include <PubSubClient.h>
#include <driver/i2s.h>
#include <Freenove_WS2812_Lib_for_ESP32.h> 

// --- CONNECTIVITY ---
const char* WIFI_SSID = "Aussie Broadband 8729";
const char* WIFI_PASS = "Ffdfmunfca";

const char* MQTT_BROKER = "192.168.20.13"; 
const int   MQTT_PORT   = 1883;
const char* MQTT_USER   = "mqtt-user";
const char* MQTT_PASS   = "sam4jo";

const char* TOPIC_AUDIO = "voice/audio_stream";
const char* TOPIC_ACK   = "voice/status"; 

// --- HARDWARE ---
#define I2S_SCK 4
#define I2S_WS  5
#define I2S_SD  6
#define I2S_PORT I2S_NUM_0
#define LED_PIN 48 
#define NUM_LEDS 12

// --- TUNING ---
#define VAD_THRESHOLD 1000 
const int BLOCK_SIZE = 512;
int16_t sBuffer[BLOCK_SIZE];

// --- OBJECTS ---
WiFiClient espClient;
PubSubClient mqtt(espClient);
Freenove_ESP32_WS2812 strip(NUM_LEDS, LED_PIN, 0, TYPE_GRB);

enum DeviceState { STATE_IDLE, STATE_RECORDING, STATE_TRANSMITTING, STATE_ACK_RECEIVED };
DeviceState currentState = STATE_IDLE;
unsigned long stateTimer = 0;

const i2s_config_t i2s_config = {
  .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
  .sample_rate = 16000,
  .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
  .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
  .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
  .dma_buf_count = 8,
  .dma_buf_len = 64,
  .use_apll = true
};

const i2s_pin_config_t pin_config = {
  .bck_io_num = I2S_SCK,
  .ws_io_num = I2S_WS,
  .data_out_num = I2S_PIN_NO_CHANGE,
  .data_in_num = I2S_SD
};

#line 59 "/media/sam/8294CD2994CD2111/Users/Dell/Documents/Arduino/voice_assistant/voice_assistant.ino"
void setRingColor(uint8_t r, uint8_t g, uint8_t b);
#line 64 "/media/sam/8294CD2994CD2111/Users/Dell/Documents/Arduino/voice_assistant/voice_assistant.ino"
void updateLEDs();
#line 79 "/media/sam/8294CD2994CD2111/Users/Dell/Documents/Arduino/voice_assistant/voice_assistant.ino"
void mqttCallback(char* topic, byte* payload, unsigned int length);
#line 84 "/media/sam/8294CD2994CD2111/Users/Dell/Documents/Arduino/voice_assistant/voice_assistant.ino"
void setup();
#line 132 "/media/sam/8294CD2994CD2111/Users/Dell/Documents/Arduino/voice_assistant/voice_assistant.ino"
void loop();
#line 59 "/media/sam/8294CD2994CD2111/Users/Dell/Documents/Arduino/voice_assistant/voice_assistant.ino"
void setRingColor(uint8_t r, uint8_t g, uint8_t b) {
  strip.setAllLedsColor(r, g, b);
  strip.show();
}

void updateLEDs() {
  if (currentState == STATE_ACK_RECEIVED) {
      for (int j = 0; j < 255; j += 20) {
        for (int i = 0; i < NUM_LEDS; i++) {
           byte pos = (i * 256 / NUM_LEDS + j) & 255;
           strip.setLedColorData(i, pos, 255-pos, 0); // Simple fast rainbow
        }
        strip.show();
        delay(5);
      }
      currentState = STATE_IDLE;
      setRingColor(0,0,0);
  }
}

void mqttCallback(char* topic, byte* payload, unsigned int length) {
  Serial.print("MSG Recv: "); Serial.println(topic);
  currentState = STATE_ACK_RECEIVED;
}

void setup() {
  delay(3000); // Wait for Serial
  Serial.begin(115200);
  Serial.println("\n\n=== BOOTING DEBUG MODE ===");
  
  // 1. LEDs
  strip.begin();
  strip.setBrightness(30);
  setRingColor(50, 50, 50); // White
  Serial.println("1. LEDs Ready");

  // 2. I2S
  if (i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL) != ESP_OK) {
    Serial.println("ERR: I2S Driver Fail");
  } else {
    i2s_set_pin(I2S_PORT, &pin_config);
    Serial.println("2. I2S Ready");
  }
  
  // 3. WiFi
  Serial.print("3. WiFi Connecting: ");
  Serial.println(WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  
  int wifi_timeout = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    setRingColor(0, 0, 255); delay(100); setRingColor(0,0,0);
    wifi_timeout++;
    if(wifi_timeout > 20) {
        Serial.println("\nERR: WiFi Timeout! Checking Creds...");
        wifi_timeout = 0; // Keep trying or reset?
    }
  }
  Serial.print("\n   WiFi Connected! IP: ");
  Serial.println(WiFi.localIP());
  
  // 4. MQTT
  mqtt.setServer(MQTT_BROKER, MQTT_PORT);
  mqtt.setCallback(mqttCallback);
  mqtt.setBufferSize(2048); // Vital for audio
  Serial.println("4. MQTT Configured");
  
  setRingColor(0,0,0);
  Serial.println("=== SYSTEM READY ===");
}

void loop() {
  // MQTT Reconnect Handler
  if (!mqtt.connected()) {
    Serial.print("MQTT Connecting to "); Serial.print(MQTT_BROKER); Serial.print(" ... ");
    if (mqtt.connect("ESP32Mic", MQTT_USER, MQTT_PASS)) {
      Serial.println("CONNECTED");
      mqtt.subscribe(TOPIC_ACK);
      setRingColor(0, 255, 0); delay(500); setRingColor(0,0,0); // Flash Green on Connect
    } else {
      Serial.print("FAIL rc="); Serial.println(mqtt.state());
      delay(2000);
      return;
    }
  }
  mqtt.loop();
  updateLEDs();

  size_t bytesRead;
  i2s_read(I2S_PORT, &sBuffer, sizeof(sBuffer), &bytesRead, portMAX_DELAY);

  // Gain Boost (x6)
  for (int i = 0; i < bytesRead / 2; i++) {
    int32_t boosted = sBuffer[i] * 6; 
    if (boosted > 32767) boosted = 32767;
    if (boosted < -32768) boosted = -32768;
    sBuffer[i] = (int16_t)boosted;
  }
  
  long sum = 0;
  for (int i = 0; i < bytesRead / 2; i++) {
    sum += abs(sBuffer[i]);
  }
  int avg = sum / (bytesRead / 2);
  
  // Uncomment to debug sensitivity
  // Serial.printf("Vol: %d\n", avg); 

  // Trigger Logic
  if (currentState == STATE_IDLE && avg > VAD_THRESHOLD) {
    Serial.println("TRIGGERED");
    currentState = STATE_RECORDING;
    stateTimer = millis();
    setRingColor(0, 255, 0); // Green
    
    // START OF SENTENCE FIX: Send the trigger buffer immediately
    mqtt.publish(TOPIC_AUDIO, (const uint8_t*)sBuffer, bytesRead);
  }

  if (currentState == STATE_RECORDING) {
    mqtt.publish(TOPIC_AUDIO, (const uint8_t*)sBuffer, bytesRead);
    
    if (avg > VAD_THRESHOLD) stateTimer = millis();
    
    if (millis() - stateTimer > 1500) {
      Serial.println("SILENCE DETECTED");
      currentState = STATE_TRANSMITTING;
      mqtt.publish("voice/status", "processing"); 
      setRingColor(0, 0, 255); // Blue
    }
  }
  
  if (currentState == STATE_TRANSMITTING && millis() - stateTimer > 4000) {
    Serial.println("TIMEOUT WAIT");
    currentState = STATE_IDLE;
    setRingColor(0,0,0);
  }
}

