package resource

import (
	"context"
	"net/http"

	nhttp "source.gleipnir.technology/Gleipnir/nidus-sync/http"
	"source.gleipnir.technology/Gleipnir/nidus-sync/platform"
	"source.gleipnir.technology/Gleipnir/nidus-sync/platform/types"
	//"github.com/rs/zerolog/log"
)

type contactR struct {
	router *router
}

func Contact(r *router) *contactR {
	return &contactR{
		router: r,
	}
}

type contact struct {
	types.Contact
	Messages []types.Message `json:"messages"`
	URI      string
}

func (res *contactR) ByIDGet(ctx context.Context, r *http.Request, user platform.User, qp QueryParams) (contact, *nhttp.ErrorWithStatus) {
	return contact{}, nil
}
func (res *contactR) List(ctx context.Context, r *http.Request, user platform.User, query QueryParams) ([]contact, *nhttp.ErrorWithStatus) {
	contacts, err := platform.ContactsForOrganization(ctx, user.Organization.ID)
	if err != nil {
		return nil, nhttp.NewError("nuisance report query: %w", err)
	}
	result := make([]contact, len(contacts))
	for i, c := range contacts {
		uri, err := res.router.IDToURI("contact.ByIDGet", int(c.ID))
		if err != nil {
			return nil, nhttp.NewError("contact uri: %w", err)
		}
		messages, err := platform.MessagesForContact(ctx, int64(c.ID))
		if err != nil {
			return nil, nhttp.NewError("messages for contact: %w", err)
		}
		result[i] = contact{
			Contact:  c,
			Messages: messages,
			URI:      uri,
		}
	}
	return result, nil
}
