package app

import (
	"fmt"
	"os"
	"path/filepath"
	"time"

	"vrc_osc_go/internal/common"
)

func appendRuntimeLog(title, text string) error {
	dir := filepath.Join(common.RootDir(), "runtime")
	if err := os.MkdirAll(dir, 0o755); err != nil {
		return err
	}
	path := filepath.Join(dir, "runtime.log")
	body := text
	if body == "" {
		body = "(empty)"
	}
	f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
	if err != nil {
		return err
	}
	defer f.Close()
	_, err = fmt.Fprintf(f, "\n[%s] %s\n%s\n", time.Now().Format("2006-01-02 15:04:05"), title, body)
	return err
}

func appendJoinLeaveLog(text string) error {
	dir := filepath.Join(common.RootDir(), "runtime")
	if err := os.MkdirAll(dir, 0o755); err != nil {
		return err
	}
	path := filepath.Join(dir, "join_leave.log")
	body := text
	if body == "" {
		body = "(empty)"
	}
	f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
	if err != nil {
		return err
	}
	defer f.Close()
	_, err = fmt.Fprintf(f, "\n[%s] VRC JOIN/LEAVE\n%s\n", time.Now().Format("2006-01-02 15:04:05"), body)
	return err
}
