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

package lunatask

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

// TrackHabitActivityRequest represents the request to track a habit activity.
type TrackHabitActivityRequest struct {
	// PerformedOn is the ISO-8601 date when the activity was performed (e.g., "2024-08-26").
	PerformedOn string `json:"performed_on"`
}

// TrackHabitActivityResponse represents the response from Lunatask API when tracking a habit activity.
type TrackHabitActivityResponse struct {
	Status  string `json:"status"`
	Message string `json:"message,omitempty"`
}

// TrackHabitActivity tracks an activity for a habit in Lunatask.
// The PerformedOn field must be an ISO-8601 date (e.g., "2024-08-26").
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 == "" {
		return nil, fmt.Errorf("%w: performed_on is required", ErrBadRequest)
	}

	// Validate date format (YYYY-MM-DD)
	if _, err := time.Parse("2006-01-02", request.PerformedOn); err != nil {
		return nil, fmt.Errorf("%w: performed_on must be a valid ISO-8601 date (YYYY-MM-DD)", ErrBadRequest)
	}

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

	return resp, nil
}
