package notification

import (
	"context"

	"github.com/google/uuid"
)

// AdminListNotificationsPage bundles the pagination metadata returned to
// the admin API. The shape mirrors `mail.AdminListDeliveriesPage` so
// handlers stay symmetric.
type AdminListNotificationsPage struct {
	Items    []Notification
	Page     int
	PageSize int
	Total    int64
}

// AdminListDeadLettersPage mirrors AdminListNotificationsPage for the
// dead-letter listing.
type AdminListDeadLettersPage struct {
	Items    []DeadLetter
	Page     int
	PageSize int
	Total    int64
}

// AdminListMalformedPage mirrors AdminListNotificationsPage for the
// malformed-intent listing.
type AdminListMalformedPage struct {
	Items    []MalformedIntent
	Page     int
	PageSize int
	Total    int64
}

// AdminListNotifications returns the notification page newest-first.
// page is 1-indexed; pageSize is bounded by normalisePaging.
func (s *Service) AdminListNotifications(ctx context.Context, page, pageSize int) (AdminListNotificationsPage, error) {
	page, pageSize = normalisePaging(page, pageSize)
	offset := (page - 1) * pageSize
	res, err := s.deps.Store.ListNotifications(ctx, offset, pageSize)
	if err != nil {
		return AdminListNotificationsPage{}, err
	}
	return AdminListNotificationsPage{
		Items:    res.Items,
		Page:     page,
		PageSize: pageSize,
		Total:    res.Total,
	}, nil
}

// AdminGetNotification returns a single notification by id; the
// sentinel ErrNotificationNotFound surfaces a 404 in the handler
// layer.
func (s *Service) AdminGetNotification(ctx context.Context, id uuid.UUID) (Notification, error) {
	return s.deps.Store.GetNotification(ctx, id)
}

// AdminListDeadLetters returns the dead-letter page newest-first.
func (s *Service) AdminListDeadLetters(ctx context.Context, page, pageSize int) (AdminListDeadLettersPage, error) {
	page, pageSize = normalisePaging(page, pageSize)
	offset := (page - 1) * pageSize
	res, err := s.deps.Store.ListDeadLetters(ctx, offset, pageSize)
	if err != nil {
		return AdminListDeadLettersPage{}, err
	}
	return AdminListDeadLettersPage{
		Items:    res.Items,
		Page:     page,
		PageSize: pageSize,
		Total:    res.Total,
	}, nil
}

// AdminListMalformed returns the malformed-intent page newest-first.
func (s *Service) AdminListMalformed(ctx context.Context, page, pageSize int) (AdminListMalformedPage, error) {
	page, pageSize = normalisePaging(page, pageSize)
	offset := (page - 1) * pageSize
	res, err := s.deps.Store.ListMalformed(ctx, offset, pageSize)
	if err != nil {
		return AdminListMalformedPage{}, err
	}
	return AdminListMalformedPage{
		Items:    res.Items,
		Page:     page,
		PageSize: pageSize,
		Total:    res.Total,
	}, nil
}

// normalisePaging clamps page and pageSize to the values handlers can
// safely pass through to the store. Defaults match the existing admin
// endpoints (`mail` package); pageSize is capped at 200.
func normalisePaging(page, pageSize int) (int, int) {
	if page <= 0 {
		page = 1
	}
	if pageSize <= 0 {
		pageSize = 25
	}
	if pageSize > 200 {
		pageSize = 200
	}
	return page, pageSize
}
