/**
 * DemoNativeFetcher.java
 * 
 * Simple demo implementation of NativeNotificationContentFetcher for the plugin demo app.
 * Returns mock notification content for demonstration purposes.
 * 
 * @author Matthew Raymer
 * @version 1.0.0
 */

package org.timesafari.dailynotification;

import android.util.Log;
import androidx.annotation.NonNull;
import org.timesafari.dailynotification.FetchContext;
import org.timesafari.dailynotification.NativeNotificationContentFetcher;
import org.timesafari.dailynotification.NotificationContent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * Demo implementation of native content fetcher
 * 
 * Returns mock notification content for demonstration. In production host apps,
 * this would fetch from TimeSafari API or other data sources.
 * 
 * <p><b>Configuration:</b></p>
 * <p>This fetcher does NOT override {@code configure()} because it uses hardcoded
 * mock data and doesn't need API credentials. The default no-op implementation
 * from the interface is sufficient.</p>
 * 
 * <p>For an example that accepts configuration from TypeScript, see
 * {@code TestNativeFetcher} in the test app.</p>
 */
public class DemoNativeFetcher implements NativeNotificationContentFetcher {
    
    private static final String TAG = "DemoNativeFetcher";
    
    // Note: We intentionally do NOT override configure() because this fetcher
    // uses hardcoded mock data. The default no-op implementation from the
    // interface is sufficient. This demonstrates that configure() is optional.
    
    @Override
    @NonNull
    public CompletableFuture<List<NotificationContent>> fetchContent(
            @NonNull FetchContext context) {
        
        Log.d(TAG, "DemoNativeFetcher: Fetch triggered - trigger=" + context.trigger +
            ", scheduledTime=" + context.scheduledTime + ", fetchTime=" + context.fetchTime);
        
        return CompletableFuture.supplyAsync(() -> {
            try {
                // Simulate network delay
                Thread.sleep(100);
                
                List<NotificationContent> contents = new ArrayList<>();
                
                // Create a demo notification
                NotificationContent demoContent = new NotificationContent();
                demoContent.setId("demo_notification_" + System.currentTimeMillis());
                demoContent.setTitle("Demo Notification from Native Fetcher");
                demoContent.setBody("This is a demo notification from the native fetcher SPI. " +
                    "Trigger: " + context.trigger + 
                    (context.scheduledTime != null ? 
                        ", Scheduled: " + new java.util.Date(context.scheduledTime) : ""));
                
                // Use scheduled time from context, or default to 1 hour from now
                long scheduledTimeMs = context.scheduledTime != null ?
                    context.scheduledTime : (System.currentTimeMillis() + 3600000);
                demoContent.setScheduledTime(scheduledTimeMs);
                
                // fetchTime is set automatically by NotificationContent constructor (as fetchedAt)
                demoContent.setPriority("default");
                demoContent.setSound(true);
                
                contents.add(demoContent);
                
                Log.i(TAG, "DemoNativeFetcher: Returning " + contents.size() + 
                    " notification(s)");
                
                return contents;
                
            } catch (InterruptedException e) {
                Log.e(TAG, "DemoNativeFetcher: Interrupted during fetch", e);
                Thread.currentThread().interrupt();
                return Collections.emptyList();
                
            } catch (Exception e) {
                Log.e(TAG, "DemoNativeFetcher: Error during fetch", e);
                return Collections.emptyList();
            }
        });
    }
}

