package database

import (
	"io"

	proto "git.xeserv.us/xena/route/proto"
	"github.com/Xe/ln"
	"golang.org/x/net/context"
)

// Routes is the set of API calls needed to manage Route resources.
//
// Database backends should implement this interface.
type Routes interface {
	io.Closer

	Get(ctx context.Context, id string) (Route, error)
	GetHost(ctx context.Context, host string) (Route, error)
	GetAll(ctx context.Context, user string) ([]Route, error)
	Put(ctx context.Context, r Route) (Route, error)
	Delete(ctx context.Context, r Route) (Route, error)
}

// Route is a single HTTP route.
type Route struct {
	ID       string `storm:"id"`
	Creator  string
	Hostname string `storm:"unique"`
}

// F https://godoc.org/github.com/Xe/ln#F
func (r Route) F() ln.F {
	return ln.F{
		"route-id":       r.ID,
		"route-creator":  r.Creator,
		"route-hostname": r.Hostname,
	}
}

// AsProto converts this into a protobuf Route.
func (r Route) AsProto() *proto.Route {
	return &proto.Route{
		Id:      r.ID,
		Creator: r.Creator,
		Host:    r.Hostname,
	}
}
