package npm

// ProxyHostConfig holds the input fields for creating or updating a proxy host.
type ProxyHostConfig struct {
	DomainNames      []string `json:"domain_names"`
	ForwardScheme    string   `json:"forward_scheme"`
	ForwardHost      string   `json:"forward_host"`
	ForwardPort      int      `json:"forward_port"`
	CertificateID    int      `json:"certificate_id"`
	SSLForced        bool     `json:"ssl_forced"`
	BlockExploits    bool     `json:"block_exploits"`
	CachingEnabled   bool     `json:"caching_enabled"`
	AllowWebsocket   bool     `json:"allow_websocket_upgrade"`
	HTTP2Support     bool     `json:"http2_support"`
	AdvancedConfig   string   `json:"advanced_config"`
	HSTSEnabled      bool     `json:"hsts_enabled"`
	HSTSSubdomains   bool     `json:"hsts_subdomains"`
	AccessListID     int      `json:"access_list_id"`
	Meta             Meta     `json:"meta"`
	Locations        []any    `json:"locations"`
}

// Meta holds metadata tags for a proxy host.
type Meta struct {
	LetsEncryptAgree bool   `json:"letsencrypt_agree"`
	DNSChallenge     bool   `json:"dns_challenge"`
	LetsEncryptEmail string `json:"letsencrypt_email,omitempty"`
}

// ProxyHost represents a proxy host as returned by the NPM API.
type ProxyHost struct {
	ID               int      `json:"id"`
	DomainNames      []string `json:"domain_names"`
	ForwardScheme    string   `json:"forward_scheme"`
	ForwardHost      string   `json:"forward_host"`
	ForwardPort      int      `json:"forward_port"`
	CertificateID    any      `json:"certificate_id"`
	SSLForced        boolInt  `json:"ssl_forced"`
	BlockExploits    boolInt  `json:"block_exploits"`
	CachingEnabled   boolInt  `json:"caching_enabled"`
	AllowWebsocket   boolInt  `json:"allow_websocket_upgrade"`
	HTTP2Support     boolInt  `json:"http2_support"`
	AdvancedConfig   string   `json:"advanced_config"`
	HSTSEnabled      boolInt  `json:"hsts_enabled"`
	HSTSSubdomains   boolInt  `json:"hsts_subdomains"`
	AccessListID     int      `json:"access_list_id"`
	Meta             Meta     `json:"meta"`
	Enabled          boolInt  `json:"enabled"`
	CreatedOn        string   `json:"created_on"`
	ModifiedOn       string   `json:"modified_on"`
}

// Certificate represents an SSL certificate as returned by the NPM API.
type Certificate struct {
	ID          int      `json:"id"`
	NiceName    string   `json:"nice_name"`
	DomainNames []string `json:"domain_names"`
	ExpiresOn   string   `json:"expires_on"`
	Provider    string   `json:"provider"`
}

// boolInt handles the NPM API's inconsistent use of 0/1 integers for boolean fields.
type boolInt bool

// authRequest is the request body for POST /api/tokens.
type authRequest struct {
	Identity string `json:"identity"`
	Secret   string `json:"secret"`
}

// authResponse is the response body from POST /api/tokens.
type authResponse struct {
	Token   string `json:"token"`
	Expires string `json:"expires"`
}

// apiError represents an error response from the NPM API.
type apiError struct {
	Error   apiErrorDetail `json:"error"`
}

type apiErrorDetail struct {
	Message string `json:"message"`
	Code    int    `json:"code"`
}
