#include "esp_log.h"
#include "mdns.h"
#include "sdkconfig.h"

#include "types.hpp"

internal const char *kTagMDNS = "MDNS";

void start_mdns()
{
  // Initialize mDNS
  esp_err_t err = mdns_init();
  if (err != ESP_OK)
  {
    ESP_LOGE(kTagMDNS, "mDNS Init failed: %d", err);
    return;
  }

  // Set mDNS hostname (from Kconfig)
  const char *hostname = CONFIG_CALENDINK_MDNS_HOSTNAME;
  mdns_hostname_set(hostname);
  ESP_LOGI(kTagMDNS, "mDNS Hostname set to: [%s.local]", hostname);

  // Set mDNS instance name
  mdns_instance_name_set("Calendink Provider");

  // Add HTTP service
  err = mdns_service_add(NULL, "_http", "_tcp", 80, NULL, 0);
  if (err != ESP_OK)
  {
    ESP_LOGE(kTagMDNS, "mDNS Service add failed: %d", err);
  }

  ESP_LOGI(kTagMDNS, "mDNS Service initialized with hostname [%s.local]", hostname);
}
