//
//  Copyright (c) 2025 Chakib Ben Ziane <contact@blob42.xyz>  and [`gosuki` contributors](https://github.com/blob42/gosuki/graphs/contributors).
//  All rights reserved.
//
//  SPDX-License-Identifier: AGPL-3.0-or-later
//
//  This file is part of GoSuki.
//
//  GoSuki is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Affero General Public License as
//  published by the Free Software Foundation, either version 3 of the
//  License, or (at your option) any later version.
//
//  GoSuki is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Affero General Public License for more details.
//
//  You should have received a copy of the GNU Affero General Public License
//  along with gosuki.  If not, see <http://www.gnu.org/licenses/>.
//

//go:build darwin && systray

package main

import (
	"context"
	"os"

	db "github.com/blob42/gosuki/internal/database"
	"github.com/blob42/gosuki/internal/gui"
	"github.com/blob42/gosuki/internal/utils"
	"github.com/blob42/gosuki/pkg/events"
	"github.com/blob42/gosuki/pkg/logging"
	"github.com/blob42/gosuki/pkg/modules"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/mattn/go-isatty"
	"github.com/urfave/cli/v3"
)

func startDaemon(ctx context.Context, cmd *cli.Command) error {
	defer utils.CleanFiles()

	// Initialize database and caches
	db.Init(ctx, cmd)

	if cmd.Bool("tui") && isatty.IsTerminal(os.Stdout.Fd()) {
		manager := initManager(true)
		modules.MsgDispatcher.AddListener("tui", ModMsgQ)

		tui, err := NewTUI(ctx, func(tea.Model) tea.Cmd {
			return func() tea.Msg {
				err := bootstrapModules(ctx, cmd, manager)
				if err != nil {
					return ErrMsg(err)
				}
				return DaemonStartedMsg{}
			}
		}, manager, tuiOptions...)
		if err != nil {
			return err
		}

		logging.SetTUI(tui.model.logBuffer)
		return tui.Run()
	} else { // drain tui bus
		go func() {
			for range events.TUIBus {
			}
		}()
	}

	manager := initManager(false)

	bootstrapModules(ctx, cmd, manager)
	gui.DarwinRunSystray(manager)
	return nil
}
