package server

import (
	"os"
	"os/signal"
	"os/user"
	"path/filepath"
	"strconv"
	"syscall"
        "fmt"

	"webserv/internal/config"
        "webserv/internal/logic"
	"webserv/internal/handler"
	"webserv/internal/service"
	"webserv/pkg/auxtool/aux509"
	"webserv/pkg/logger"
        "webserv/pkg/swaginfo"

        "github.com/swaggo/swag"

)

type Server struct {
	conf       *config.Config
	svc        *service.Service
        lg         *logic.Logic
	hand       *handler.Handler
	log        *logger.Logger


	x509cert   []byte
	x509key    []byte
	x509caCert []byte
	x509caKey  []byte
}

func NewServer() (*Server, error) {
	var err error
	srv := &Server{}
	srv.log = logger.NewLogger("server")
	return srv, err
}

func (srv *Server) Service() *service.Service {
	return srv.svc
}

func (srv *Server) Configure() error {
	var err error
	srv.conf = config.NewConfig()

	err = srv.conf.ReadFile()
	if err != nil {
                srv.log.Warningf("Cannot load config file, error: %v", err)
		//return err
	}
	err = srv.conf.ReadEnv()
	if err != nil {
		return err
	}
	err = srv.conf.ReadOpts()
	if err != nil {
		return err
	}
	err = srv.WriteSwaginfo()
	if err != nil {
		return err
	}
	srv.log.Info("Service configuration completed")
	return err
}

func (srv *Server) Build() error {
	var err error
	srv.log.Infof("Build server")

	confData, err := srv.conf.Yaml()
	if err != nil {
		return err
	}
	srv.log.Infof("Current configuration is:\n%s\n", confData)
	// Creating X509 certs
	srv.x509caCert, srv.x509caKey, err = aux509.CreateX509CACert(srv.conf.Hostname)
	if err != nil {
		return err
	}
	srv.x509cert, srv.x509key, err = aux509.CreateX509Cert(srv.conf.Hostname, srv.x509caKey)
	if err != nil {
		return err
	}
        // Creating logic
	logicConfig := &logic.LogicConfig{}
	srv.lg, err = logic.NewLogic(logicConfig)
	if err != nil {
		return err
	}

        // Creating handler
	handlerConfig := &handler.HandlerConfig{
                Logic: srv.lg,
	}
	srv.hand, err = handler.NewHandler(handlerConfig)
	if err != nil {
		return err
	}
	// Creating service
	serviceConfig := &service.ServiceConfig{
		AssetsDir: srv.conf.SharePath,
		PortNum:  srv.conf.Service.PortNum,
		Hostname: srv.conf.Hostname,
		Handler:  srv.hand,
		X509Cert: srv.x509cert,
		X509Key:  srv.x509key,
	}
	srv.svc, err = service.NewService(serviceConfig)
	if err != nil {
		return err
	}
        // Build service
	err = srv.svc.Build()
	if err != nil {
		return err
	}
	return err
}

func (srv *Server) Run() error {
	var err error
	currUser, err := user.Current()
	if err != nil {
		return err
	}
	srv.log.Infof("Running server as user %s", currUser.Username)
	sigs := make(chan os.Signal, 1)
	svcDone := make(chan error, 1)
	// Running service
	startService := func(svc *service.Service, done chan error) {
		err = svc.Run()
		if err != nil {
			srv.log.Errorf("Service error: %v", err)
			done <- err
		}
	}
	go startService(srv.svc, svcDone)

	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
	var signal os.Signal

	select {
	case signal = <-sigs:
		srv.log.Infof("Services stopped by signal: %v", signal)
		srv.svc.Stop()
	case err = <-svcDone:
		srv.log.Infof("Service stopped by service error: %v", err)
		srv.svc.Stop()
	}
	return err
}

func (srv *Server) WriteSwaginfo() error {
	var err error
	srv.log.Info("Writing swagger info")

	swaggerUIDir := filepath.Join(srv.conf.SharePath, "swagger")
	err = os.MkdirAll(swaggerUIDir, 0755)
	if err != nil {
		return err
	}

	swagInfo := swaginfo.SwaggerInfo
	swagInfo.Host = fmt.Sprintf("%s:%d", srv.conf.Hostname, srv.conf.Service.PortNum)
	swagInfo.InfoInstanceName = "server"
	swag.Register(swagInfo.InstanceName(), swagInfo)

	swagDoc := swagInfo.ReadDoc()
        swaggerFilePath := filepath.Join(swaggerUIDir, "swagger.json")
	err = os.WriteFile(swaggerFilePath, []byte(swagDoc), 0644)
	if err != nil {
		return err
	}
	srv.log.Infof("Swagger info wrote to %s", swaggerFilePath)
	return err
}


func (srv *Server) PseudoFork() error {
	const successExit int = 0
	var keyEnv string = "IMX0LTSELMRF8K"
	var err error

	_, isChild := os.LookupEnv(keyEnv)
	switch {
	case !isChild:
		os.Setenv(keyEnv, "TRUE")

		procAttr := syscall.ProcAttr{}
		cwd, err := os.Getwd()
		if err != nil {
			return err
		}
		var sysFiles = make([]uintptr, 3)
		sysFiles[0] = uintptr(syscall.Stdin)
		sysFiles[1] = uintptr(syscall.Stdout)
		sysFiles[2] = uintptr(syscall.Stderr)

		procAttr.Files = sysFiles
		procAttr.Env = os.Environ()
		procAttr.Dir = cwd

		_, err = syscall.ForkExec(os.Args[0], os.Args, &procAttr)
		if err != nil {
			return err
		}
		os.Exit(successExit)
	case isChild:
		_, err = syscall.Setsid()
		if err != nil {
			return err
		}
	}
	os.Unsetenv(keyEnv)
	return err
}

func (srv *Server) Daemonize() error {
	var err error
	if srv.conf.Daemon {
		// Restart process process
		err = srv.PseudoFork()
		if err != nil {
			return err
		}

		// Redirect stdin
		nullFile, err := os.OpenFile("/dev/null", os.O_RDWR, 0)
		if err != nil {
			return err
		}
		err = syscall.Dup2(int(nullFile.Fd()), int(os.Stdin.Fd()))
		if err != nil {
			return err
		}
		// Redirect stderr and stout
		logdir := filepath.Dir(srv.conf.LogPath)
		err = os.MkdirAll(logdir, 0750)
		if err != nil {
			return err
		}
		logFile, err := os.OpenFile(srv.conf.LogPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
		if err != nil {
			return err
		}
		err = syscall.Dup2(int(logFile.Fd()), int(os.Stdout.Fd()))
		if err != nil {
			return err
		}
		err = syscall.Dup2(int(logFile.Fd()), int(os.Stderr.Fd()))
		if err != nil {
			return err
		}
		// Write process ID
		rundir := filepath.Dir(srv.conf.RunPath)
		err = os.MkdirAll(rundir, 0750)
		if err != nil {
			return err
		}
		pidFile, err := os.OpenFile(srv.conf.RunPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0640)
		if err != nil {
			return err
		}
		defer pidFile.Close()
		currPid := os.Getpid()
		_, err = pidFile.WriteString(strconv.Itoa(currPid))
		if err != nil {
			return err
		}
	}
	return err
}
