package user

import (
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
	log "github.com/sirupsen/logrus"
)

func dataSourceUserSchemaMake() map[string]*schema.Schema {
	return map[string]*schema.Schema{
		"user_id": {
			Type:        schema.TypeString,
			Required:    true,
			Description: "user_id",
		},
		"ckey": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "ckey",
		},
		"meta": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
			Description: "meta",
		},
		"active": {
			Type:        schema.TypeBool,
			Computed:    true,
			Description: "active",
		},
		"api_access": {
			Type:     schema.TypeMap,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
			Description: "api_access",
		},
		"authkey": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "authkey",
		},
		"authkeys": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
			Description: "authkeys",
		},
		"blocked": {
			Type:        schema.TypeBool,
			Computed:    true,
			Description: "is the user blocked",
		},
		"data": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "data",
		},
		"description": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "description",
		},
		"domain": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "domain",
		},
		"emails": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
			Description: "emails",
		},
		"gid": {
			Type:        schema.TypeInt,
			Computed:    true,
			Description: "gid",
		},
		"groups": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
			Description: "groups",
		},
		"guid": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "guid",
		},
		"last_check": {
			Type:        schema.TypeInt,
			Computed:    true,
			Description: "last_check",
		},
		"mobile": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
			Description: "mobile",
		},
		"password": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "password",
		},
		"protected": {
			Type:        schema.TypeBool,
			Computed:    true,
			Description: "protected",
		},
		"roles": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
			Description: "roles",
		},
		"service_account": {
			Type:        schema.TypeBool,
			Computed:    true,
			Description: "service_account",
		},
		"xmpp": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
			Description: "xmpp",
		},
	}
}

func dataSourceUserGetAuditSchemaMake() map[string]*schema.Schema {
	return map[string]*schema.Schema{
		"username": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "name of user (get audits for current user if set to empty)",
		},
		"call": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "find by api call",
		},
		"status_code": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "find by status code",
		},
		"timestamp_at": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "find all audits after point in time (unixtime)",
		},
		"sort_by": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "sort by one of supported fields, format ±",
		},
		"timestamp_to": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "find all audits before point in time (unixtime)",
		},
		"page": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "Page number",
		},
		"size": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "Page size",
		},

		"items": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"call": {
						Type:     schema.TypeString,
						Computed: true,
					},
					"response_time": {
						Type:     schema.TypeFloat,
						Computed: true,
					},
					"status_code": {
						Type:     schema.TypeInt,
						Computed: true,
					},
					"time": {
						Type:     schema.TypeFloat,
						Computed: true,
					},
					"guid": {
						Type:     schema.TypeString,
						Computed: true,
					},
				},
			},
		},
		"entry_count": {
			Type:        schema.TypeInt,
			Computed:    true,
			Description: "entry_count",
		},
	}
}

func dataSourceUserListSchemaMake() map[string]*schema.Schema {
	return map[string]*schema.Schema{
		"by_id": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "find by id",
		},
		"active": {
			Type:        schema.TypeBool,
			Optional:    true,
			Description: "find by active. True or False",
		},
		"email": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "find by email",
		},
		"service_account": {
			Type:        schema.TypeBool,
			Optional:    true,
			Description: "find by service account. True or False",
		},
		"sort_by": {
			Type:        schema.TypeString,
			Optional:    true,
			Description: "sort by one of supported fields, format +|-(field)",
		},
		"page": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "Page number",
		},
		"size": {
			Type:        schema.TypeInt,
			Optional:    true,
			Description: "Page size",
		},

		"items": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"ckey": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "ckey",
					},
					"meta": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Schema{
							Type: schema.TypeString,
						},
						Description: "meta",
					},
					"active": {
						Type:        schema.TypeBool,
						Computed:    true,
						Description: "active",
					},
					"apiaccess": {
						Type:     schema.TypeMap,
						Computed: true,
						Elem: &schema.Schema{
							Type: schema.TypeString,
						},
						Description: "apiaccess",
					},
					"authkey": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "authkey",
					},
					"authkeys": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Schema{
							Type: schema.TypeString,
						},
						Description: "authkeys",
					},
					"blocked": {
						Type:        schema.TypeBool,
						Computed:    true,
						Description: "is the user blocked",
					},
					"data": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "data",
					},
					"description": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "description",
					},
					"domain": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "domain",
					},
					"emails": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Schema{
							Type: schema.TypeString,
						},
						Description: "emails",
					},
					"gid": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "gid",
					},
					"groups": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Schema{
							Type: schema.TypeString,
						},
						Description: "groups",
					},
					"guid": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "guid",
					},
					"user_id": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "user id",
					},
					"last_check": {
						Type:        schema.TypeInt,
						Computed:    true,
						Description: "last_check",
					},
					"mobile": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Schema{
							Type: schema.TypeString,
						},
						Description: "mobile",
					},
					"password": {
						Type:        schema.TypeString,
						Computed:    true,
						Description: "password",
					},
					"protected": {
						Type:        schema.TypeBool,
						Computed:    true,
						Description: "protected",
					},
					"roles": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Schema{
							Type: schema.TypeString,
						},
						Description: "roles",
					},
					"service_account": {
						Type:        schema.TypeBool,
						Computed:    true,
						Description: "service_account",
					},
					"xmpp": {
						Type:     schema.TypeList,
						Computed: true,
						Elem: &schema.Schema{
							Type: schema.TypeString,
						},
						Description: "xmpp",
					},
				},
			},
		},
		"entry_count": {
			Type:        schema.TypeInt,
			Computed:    true,
			Description: "entry_count",
		},
	}
}

func resourceUserSchemaMake() map[string]*schema.Schema {
	log.Debugf("resourceUserSchemaMake: invoked")
	return map[string]*schema.Schema{
		"username": {
			Type:        schema.TypeString,
			Required:    true,
			Description: "ID of user",
		},
		"emailaddress": {
			Type:        schema.TypeString,
			Required:    true,
			Description: "email address of the user",
		},
		"password": {
			Type:        schema.TypeString,
			Default:     "strongpassword",
			Optional:    true,
			Description: "password of user",
		},
		"provider_name": {
			Type:         schema.TypeString,
			Optional:     true,
			Description:  "provider",
			ValidateFunc: validation.StringInSlice([]string{"bvs", "decs3o"}, false),
		},
		"groups": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
			Description: "list of groups this user belongs to",
		},
		"ckey": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "ckey",
		},
		"meta": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
			Description: "meta",
		},
		"active": {
			Type:        schema.TypeBool,
			Computed:    true,
			Description: "active",
		},
		"apiaccess": {
			Type:     schema.TypeSet,
			Optional: true,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeInt,
			},
			Description: "list of apiaccess groups this user belongs to",
		},
		"blocked": {
			Type:        schema.TypeBool,
			Optional:    true,
			Computed:    true,
			Description: "is the user blocked",
		},
		"authkey": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "authkey",
		},
		"auth_keys": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
			Description: "authkeys",
		},
		"data": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "data",
		},
		"description": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "description",
		},
		"domain": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "domain",
		},
		"gid": {
			Type:        schema.TypeInt,
			Computed:    true,
			Description: "gid",
		},
		"guid": {
			Type:        schema.TypeString,
			Computed:    true,
			Description: "guid",
		},
		"last_check": {
			Type:        schema.TypeInt,
			Computed:    true,
			Description: "last_check",
		},
		"mobile": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
			Description: "mobile",
		},
		"protected": {
			Type:        schema.TypeBool,
			Computed:    true,
			Description: "protected",
		},
		"roles": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
			Description: "roles",
		},
		"service_account": {
			Type:        schema.TypeBool,
			Computed:    true,
			Description: "service_account",
		},
		"xmpp": {
			Type:     schema.TypeList,
			Computed: true,
			Elem: &schema.Schema{
				Type: schema.TypeString,
			},
			Description: "xmpp",
		},
	}
}
