#include <WiFi.h>
#include <time.h>
#include <ArduinoJson.h>
#include "AuthApiClient.h"

const char* WIFI_SSID = "Barriball - Automation";
const char* WIFI_PASS = "password123abc";
const char* API_BASE = "http://172.21.10.191:5250/";
const char* USERNAME = "test";
const char* PASSWORD = "password";

AuthApiClient api(API_BASE);

void connectWiFi() 
{
  Serial.printf("Connecting to %s", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) 
  { 
    delay(500); Serial.print("."); 
  }

  Serial.println("\nWiFi connected.");
}

void setupTime() 
{
  configTime(0, 0, "pool.ntp.org", "time.nist.gov");
  Serial.print("  Syncing time");

  for (int i = 0; i < 20; i++) 
  { 
    if (time(nullptr) > 1700000000) break; 

    delay(500); 
    Serial.print("."); 
  }

  Serial.println();
}

void setup() 
{
  Serial.begin(115200);
  delay(200);
  connectWiFi();
  setupTime();
  //api.useCertBundle(true); // ESP32-S3 recommended

  if (!api.login(USERNAME, PASSWORD)) 
  { 
    Serial.println("Login failed."); return; 
  }

  Serial.println("Login OK.");
}

unsigned long lastTick = 0;

void loop() 
{
 
  delay(20);
}
