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

package lunatask

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

// JournalEntry is a daily journal entry. Name and Content are encrypted
// client-side and will be null when read back from the API.
type JournalEntry struct {
	ID        string    `json:"id"`
	DateOn    Date      `json:"date_on"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

// CreateJournalEntryRequest defines a new journal entry.
// Use [JournalEntryBuilder] for a fluent construction API.
type CreateJournalEntryRequest struct {
	// DateOn is the date for the journal entry (required).
	DateOn Date `json:"date_on"`
	// Name is the title for the entry (optional, defaults to weekday name like "Tuesday, July 1st").
	Name *string `json:"name,omitempty"`
	// Content is the Markdown content of the entry (optional).
	Content *string `json:"content,omitempty"`
}

// journalEntryResponse wraps a single journal entry from the API.
type journalEntryResponse struct {
	JournalEntry JournalEntry `json:"journal_entry"`
}

// CreateJournalEntry adds a journal entry. Returns the created entry's metadata;
// Name and Content won't round-trip due to E2EE.
func (c *Client) CreateJournalEntry(ctx context.Context, entry *CreateJournalEntryRequest) (*JournalEntry, error) {
	resp, _, err := doJSON[journalEntryResponse](ctx, c, http.MethodPost, "/journal_entries", entry)
	if err != nil {
		return nil, err
	}

	return &resp.JournalEntry, nil
}
