package logger

import (
	"sync"
	"time"
)

type Monitor struct {
	eventLogger *EventLogger
	lastWrite   time.Time
	mu          sync.RWMutex
}

func NewMonitor(eventLogger *EventLogger) *Monitor {
	m := &Monitor{
		eventLogger: eventLogger,
		lastWrite:   time.Now(),
	}
	go m.watchdogLoop()
	return m
}

func (m *Monitor) RecordWrite() {
	m.mu.Lock()
	m.lastWrite = time.Now()
	m.mu.Unlock()
}

func (m *Monitor) watchdogLoop() {
	ticker := time.NewTicker(1 * time.Minute)
	for range ticker.C {
		m.mu.RLock()
		silence := time.Since(m.lastWrite)
		m.mu.RUnlock()

		if silence > 10*time.Minute {
			m.eventLogger.Event("ТИШИНА_10МИН")
		} else if silence > 5*time.Minute {
			m.eventLogger.Event("ТИШИНА_5МИН")
		}
	}
}