package regfishapi

import "context"

// HostingBackup is the state of a running or finished backup recovery.
type HostingBackup struct {
	RecoverStatus      *string `json:"recover_status"`
	RecoverPointInTime *int64  `json:"recover_point_in_time"`
	RecoveredAt        *int64  `json:"recovered_at"`
}

// HostingPackage is a web hosting package of the account.
type HostingPackage struct {
	// ID is the package id, which is also the system user group of the package.
	ID            string         `json:"id"`
	WebhostID     int            `json:"webhost_id"`
	Plan          string         `json:"plan"`
	Generation    int            `json:"generation,omitempty"`
	Status        string         `json:"status"`
	Active        bool           `json:"active"`
	Cancelled     bool           `json:"cancelled"`
	PHPVersion    string         `json:"php_version,omitempty"`
	CreatedAt     int64          `json:"created_at,omitempty"`
	StartsAt      *int64         `json:"starts_at"`
	ExpiresAt     *int64         `json:"expires_at"`
	AliasCount    int            `json:"alias_count,omitempty"`
	DatabaseCount int            `json:"database_count,omitempty"`
	Backup        *HostingBackup `json:"backup,omitempty"`
}

// HostingAlias is a domain alias of a hosting package.
type HostingAlias struct {
	Name        string `json:"name"`
	OwnVhost    bool   `json:"own_vhost,omitempty"`
	VhostDir    string `json:"vhost_dir,omitempty"`
	RFDomainID  *int   `json:"rf_domain_id"`
	LinkedToApp bool   `json:"linked_to_app,omitempty"`
}

// HostingDatabase is a database of a hosting package. Credentials are never
// returned by the API — database passwords are available only in the dash.
type HostingDatabase struct {
	Name string `json:"name"`
	User string `json:"user"`
	Host string `json:"host,omitempty"`
}

type hostingPackagesResponse struct {
	Packages []HostingPackage `json:"packages"`
}

type hostingAliasesResponse struct {
	Aliases []HostingAlias `json:"aliases"`
}

type hostingDatabasesResponse struct {
	Databases []HostingDatabase `json:"databases"`
}

// ListHostingPackages lists the customer's web hosting packages.
func (c *Client) ListHostingPackages(ctx context.Context) ([]HostingPackage, error) {
	var out hostingPackagesResponse
	err := c.doJSON(ctx, "GET", "/hosting/packages", nil, &out)
	return out.Packages, err
}

// GetHostingPackage retrieves a single web hosting package.
func (c *Client) GetHostingPackage(ctx context.Context, id string) (HostingPackage, error) {
	var out HostingPackage
	err := c.doJSON(ctx, "GET", "/hosting/packages/"+pathSegment(id), nil, &out)
	return out, err
}

// ListHostingAliases lists the domain aliases of a hosting package.
func (c *Client) ListHostingAliases(ctx context.Context, id string) ([]HostingAlias, error) {
	var out hostingAliasesResponse
	err := c.doJSON(ctx, "GET", "/hosting/packages/"+pathSegment(id)+"/aliases", nil, &out)
	return out.Aliases, err
}

// ListHostingDatabases lists the databases of a hosting package.
func (c *Client) ListHostingDatabases(ctx context.Context, id string) ([]HostingDatabase, error) {
	var out hostingDatabasesResponse
	err := c.doJSON(ctx, "GET", "/hosting/packages/"+pathSegment(id)+"/databases", nil, &out)
	return out.Databases, err
}
