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

package lunatask

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

// TrackHabitActivityRequest specifies when a habit was performed.
type TrackHabitActivityRequest struct {
	PerformedOn Date `json:"performed_on"`
}

// TrackHabitActivityResponse contains the API's acknowledgement.
type TrackHabitActivityResponse struct {
	Status  string `json:"status"`
	Message string `json:"message,omitempty"`
}

// TrackHabitActivity records that a habit was performed on the given date.
// Habit IDs are found in the Lunatask app's habit settings.
func (c *Client) TrackHabitActivity(
	ctx context.Context,
	habitID string,
	request *TrackHabitActivityRequest,
) (*TrackHabitActivityResponse, error) {
	if habitID == "" {
		return nil, fmt.Errorf("%w: habit ID cannot be empty", ErrBadRequest)
	}

	if request.PerformedOn.IsZero() {
		return nil, fmt.Errorf("%w: performed_on is required", ErrBadRequest)
	}

	resp, _, err := doJSON[TrackHabitActivityResponse](ctx, c, http.MethodPost, "/habits/"+habitID+"/track", request)
	if err != nil {
		return nil, err
	}

	return resp, nil
}
