package store

import (
	"database/sql"
	"errors"
	"fmt"
	"strings"

	"github.com/google/uuid"
)

const workloadNotificationColumns = `id, workload_id, name, url, secret,
	event_types, enabled, sort_order, created_at, updated_at`

func scanWorkloadNotification(scanner interface{ Scan(...any) error }) (WorkloadNotification, error) {
	var n WorkloadNotification
	var enabled int
	err := scanner.Scan(
		&n.ID, &n.WorkloadID, &n.Name, &n.URL, &n.Secret,
		&n.EventTypes, &enabled, &n.SortOrder, &n.CreatedAt, &n.UpdatedAt,
	)
	n.Enabled = enabled != 0
	return n, err
}

// CreateWorkloadNotification inserts a notification route. Returns the
// populated row (with assigned id + timestamps) so callers don't need to
// follow up with a Get.
func (s *Store) CreateWorkloadNotification(n WorkloadNotification) (WorkloadNotification, error) {
	if n.WorkloadID == "" {
		return WorkloadNotification{}, fmt.Errorf("workload_id is required")
	}
	if n.URL == "" {
		return WorkloadNotification{}, fmt.Errorf("url is required")
	}
	if n.ID == "" {
		n.ID = uuid.New().String()
	}
	n.CreatedAt = Now()
	n.UpdatedAt = n.CreatedAt

	_, err := s.db.Exec(
		`INSERT INTO workload_notifications (`+workloadNotificationColumns+`)
		 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
		n.ID, n.WorkloadID, n.Name, n.URL, n.Secret,
		n.EventTypes, BoolToInt(n.Enabled), n.SortOrder, n.CreatedAt, n.UpdatedAt,
	)
	if err != nil {
		return WorkloadNotification{}, fmt.Errorf("insert workload_notification: %w", err)
	}
	return n, nil
}

// ListWorkloadNotifications returns every notification row for a
// workload ordered by (sort_order, created_at) so the UI stays stable
// across reorderings.
func (s *Store) ListWorkloadNotifications(workloadID string) ([]WorkloadNotification, error) {
	rows, err := s.db.Query(
		`SELECT `+workloadNotificationColumns+`
		 FROM workload_notifications
		 WHERE workload_id = ?
		 ORDER BY sort_order, created_at`,
		workloadID,
	)
	if err != nil {
		return nil, fmt.Errorf("list workload_notifications: %w", err)
	}
	defer rows.Close()

	out := []WorkloadNotification{}
	for rows.Next() {
		n, err := scanWorkloadNotification(rows)
		if err != nil {
			return nil, fmt.Errorf("scan workload_notification: %w", err)
		}
		out = append(out, n)
	}
	return out, rows.Err()
}

// GetWorkloadNotification fetches one notification row by id. Returns
// ErrNotFound when the row does not exist so callers can return 404
// cleanly.
func (s *Store) GetWorkloadNotification(id string) (WorkloadNotification, error) {
	n, err := scanWorkloadNotification(s.db.QueryRow(
		`SELECT `+workloadNotificationColumns+`
		 FROM workload_notifications WHERE id = ?`, id,
	))
	if errors.Is(err, sql.ErrNoRows) {
		return WorkloadNotification{}, fmt.Errorf("workload_notification %s: %w", id, ErrNotFound)
	}
	if err != nil {
		return WorkloadNotification{}, fmt.Errorf("query workload_notification: %w", err)
	}
	return n, nil
}

// UpdateWorkloadNotification rewrites an existing row. WorkloadID is
// immutable — re-anchoring a route to a different workload would invite
// silent reassignments after a paste-bug in the UI; recreate instead.
func (s *Store) UpdateWorkloadNotification(n WorkloadNotification) error {
	if n.ID == "" {
		return fmt.Errorf("id is required")
	}
	if n.URL == "" {
		return fmt.Errorf("url is required")
	}
	n.UpdatedAt = Now()
	res, err := s.db.Exec(
		`UPDATE workload_notifications
		 SET name = ?, url = ?, secret = ?, event_types = ?,
		     enabled = ?, sort_order = ?, updated_at = ?
		 WHERE id = ?`,
		n.Name, n.URL, n.Secret, n.EventTypes,
		BoolToInt(n.Enabled), n.SortOrder, n.UpdatedAt, n.ID,
	)
	if err != nil {
		return fmt.Errorf("update workload_notification: %w", err)
	}
	rows, _ := res.RowsAffected()
	if rows == 0 {
		return fmt.Errorf("workload_notification %s: %w", n.ID, ErrNotFound)
	}
	return nil
}

// DeleteWorkloadNotification drops a single notification row.
// Idempotent: missing id returns ErrNotFound so the API can map it to
// 404 cleanly.
func (s *Store) DeleteWorkloadNotification(id string) error {
	res, err := s.db.Exec(`DELETE FROM workload_notifications WHERE id = ?`, id)
	if err != nil {
		return fmt.Errorf("delete workload_notification: %w", err)
	}
	rows, _ := res.RowsAffected()
	if rows == 0 {
		return fmt.Errorf("workload_notification %s: %w", id, ErrNotFound)
	}
	return nil
}

// MatchesEventType returns true when the notification row's EventTypes
// allow-list includes eventType (or is empty, meaning "match all").
// Helper exported so the notification dispatcher can fan-out filtering
// inline without duplicating the comma-split parser.
func (n WorkloadNotification) MatchesEventType(eventType string) bool {
	if !n.Enabled {
		return false
	}
	if n.EventTypes == "" {
		return true
	}
	for _, et := range strings.Split(n.EventTypes, ",") {
		if strings.TrimSpace(et) == eventType {
			return true
		}
	}
	return false
}
