package com.example.cuida.utils;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import com.example.cuida.MainActivity;
import com.example.cuida.R;

public class NotificationHelper {

    private final Context context;
    public static final String MEDICATION_CHANNEL_ID = "MEDICATION_CHANNEL_ID";
    public static final String APPOINTMENT_CHANNEL_ID = "APPOINTMENT_CHANNEL_ID";

    public NotificationHelper(Context context) {
        this.context = context;
        createChannels();
    }

    private void createChannels() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager manager = context.getSystemService(NotificationManager.class);
            if (manager != null) {
                // Medication Channel
                NotificationChannel medChannel = new NotificationChannel(
                        MEDICATION_CHANNEL_ID,
                        "Lembretes de Medicação",
                        NotificationManager.IMPORTANCE_HIGH);
                medChannel.setDescription("Notificações para tomar a medicação a horas");
                medChannel.enableLights(true);
                medChannel.setLightColor(Color.BLUE);
                manager.createNotificationChannel(medChannel);

                // Appointment Channel
                NotificationChannel apptChannel = new NotificationChannel(
                        APPOINTMENT_CHANNEL_ID,
                        "Lembretes de Consultas",
                        NotificationManager.IMPORTANCE_HIGH);
                apptChannel.setDescription("Notificações antes das consultas");
                apptChannel.enableLights(true);
                apptChannel.setLightColor(Color.GREEN);
                manager.createNotificationChannel(apptChannel);
            }
        }
    }

    public void sendNotification(String title, String message, int notificationId, String channelId) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context,
                0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId)
                .setSmallIcon(R.drawable.ic_launcher_final) // Using app icon
                .setContentTitle(title)
                .setContentText(message)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (manager != null) {
            manager.notify(notificationId, builder.build());
        }
    }
}
