// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package lunatask

import (
	"context"
	"fmt"
	"net/http"
	"time"
)

// PersonTimelineNote is a note on a person's memory timeline.
// Content is encrypted client-side and will be null when read.
type PersonTimelineNote struct {
	ID        string    `json:"id"`
	DateOn    *Date     `json:"date_on"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

// CreatePersonTimelineNoteRequest defines a timeline note for a person.
// Use [TimelineNoteBuilder] for a fluent construction API.
type CreatePersonTimelineNoteRequest struct {
	// PersonID is the ID of the person to add the note to (required).
	PersonID string `json:"person_id"`
	// DateOn is the date for the note (optional, defaults to today).
	DateOn *Date `json:"date_on,omitempty"`
	// Content is the Markdown content of the note (optional but impractical if empty).
	Content *string `json:"content,omitempty"`
}

// personTimelineNoteResponse wraps a single timeline note from the API.
type personTimelineNoteResponse struct {
	PersonTimelineNote PersonTimelineNote `json:"person_timeline_note"`
}

// CreatePersonTimelineNote adds a note to a person's memory timeline.
// Get person IDs from [Client.ListPeople].
func (c *Client) CreatePersonTimelineNote(
	ctx context.Context,
	note *CreatePersonTimelineNoteRequest,
) (*PersonTimelineNote, error) {
	if note.PersonID == "" {
		return nil, fmt.Errorf("%w: person_id is required", ErrBadRequest)
	}

	resp, _, err := doJSON[personTimelineNoteResponse](ctx, c, http.MethodPost, "/person_timeline_notes", note)
	if err != nil {
		return nil, err
	}

	return &resp.PersonTimelineNote, nil
}
