/*
 * Copyright 2026 Oleg Borodin <onborodin@gmail.com>
 *
 * This work is published and licensed under a Creative Commons
 * Attribution-NonCommercial-NoDerivatives 4.0 International License.
 *
 * Distribution of this work is permitted, but commercial use and
 * modifications are strictly prohibited.
 */

package server

import (
	"context"
	"io/ioutil"
	"os"
	"os/signal"
	"os/user"
	"path/filepath"
	"strconv"
	"syscall"
	"time"

	"mstore/app/config"
	"mstore/app/handler"
	"mstore/app/logger"
	"mstore/app/maindb"
	"mstore/app/operator"
	"mstore/app/service"
	"mstore/app/storage"
	"mstore/pkg/auxtool"
	"mstore/pkg/descr"

	yaml "go.yaml.in/yaml/v4"
)

type Server struct {
	conf *config.Config
	oper *operator.Operator
	svc  *service.Service
	mdb  *maindb.Database
	hand *handler.Handler
	logg *logger.Logger
	stor *storage.Storage
	stat descr.Server
}

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

func (srv *Server) Handler() *handler.Handler {
	return srv.hand
}

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

func (srv *Server) SetLogdir(dir string) {
	srv.conf.Logpath = dir
}

func (srv *Server) SetRundir(dir string) {
	srv.conf.Runpath = dir
}

func (srv *Server) SetDatadir(dir string) {
	srv.conf.Database.Basepath = dir
	srv.conf.Storage.Basepath = dir
	srv.conf.Datadir = dir
}

func (srv *Server) SetPort(port int64) {
	srv.conf.Service.Port = port
}

func (srv *Server) Configure() error {
	var err error
	srv.logg.Infof("Configuration server")
	srv.conf = config.NewConfig()
	if err != nil {
		return err
	}
	err = srv.conf.ReadConfigfile()
	if err != nil {
		//srv.logg.Warningf("Error loading config file: %v", err)
		//return err
	}
	err = srv.conf.ReadX509Cert()
	if err != nil {
		return err
	}

	err = srv.conf.ReadOptions()
	if err != nil {
		return err
	}
	return err
}

func (srv *Server) WriteStat() error {
	// Write status file
	var err error
	statefilePath := filepath.Join(srv.conf.Datadir, "server.yaml")
	stateData, err := yaml.Marshal(srv.stat)
	if err != nil {
		return err
	}
	err = ioutil.WriteFile(statefilePath, stateData, 0640)
	if err != nil {
		return err
	}
	return err
}

func (srv *Server) ReadStat() error {
	var err error
	// Read state file
	statefilePath := filepath.Join(srv.conf.Datadir, "server.yaml")
	if auxtool.FileExists(statefilePath) {
		stateData, err := ioutil.ReadFile(statefilePath)
		if err == nil {
			stat := descr.Server{}
			err = yaml.Unmarshal(stateData, &stat)
			if err != nil {
				return err
			}
			srv.stat = stat
		}
	}
	return err
}

func (srv *Server) Build() error {
	var err error
	srv.logg.Infof("Server building")

	confDump := srv.conf.String()
	srv.logg.Infof("Current server configuration is:\n%s\n", confDump)

	if srv.conf.AsDaemon {
		logdir := filepath.Dir(srv.conf.Logpath)
		srv.logg.Infof("Creating log directory %s", logdir)
		err = os.MkdirAll(logdir, 0750)
		if err != nil {
			return err
		}
		rundir := filepath.Dir(srv.conf.Runpath)
		srv.logg.Infof("Creating run directory %s", rundir)
		err = os.MkdirAll(rundir, 0750)
		if err != nil {
			return err
		}
	}

	// Creating datadir
	datadir := srv.conf.Datadir
	if !auxtool.DirExists(datadir) {
		srv.logg.Infof("Creating data directory %s ", datadir)
		err = os.MkdirAll(datadir, 0750)
		if err != nil {
			return err
		}
	}
	// Read state file
	srv.logg.Infof("Reading server status")
	err = srv.ReadStat()
	if err != nil {
		return err
	}
	// Creating database dir
	dbdir := srv.conf.Database.Basepath
	//if !auxtool.DirExists(dbdir) {
	srv.logg.Infof("Creating database directory %s ", dbdir)
	err = os.MkdirAll(dbdir, 0750)
	if err != nil {
		return err
	}
	//}
	// Creating database
	mdb := maindb.NewDatabase(dbdir)
	srv.logg.Infof("Opening main database")
	err = mdb.OpenDatabase()
	if err != nil {
		return err
	}
	srv.mdb = mdb

	ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
	// Created db scheme
	if !srv.stat.SchemeCreated {
		srv.logg.Infof("Initialize main database")
		err = mdb.InitDatabase()
		if err != nil {
			return err
		}
		srv.stat.SchemeCreated = true
		srv.stat.SchemeCreatedAt = auxtool.TimeNow()
		srv.logg.Infof("Writing server status")
		err = srv.WriteStat()
		if err != nil {
			return err
		}
	}
	// Created anonymous user
	if !srv.stat.AnonymousCreated {
		srv.logg.Infof("Creating anonymous user")
		err = mdb.WriteAnonymous(ctx)
		if err != nil {
			return err
		}
		srv.stat.AnonymousCreated = true
		srv.stat.AnonymousCreatedAt = auxtool.TimeNow()
		srv.logg.Infof("Writing server status")
		err = srv.WriteStat()
		if err != nil {
			return err
		}
	}
	if !srv.stat.InituserCreated {
		srv.logg.Infof("Creating init user")
		err = srv.mdb.WriteInituser(ctx)
		if err != nil {
			return err
		}
		srv.stat.InituserCreatedAt = auxtool.TimeNow()
		srv.stat.InituserCreated = true

		srv.logg.Infof("Writing server status")
		err = srv.WriteStat()
		if err != nil {
			return err
		}
	}

	// Creating storage
	srv.logg.Infof("Creating storage directory")
	datadir = srv.conf.Database.Basepath
	err = os.MkdirAll(datadir, 0750)
	if err != nil {
		return err
	}

	srv.logg.Infof("Creating storage")
	store := storage.NewStorage(datadir)
	srv.stor = store

	// Creating operator
	srv.logg.Infof("Creating operator")
	operatorParams := &operator.OperatorParams{
		MainDB: srv.mdb,
		Store:  srv.stor,
	}
	srv.oper, err = operator.NewOperator(operatorParams)
	if err != nil {
		return err
	}
	// Creating handler
	srv.logg.Infof("Creating handler")
	handlerParams := &handler.HandlerParams{
		Operator: srv.oper,
		MainDB:   srv.mdb,
	}
	srv.hand, err = handler.NewHandler(handlerParams)
	if err != nil {
		return err
	}
	// Creating service
	serviceParams := &service.ServiceParams{
		Handler:  srv.hand,
		X509Cert: srv.conf.X509Cert,
		X509Key:  srv.conf.X509Key,
		Address:  srv.conf.Service.Address,
		Portnum:  srv.conf.Service.Port,
	}
	srv.logg.Infof("Creating service")
	srv.svc, err = service.NewService(serviceParams)
	if err != nil {
		return err
	}
	// Building 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.logg.Infof("Server run as user %s", currUser.Username)

	svcDone := make(chan error, 1)

	// Service run
	srv.logg.Infof("Start service")
	startService := func(svc *service.Service, done chan error) {
		err = svc.Run()
		if err != nil {
			srv.logg.Errorf("Service error: %v", err)
			done <- err
		}
	}
	go startService(srv.svc, svcDone)

	sigs := make(chan os.Signal, 1)

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

	select {
	case signal = <-sigs:
		srv.logg.Infof("Services stopped by signal: %v", signal)
		srv.svc.Stop()
	case err = <-svcDone:
		srv.logg.Infof("Service stopped by service error: %v", err)
		srv.svc.Stop()
	}
	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.AsDaemon {
		// 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
}
