// Copyright (c) 2017-2026 Lethean (https://lt.hn)
//
// Licensed under the European Union Public Licence (EUPL) version 1.2.
// SPDX-License-Identifier: EUPL-1.2

package blockchain

import (
	"context"
	"fmt"
	"log"
	"os"
	"os/signal"
	"path/filepath"
	"sync"
	"syscall"

	coreerr "dappco.re/go/core/log"

	"dappco.re/go/core/blockchain/chain"
	"dappco.re/go/core/process"
	store "dappco.re/go/core/store"
	"github.com/spf13/cobra"
)

// newChainSyncCommand builds the `chain sync` command family.
//
// Example:
//
//	chain sync
//	chain sync --daemon
//	chain sync --stop
//
// It keeps the foreground and daemon modes behind a predictable command path.
func newChainSyncCommand(dataDir, seed *string, testnet *bool) *cobra.Command {
	var (
		daemon bool
		stop   bool
	)

	cmd := &cobra.Command{
		Use:   "sync",
		Short: "Headless P2P chain sync",
		Long:  "Sync the blockchain from P2P peers without the TUI explorer.",
		RunE: func(cmd *cobra.Command, args []string) error {
			if stop {
				return stopChainSyncDaemon(*dataDir)
			}
			if daemon {
				return runChainSyncDaemon(*dataDir, *seed, *testnet)
			}
			return runChainSyncForeground(*dataDir, *seed, *testnet)
		},
	}

	cmd.Flags().BoolVar(&daemon, "daemon", false, "run as background daemon")
	cmd.Flags().BoolVar(&stop, "stop", false, "stop a running sync daemon")

	return cmd
}

func runChainSyncForeground(dataDir, seed string, testnet bool) error {
	if err := ensureChainDataDirExists(dataDir); err != nil {
		return err
	}

	dbPath := filepath.Join(dataDir, "chain.db")
	chainStore, err := store.New(dbPath)
	if err != nil {
		return coreerr.E("runChainSyncForeground", "open store", err)
	}
	defer chainStore.Close()

	blockchain := chain.New(chainStore)
	chainConfig, hardForks, resolvedSeed := chainConfigForSeed(testnet, seed)

	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
	defer cancel()

	log.Println("Starting headless P2P sync...")
	runChainSyncLoop(ctx, blockchain, &chainConfig, hardForks, resolvedSeed)
	log.Println("Sync stopped.")
	return nil
}

func runChainSyncDaemon(dataDir, seed string, testnet bool) error {
	if err := ensureChainDataDirExists(dataDir); err != nil {
		return err
	}

	pidFile := filepath.Join(dataDir, "sync.pid")

	daemon := process.NewDaemon(process.DaemonOptions{
		PIDFile:  pidFile,
		Registry: process.DefaultRegistry(),
		RegistryEntry: process.DaemonEntry{
			Code:   "dappco.re/go/core/blockchain",
			Daemon: "sync",
		},
	})

	if err := daemon.Start(); err != nil {
		return coreerr.E("runChainSyncDaemon", "daemon start", err)
	}

	dbPath := filepath.Join(dataDir, "chain.db")
	chainStore, err := store.New(dbPath)
	if err != nil {
		_ = daemon.Stop()
		return coreerr.E("runChainSyncDaemon", "open store", err)
	}
	defer chainStore.Close()

	blockchain := chain.New(chainStore)
	chainConfig, hardForks, resolvedSeed := chainConfigForSeed(testnet, seed)

	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
	defer cancel()

	daemon.SetReady(true)
	log.Println("Sync daemon started.")

	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		defer wg.Done()
		runChainSyncLoop(ctx, blockchain, &chainConfig, hardForks, resolvedSeed)
	}()

	err = daemon.Run(ctx)
	wg.Wait() // Wait for the sync loop to finish before closing the store.
	return err
}

func stopChainSyncDaemon(dataDir string) error {
	pidFile := filepath.Join(dataDir, "sync.pid")
	pid, running := process.ReadPID(pidFile)
	if pid == 0 || !running {
		return coreerr.E("stopChainSyncDaemon", "no running sync daemon found", nil)
	}

	processHandle, err := os.FindProcess(pid)
	if err != nil {
		return coreerr.E("stopChainSyncDaemon", fmt.Sprintf("find process %d", pid), err)
	}

	if err := processHandle.Signal(syscall.SIGTERM); err != nil {
		return coreerr.E("stopChainSyncDaemon", fmt.Sprintf("signal process %d", pid), err)
	}

	log.Printf("Sent SIGTERM to sync daemon (PID %d)", pid)
	return nil
}
