package app

import (
	"context"

	"go.sour.is/pkg/mercury"
)

type mercuryDefault struct{}

var (
	_ mercury.GetRules = (*mercuryDefault)(nil)

	_ mercury.GetIndex  = (*mercuryEnviron)(nil)
	_ mercury.GetConfig = (*mercuryEnviron)(nil)
	_ mercury.GetRules  = (*mercuryEnviron)(nil)
)

// GetRules returns default rules for user role.
func (mercuryDefault) GetRules(ctx context.Context, i mercury.Ident) (lis mercury.Rules, err error) {
	u, ok := i.(hasRole)
	if !ok {
		return nil, nil
	}

	if u.HasRole("admin") {
		lis = append(lis,
			mercury.Rule{
				Role:  "admin",
				Type:  "NS",
				Match: "*",
			},
			mercury.Rule{
				Role:  "write",
				Type:  "NS",
				Match: "*",
			},
			mercury.Rule{
				Role:  "admin",
				Type:  "GR",
				Match: "*",
			},
		)
	} else if u.HasRole("write") {
		lis = append(lis,
			mercury.Rule{
				Role:  "write",
				Type:  "NS",
				Match: "*",
			},
		)
	} else if u.HasRole("read") {
		lis = append(lis,
			mercury.Rule{
				Role:  "read",
				Type:  "NS",
				Match: "*",
			},
		)
	}

	return lis, nil
}
