#include <WiFi.h>
#include <AsyncTCP.h>
#include <AsyncWebServer_ESP32_ENC.h>

// Replace with your WiFi credentials
const char* ssid = "Simon";
const char* password = "s326n267";

AsyncWebServer server(80);

// Define the LED pin
const int ledPin = 2;

// Define the valid fingerprint IDs
const int validFingerprints[] = {1, 2, 3};

bool isValidFingerprint(int fingerprintId) {
  for (int i = 0; i < sizeof(validFingerprints) / sizeof(validFingerprints[0]); i++) {
    if (fingerprintId == validFingerprints[i]) {
      return true;
    }
  }
  return false;
}

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

  Serial.begin(115200);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
    Serial.end();
  }
  Serial.println("Connected to WiFi");

  server.on("/fingerprint", HTTP_POST, [](AsyncWebServerRequest *request) {
    if (request->hasParam("id", true)) {
      int fingerprintId = request->getParam("id", true)->value().toInt();
      if (isValidFingerprint(fingerprintId)) {
        digitalWrite(ledPin, HIGH); // Turn on the LED to simulate the door opening
        request->send(200, "text/plain", "Door Unlocked");
        delay(5000); // Simulate door being open for 5 seconds
        digitalWrite(ledPin, LOW); // Turn off the LED to simulate the door closing
      } else {
        request->send(403, "text/plain", "Access Denied");
      }
    } else {
      request->send(400, "text/plain", "Bad Request");
    }
  });
  server.begin();
}

void loop() {
}