package server

/*
Copyright 2024 The external-dns-infoblox-webhook Contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
*/

import (
	"fmt"
	"log"
	"net"
	"net/http"

	"github.com/AbsaOSS/external-dns-infoblox-webhook/cmd/webhook/init/configuration"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"sigs.k8s.io/external-dns/provider"
	"sigs.k8s.io/external-dns/provider/webhook/api"
)

type WebhookServer struct {
	Ready   bool
	Channel chan struct{}
}

func NewServer() *WebhookServer {
	return &WebhookServer{
		Ready:   false,
		Channel: make(chan struct{}, 1),
	}
}

// Init server initialization function
// The server will respond to the following endpoints:
// - / (GET): initialization, negotiates headers and returns the domain filter
// - /records (GET): returns the current records
// - /records (POST): applies the changes
// - /adjustendpoints (POST): executes the AdjustEndpoints method
func (wh *WebhookServer) Start(config configuration.Config, p provider.Provider) {
	api.StartHTTPApi(p, wh.Channel, 0, 0, fmt.Sprintf("%s:%d", config.ServerHost, config.ServerPort))
}

func (wh *WebhookServer) StartHealth(config configuration.Config) {
	go func() {
		listenAddr := fmt.Sprintf("0.0.0.0:%d", config.HealthCheckPort)
		m := http.NewServeMux()
		m.Handle("/metrics", promhttp.Handler())
		m.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
			select {
			case <-wh.Channel:
				wh.Ready = true
			default:
			}
			if wh.Ready {
				w.WriteHeader(http.StatusOK)
				return
			}
			w.WriteHeader(http.StatusInternalServerError)
		})
		s := &http.Server{
			Addr:    listenAddr,
			Handler: m,
		}

		l, err := net.Listen("tcp", listenAddr)
		if err != nil {
			log.Fatal(err)
		}
		err = s.Serve(l)
		if err != nil {
			log.Fatalf("health listener stopped : %s", err)
		}
	}()
}
