package dev.photosync.core.provider.google;

import dev.photosync.core.provider.PhotoProvider;
import dev.photosync.core.provider.ProviderConnection;
import dev.photosync.core.provider.ProviderDescriptor;
import dev.photosync.core.provider.ProviderFactory;
import dev.photosync.core.provider.ProviderId;

import java.nio.file.Path;
import java.util.function.Supplier;

/**
 * Registers Google Photos with the {@code ProviderCatalog}.
 *
 * <p>Two things have to be handed in that Immich's factory does not need, and
 * both are consequences of OAuth. The sign-in produces a refresh token that has
 * to outlive the session, so this needs to know where to keep it; and getting
 * that token means opening a browser, which core cannot do, so the caller
 * supplies the way to do it. Both arrive as functions rather than values
 * because the settings directory can move while the game is running.
 */
public final class GooglePhotosProviderFactory implements ProviderFactory {

    /**
     * Where the refresh token lives, next to the settings file.
     *
     * <p>Kept out of {@code photosync.json} deliberately. That file is the one
     * players paste into issue reports, and a refresh token in it is a
     * standing invitation to somebody else's photo library. This one is written
     * through {@code JsonFile}, which restricts it to the owning user.
     */
    public static final String TOKEN_FILE = "google-photos.json";

    private final ProviderDescriptor descriptor =
            new ProviderDescriptor(new ProviderId("googlephotos"), "photosync.provider.googlephotos", true);

    private final Supplier<Path> tokenFile;
    private final SignInPrompt prompt;

    public GooglePhotosProviderFactory(Supplier<Path> tokenFile, SignInPrompt prompt) {
        this.tokenFile = tokenFile;
        this.prompt = prompt;
    }

    @Override
    public ProviderDescriptor descriptor() {
        return descriptor;
    }

    @Override
    public PhotoProvider connect(ProviderConnection connection) {
        return new GooglePhotosProvider(descriptor, connection, tokenFile, prompt);
    }
}
