/*
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 RemindVotersJob struct {
	infoLog, errorLog *log.Logger
	timer             *time.Timer
	voters            *models.UserModel
	decisions         *models.MotionModel
	notifier          *notifications.MailNotifier
}

func (r *RemindVotersJob) Identifier() JobIdentifier {
	return JobIDRemindVoters
}

func (r *RemindVotersJob) Schedule() {
	const reminderDays = 3

	now := time.Now().UTC()

	year, month, day := now.Date()

	nextPotentialRun := time.Date(year, month, day+1, 0, 0, 0, 0, time.UTC)
	nextPotentialRun.Add(hoursInDay * time.Hour)

	relevantDue := nextPotentialRun.Add(reminderDays * hoursInDay * time.Hour)

	due, err := r.decisions.NextPendingDue(context.Background(), relevantDue)
	if err != nil {
		r.errorLog.Printf("could not fetch next due date: %v", err)
	}

	if due == nil {
		r.infoLog.Printf("no due motions after relevant due date %s, not scheduling ReminderJob", relevantDue)

		return
	}

	remindNext := due.Add(-reminderDays * hoursInDay * time.Hour).UTC()

	year, month, day = remindNext.Date()

	potentialRun := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)

	if potentialRun.Before(time.Now().UTC()) {
		r.infoLog.Printf("potential reminder time %s is in the past, not scheduling ReminderJob", potentialRun)

		return
	}

	r.infoLog.Printf("scheduling RemindVotersJob for %s", potentialRun)

	when := time.Until(potentialRun)

	if r.timer != nil {
		r.timer.Reset(when)

		return
	}

	r.timer = time.AfterFunc(when, r.Run)
}

func (r *RemindVotersJob) Run() {
	r.infoLog.Print("running RemindVotersJob")

	defer func(r *RemindVotersJob) { r.Schedule() }(r)

	var (
		voters    []*models.User
		decisions []*models.Motion
		err       error
	)

	ctx := context.Background()

	voters, err = r.voters.ReminderVoters(ctx)
	if err != nil {
		r.errorLog.Printf("problem getting voters: %v", err)

		return
	}

	for _, voter := range voters {
		v := voter

		decisions, err = r.decisions.UnvotedForVoter(ctx, v)
		if err != nil {
			r.errorLog.Printf("problem getting unvoted decisions: %v", err)

			return
		}

		if len(decisions) > 0 {
			r.notifier.Notify(&notifications.RemindVoterNotification{Voter: voter, Decisions: decisions})
		}
	}
}

func (r *RemindVotersJob) Stop() {
	if r.timer != nil {
		r.timer.Stop()

		r.timer = nil
	}
}

type RemindVotersOption func(job *RemindVotersJob)

func NewRemindVoters(
	voters *models.UserModel,
	decisions *models.MotionModel,
	notifier *notifications.MailNotifier,
	opts ...RemindVotersOption,
) Job {
	j := &RemindVotersJob{
		voters:    voters,
		decisions: decisions,
		notifier:  notifier,
		infoLog:   log.New(os.Stdout, "", 0),
		errorLog:  log.New(os.Stderr, "", 0),
	}

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

	return j
}

func RemindVotersLog(infoLog, errorLog *log.Logger) RemindVotersOption {
	return func(r *RemindVotersJob) {
		r.infoLog = infoLog
		r.errorLog = errorLog
	}
}
