package main

import (
	"fmt"
	"strings"
)

type Resource struct {
	Subject    string            `json:"subject"`
	Aliases    []string          `json:"aliases,omitempty"`
	Properties map[string]string `json:"properties,omitempty"`
	Links      []Links           `json:"links,omitempty"`
}

type Links struct {
	Rel  string `json:"rel"`
	Type string `json:"type,omitempty"`
	Href string `json:"href,omitempty"`
}

type Webfinger struct {
	domain      string
	username    string
	accountName string
}

func NewWebfinger(username string, domain string, aliases ...string) (*Webfinger, error) {
	return &Webfinger{
		username:    username,
		domain:      domain,
		accountName: fmt.Sprintf("acct:%s@%s", username, domain),
	}, nil
}

func (w *Webfinger) FingerResource(name string) (Resource, error) {
	if !strings.EqualFold(w.accountName, name) {
		return Resource{}, fmt.Errorf("account not found")
	}
	return Resource{
		Subject: w.accountName,
		Links: []Links{
			{
				Rel:  "self",
				Type: "application/activity+json",
				Href: fmt.Sprintf("https://%s/users/%s", w.domain, w.username),
			},
		},
	}, nil
}
