package services

import (
	"context"
	"database/sql"

	"github.com/rede5/gohorsejobs/backend/internal/models"
)

type NotificationService struct {
	DB  *sql.DB
	FCM *FCMService
}

func NewNotificationService(db *sql.DB, fcm *FCMService) *NotificationService {
	return &NotificationService{DB: db, FCM: fcm}
}

func (s *NotificationService) CreateNotification(ctx context.Context, userID string, nType, title, message string, link *string) error {
	query := `
		INSERT INTO notifications (user_id, type, title, message, link, created_at, updated_at)
		VALUES ($1, $2, $3, $4, $5, NOW(), NOW())
	`
	_, err := s.DB.ExecContext(ctx, query, userID, nType, title, message, link)
	return err
}

func (s *NotificationService) ListNotifications(ctx context.Context, userID string) ([]models.Notification, error) {
	query := `
		SELECT id, user_id, type, title, message, link, read_at, created_at, updated_at
		FROM notifications
		WHERE user_id = $1
		ORDER BY created_at DESC
		LIMIT 50
	`
	rows, err := s.DB.QueryContext(ctx, query, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	notifications := []models.Notification{}
	for rows.Next() {
		var n models.Notification
		if err := rows.Scan(
			&n.ID,
			&n.UserID,
			&n.Type,
			&n.Title,
			&n.Message,
			&n.Link,
			&n.ReadAt,
			&n.CreatedAt,
			&n.UpdatedAt,
		); err != nil {
			return nil, err
		}
		notifications = append(notifications, n)
	}
	return notifications, nil
}

func (s *NotificationService) MarkAsRead(ctx context.Context, id string, userID string) error {
	query := `
		UPDATE notifications
		SET read_at = NOW(), updated_at = NOW()
		WHERE id = $1 AND user_id = $2
	`
	_, err := s.DB.ExecContext(ctx, query, id, userID)
	return err
}

func (s *NotificationService) MarkAllAsRead(ctx context.Context, userID string) error {
	query := `
		UPDATE notifications
		SET read_at = NOW(), updated_at = NOW()
		WHERE user_id = $1 AND read_at IS NULL
	`
	_, err := s.DB.ExecContext(ctx, query, userID)
	return err
}

func (s *NotificationService) SaveFCMToken(ctx context.Context, userID, token, platform string) error {
	query := `
		INSERT INTO fcm_tokens (user_id, token, platform, last_seen_at)
		VALUES ($1, $2, $3, NOW())
		ON CONFLICT (user_id, token) 
		DO UPDATE SET last_seen_at = NOW(), platform = EXCLUDED.platform
	`
	_, err := s.DB.ExecContext(ctx, query, userID, token, platform)
	return err
}
