package redriver

type SystemMessageAttributes struct {
	ApproximateFirstReceiveTimestamp int64  `json:"approximate_first_receive_timestamp,omitempty"`
	ApproximateReceiveCount          int64  `json:"approximate_receive_count,omitempty"`
	DeadLetterQueueSourceARN         string `json:"dead_letter_queue_source_arn,omitempty"`
	SenderId                         string `json:"sender_id,omitempty"` // SenderId may be an IAM user or role
	SentTimestamp                    int64  `json:"sent_timestamp,omitempty"`
	SequenceNumber                   int    `json:"sequence_number,omitempty"` // Only returned when producing, not consuming
}

type Message struct {
	Attributes             *SystemMessageAttributes `json:"attributes,omitempty"`
	Body                   string                   `json:"body,omitempty"`                      // Body is not url-encoded
	Md5OfBody              string                   `json:"md5_of_body,omitempty"`               // Md5OfBody hold an MD5 digest of the non-URL-encoded message body string.
	Md5OfMessageAttributes string                   `json:"md5_of_message_attributes,omitempty"` // Md5OfMessageAttributes holds an MD5 digest of the non-URL-encoded message attribute string. You can use this attribute to verify that Amazon SQS received the message correctly. Amazon SQS URL-decodes the message before creating the MD5 digest. For information about MD5, see RFC1321 (https://www.ietf.org/rfc/rfc1321.txt).
	MessageAttributes      map[string]any           `json:"message_attributes,omitempty"`        // Needs app-level interpretation
	MessageId              string                   `json:"message_id,omitempty"`                // MessageId is opaque.
	ReceiptHandle          string                   `json:"receipt_handle,omitempty"`            // ReceiptHandle is an identifier associated with the act of receiving the message. A new receipt handle is returned every time you receive a message. When deleting a message, you provide the last received receipt handle to delete the message.
}

func (m Message) Keys() ItemsKeys {
	return ItemsKeys{
		MessageID:     m.MessageId,
		ReceiptHandle: m.ReceiptHandle,
	}
}

type NameRedriveBody struct {
	Id            string `json:"id,omitempty"`
	ReceiptHandle string `json:"receipt_handle,omitempty"`
}

type QueueInfo struct {
	// Name is the queue name, for both human and machine use
	Name       string               `json:"name,omitempty"`
	URL        string               `json:"url,omitempty"`
	Attributes *QueueInfoAttributes `json:"attributes,omitempty"`
}

func (qi QueueInfo) IsDLQ() bool {
	return qi.Attributes.RedriveAllowPolicy != nil
}

type QueueInfoAttributes struct {
	// ApproximateNumberOfMessages holds the approximate number of messages available for retrieval from the queue.
	ApproximateNumberOfMessages int64 `json:"approximate_number_of_messages,omitempty"`
	// ApproximateNumberOfMessagesDelayed holds he approximate number of messages in the queue that are delayed and not available for reading immediately. This can happen when the queue is configured as a delay queue or when a message has been sent with a delay parameter.
	ApproximateNumberOfMessagesDelayed int64 `json:"approximate_number_of_messages_delayed,omitempty"`
	// ApproximateNumberOfMessagesNotVisible holds the approximate number of messages that are in flight. Messages are considered to be in flight if they have been sent to a client but have not yet been deleted or have not yet reached the end of their visibility window.
	ApproximateNumberOfMessagesNotVisible int64 `json:"approximate_number_of_messages_not_visible,omitempty"`
	// CreatedTimestamp holds the time when the queue was created in seconds (epoch time).
	CreatedTimestamp int64 `json:"created_timestamp,omitempty"`
	// DelaySeconds holds the default delay on the queue in seconds.
	DelaySeconds int64 `json:"delay_seconds,omitempty"`
	// LastModifiedTimestamp holds the time when the queue was last changed in seconds (epoch time).
	LastModifiedTimestamp int64 `json:"last_modified_timestamp,omitempty"`
	// MaximumMessageSize holds the limit of how many bytes a message can contain before Amazon SQS rejects it.
	MaximumMessageSize int64 `json:"maximum_message_size,omitempty"`
	// MessageRetentionPeriod holds the length of time, in seconds, for which Amazon SQS retains a message. When you change a queue's attributes, the change can take up to 60 seconds for most of the attributes to propagate throughout the Amazon SQS system. Changes made to the MessageRetentionPeriod attribute can take up to 15 minutes and will impact existing messages in the queue potentially causing them to be expired and deleted if the MessageRetentionPeriod is reduced below the age of existing messages.
	MessageRetentionPeriod int64 `json:"message_retention_period,omitempty"`

	QueueARN string `json:"queue_arn,omitempty"`
	// ReceiveMessageWaitTimeSeconds holds the length of time, in seconds, for which the ReceiveMessage action waits for a message to arrive.
	ReceiveMessageWaitTimeSeconds int64 `json:"receive_message_wait_time_seconds,omitempty"`
	// VisibilityTimeout holds the number of seconds a message remains hidden after being received. Max 43200.
	VisibilityTimeout int64 `json:"visibility_timeout,omitempty"`

	RedrivePolicy      *QueueInfoAttributesRedrivePolicy      `json:"redrive_policy,omitempty"`
	RedriveAllowPolicy *QueueInfoAttributesRedriveAllowPolicy `json:"redrive_allow_policy,omitempty"`
}

// QueueInfoAttributesRedriveAllowPolicy describes the permissions for the dead-letter queue redrive permission and which source queues can specify dead-letter queues.
type QueueInfoAttributesRedriveAllowPolicy struct {
	// RedrivePermission defines which source queues can specify the current queue as the dead-letter queue.
	RedrivePermission string   `json:"redrivePermission,omitempty"`
	SourceQueueARNs   []string `json:"sourceQueueArns,omitempty"`
}

// QueueInfoAttributesRedrivePolicy holds the parameters for the dead-letter queue functionality of the source queue
type QueueInfoAttributesRedrivePolicy struct {
	// DeadLetterTargetARN holds the Amazon Resource Name (ARN) of the dead-letter queue to which Amazon SQS moves messages after the value of maxReceiveCount is exceeded.
	DeadLetterTargetARN string `json:"deadLetterTargetArn,omitempty"`
	// MaxReceiveCount holds the number of times a message is delivered to the source queue before being moved to the dead-letter queue. Default 10. When the ReceiveCount for a message exceeds the maxReceiveCount for a queue, Amazon SQS moves the message to the dead-letter-queue.
	MaxReceiveCount int64 `json:"maxReceiveCount,omitempty"`
}

// Error is a general error response format
type Error struct {
	// Message is a free-form string describing the error.
	Message string `json:"message,omitempty"`
}
