/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>

This file is part of Scotty.

Scotty is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.

Scotty 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 General Public License for more details.

You should have received a copy of the GNU General Public License along with
Scotty. If not, see <https://www.gnu.org/licenses/>.
*/
package models

import (
	"net/url"
	"time"

	"go.uploadedlobster.com/scotty/internal/auth"
	"go.uploadedlobster.com/scotty/internal/config"
	"golang.org/x/oauth2"
)

// A listen service backend.
// All listen services must implement this interface.
type Backend interface {
	// Return the name of the interface
	Name() string

	// Initialize the backend from a config.
	FromConfig(config *config.ServiceConfig) Backend

	// Return configuration options
	Options() []BackendOption
}

type ImportBackend interface {
	Backend

	// If the backend needs to setup resources before starting to import,
	// this can be done here.
	StartImport() error

	// The implementation can perform all steps here to finalize the
	// export/import and free used resources.
	FinishImport() error
}

// Must be implemented by services supporting the export of listens.
type ListensExport interface {
	Backend

	// Returns a list of all listens newer then oldestTimestamp.
	// The returned list of listens is supposed to be ordered by the
	// Listen.ListenedAt timestamp, with the oldest entry first.
	ExportListens(oldestTimestamp time.Time, results chan ListensResult, progress chan Progress)
}

// Must be implemented by services supporting the import of listens.
type ListensImport interface {
	ImportBackend

	// Imports the given list of listens.
	ImportListens(export ListensResult, importResult ImportResult, progress chan Progress) (ImportResult, error)
}

// Must be implemented by services supporting the export of loves.
type LovesExport interface {
	Backend

	// Returns a list of all loves newer then oldestTimestamp.
	// The returned list of listens is supposed to be ordered by the
	// Love.Created timestamp, with the oldest entry first.
	ExportLoves(oldestTimestamp time.Time, results chan LovesResult, progress chan Progress)
}

// Must be implemented by services supporting the import of loves.
type LovesImport interface {
	ImportBackend

	// Imports the given list of loves.
	ImportLoves(export LovesResult, importResult ImportResult, progress chan Progress) (ImportResult, error)
}

// Must be implemented by backends requiring OAuth2 authentication
type OAuth2Authenticator interface {
	Backend

	// Returns OAuth2 config suitable for this backend
	OAuth2Strategy(redirectUrl *url.URL) auth.OAuth2Strategy

	// Setup the OAuth2 client
	OAuth2Setup(token oauth2.TokenSource) error
}