/*
Copyright 2017-2022 CAcert Inc.
SPDX-License-Identifier: Apache-2.0

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package jobs

import (
	"context"
	"log"
	"os"
	"time"

	"git.cacert.org/cacert-boardvoting/internal/models"
	"git.cacert.org/cacert-boardvoting/internal/notifications"
)

type CloseDecisionsJob struct {
	timer     *time.Timer
	infoLog   *log.Logger
	errorLog  *log.Logger
	decisions *models.MotionModel
	notifier  *notifications.MailNotifier
}

func (c *CloseDecisionsJob) Identifier() JobIdentifier {
	return JobIDCloseDecisions
}

func (c *CloseDecisionsJob) Schedule() {
	var (
		nextDue *time.Time
		err     error
	)

	ctx := context.Background()

	nextDue, err = c.decisions.NextPendingDue(ctx, time.Now().UTC())
	if err != nil {
		c.errorLog.Printf("could not get next pending due date")

		c.Stop()

		return
	}

	if nextDue == nil {
		c.infoLog.Printf("no next planned execution of CloseDecisionsJob")
		c.Stop()

		return
	}

	c.infoLog.Printf("scheduling CloseDecisionsJob for %s", nextDue)
	when := time.Until(nextDue.Add(time.Second))

	if c.timer == nil {
		c.timer = time.AfterFunc(when, c.Run)

		return
	}

	c.timer.Reset(when)
}

func (c *CloseDecisionsJob) Run() {
	c.infoLog.Printf("running CloseDecisionsJob")

	defer func(c *CloseDecisionsJob) { c.Schedule() }(c)

	c.RunExpired()
}

func (c *CloseDecisionsJob) RunExpired() {
	results, err := c.decisions.CloseDecisions(context.Background())
	if err != nil {
		c.errorLog.Printf("closing decisions failed: %v", err)
	}

	for _, res := range results {
		c.infoLog.Printf(
			"decision %s closed with result %s: reasoning '%s'",
			res.Tag,
			res.Status,
			res.Reasoning,
		)

		c.notifier.Notify(&notifications.ClosedDecisionNotification{Decision: res})
	}
}

func (c *CloseDecisionsJob) Stop() {
	if c.timer != nil {
		c.timer.Stop()
		c.timer = nil
	}
}

type CloseDecisionsOption func(job *CloseDecisionsJob)

func NewCloseDecisionsJob(
	decisions *models.MotionModel,
	mailNotifier *notifications.MailNotifier,
	opts ...CloseDecisionsOption,
) Job {
	j := &CloseDecisionsJob{
		infoLog:   log.New(os.Stdout, "", 0),
		errorLog:  log.New(os.Stderr, "", 0),
		decisions: decisions,
		notifier:  mailNotifier,
	}

	for _, o := range opts {
		o(j)
	}

	j.RunExpired()

	return j
}

func CloseDecisionsLog(infoLog, errorLog *log.Logger) CloseDecisionsOption {
	return func(j *CloseDecisionsJob) {
		j.infoLog = infoLog
		j.errorLog = errorLog
	}
}
