// Package hosting provides the Synor Hosting SDK for Go.
// Decentralized web hosting with domain management, DNS, and deployments.
package hosting

import (
	"time"
)

// Network represents the network environment
type Network string

const (
	NetworkMainnet Network = "mainnet"
	NetworkTestnet Network = "testnet"
	NetworkDevnet  Network = "devnet"
)

// Config represents the hosting client configuration
type Config struct {
	APIKey   string
	Endpoint string
	Network  Network
	Timeout  time.Duration
	Retries  int
	Debug    bool
}

// DefaultConfig returns the default configuration
func DefaultConfig(apiKey string) Config {
	return Config{
		APIKey:   apiKey,
		Endpoint: "https://hosting.synor.io/v1",
		Network:  NetworkMainnet,
		Timeout:  60 * time.Second,
		Retries:  3,
		Debug:    false,
	}
}

// ==================== Domain Types ====================

// DomainStatus represents a domain status
type DomainStatus string

const (
	DomainStatusPending   DomainStatus = "pending"
	DomainStatusActive    DomainStatus = "active"
	DomainStatusExpired   DomainStatus = "expired"
	DomainStatusSuspended DomainStatus = "suspended"
)

// Domain represents a domain
type Domain struct {
	Name         string            `json:"name"`
	Status       DomainStatus      `json:"status"`
	Owner        string            `json:"owner"`
	RegisteredAt int64             `json:"registered_at"`
	ExpiresAt    int64             `json:"expires_at"`
	AutoRenew    bool              `json:"auto_renew"`
	Records      *DomainRecord     `json:"records,omitempty"`
}

// DomainRecord represents a domain record for resolution
type DomainRecord struct {
	CID      string            `json:"cid,omitempty"`
	IPv4     []string          `json:"ipv4,omitempty"`
	IPv6     []string          `json:"ipv6,omitempty"`
	CNAME    string            `json:"cname,omitempty"`
	TXT      []string          `json:"txt,omitempty"`
	Metadata map[string]string `json:"metadata,omitempty"`
}

// DomainAvailability represents domain availability check result
type DomainAvailability struct {
	Name      string   `json:"name"`
	Available bool     `json:"available"`
	Price     *float64 `json:"price,omitempty"`
	Premium   bool     `json:"premium"`
}

// RegisterDomainOptions represents options for registering a domain
type RegisterDomainOptions struct {
	Years     int           `json:"years,omitempty"`
	AutoRenew bool          `json:"auto_renew,omitempty"`
	Records   *DomainRecord `json:"records,omitempty"`
}

// ==================== DNS Types ====================

// DnsRecordType represents a DNS record type
type DnsRecordType string

const (
	DnsRecordTypeA     DnsRecordType = "A"
	DnsRecordTypeAAAA  DnsRecordType = "AAAA"
	DnsRecordTypeCNAME DnsRecordType = "CNAME"
	DnsRecordTypeTXT   DnsRecordType = "TXT"
	DnsRecordTypeMX    DnsRecordType = "MX"
	DnsRecordTypeNS    DnsRecordType = "NS"
	DnsRecordTypeSRV   DnsRecordType = "SRV"
	DnsRecordTypeCAA   DnsRecordType = "CAA"
)

// DnsRecord represents a DNS record
type DnsRecord struct {
	Type     DnsRecordType `json:"type"`
	Name     string        `json:"name"`
	Value    string        `json:"value"`
	TTL      int           `json:"ttl,omitempty"`
	Priority *int          `json:"priority,omitempty"`
}

// DnsZone represents a DNS zone
type DnsZone struct {
	Domain    string      `json:"domain"`
	Records   []DnsRecord `json:"records"`
	UpdatedAt int64       `json:"updated_at"`
}

// ==================== Deployment Types ====================

// DeploymentStatus represents a deployment status
type DeploymentStatus string

const (
	DeploymentStatusPending   DeploymentStatus = "pending"
	DeploymentStatusBuilding  DeploymentStatus = "building"
	DeploymentStatusDeploying DeploymentStatus = "deploying"
	DeploymentStatusActive    DeploymentStatus = "active"
	DeploymentStatusFailed    DeploymentStatus = "failed"
	DeploymentStatusInactive  DeploymentStatus = "inactive"
)

// Deployment represents a deployment
type Deployment struct {
	ID           string           `json:"id"`
	Domain       string           `json:"domain"`
	CID          string           `json:"cid"`
	Status       DeploymentStatus `json:"status"`
	URL          string           `json:"url"`
	CreatedAt    int64            `json:"created_at"`
	UpdatedAt    int64            `json:"updated_at"`
	BuildLogs    string           `json:"build_logs,omitempty"`
	ErrorMessage string           `json:"error_message,omitempty"`
}

// RedirectRule represents a redirect rule
type RedirectRule struct {
	Source      string `json:"source"`
	Destination string `json:"destination"`
	StatusCode  int    `json:"status_code,omitempty"`
	Permanent   bool   `json:"permanent,omitempty"`
}

// DeployOptions represents options for deploying a site
type DeployOptions struct {
	Domain        string            `json:"domain,omitempty"`
	Subdomain     string            `json:"subdomain,omitempty"`
	Headers       map[string]string `json:"headers,omitempty"`
	Redirects     []RedirectRule    `json:"redirects,omitempty"`
	SPA           bool              `json:"spa,omitempty"`
	CleanURLs     bool              `json:"clean_urls,omitempty"`
	TrailingSlash bool              `json:"trailing_slash,omitempty"`
}

// DeploymentStats represents deployment statistics
type DeploymentStats struct {
	Requests       int    `json:"requests"`
	Bandwidth      int64  `json:"bandwidth"`
	UniqueVisitors int    `json:"unique_visitors"`
	Period         string `json:"period"`
}

// ==================== SSL Types ====================

// CertificateStatus represents a certificate status
type CertificateStatus string

const (
	CertificateStatusPending CertificateStatus = "pending"
	CertificateStatusIssued  CertificateStatus = "issued"
	CertificateStatusExpired CertificateStatus = "expired"
	CertificateStatusRevoked CertificateStatus = "revoked"
)

// Certificate represents an SSL certificate
type Certificate struct {
	Domain      string            `json:"domain"`
	Status      CertificateStatus `json:"status"`
	AutoRenew   bool              `json:"auto_renew"`
	Issuer      string            `json:"issuer"`
	IssuedAt    *int64            `json:"issued_at,omitempty"`
	ExpiresAt   *int64            `json:"expires_at,omitempty"`
	Fingerprint string            `json:"fingerprint,omitempty"`
}

// ProvisionSSLOptions represents options for provisioning SSL
type ProvisionSSLOptions struct {
	IncludeWWW bool `json:"include_www,omitempty"`
	AutoRenew  bool `json:"auto_renew,omitempty"`
}

// ==================== Site Configuration ====================

// SiteConfig represents site configuration
type SiteConfig struct {
	Domain        string            `json:"domain"`
	CID           string            `json:"cid,omitempty"`
	Headers       map[string]string `json:"headers,omitempty"`
	Redirects     []RedirectRule    `json:"redirects,omitempty"`
	ErrorPages    map[string]string `json:"error_pages,omitempty"`
	SPA           bool              `json:"spa"`
	CleanURLs     bool              `json:"clean_urls"`
	TrailingSlash bool              `json:"trailing_slash"`
}

// ==================== Analytics ====================

// PageView represents page view data
type PageView struct {
	Path  string `json:"path"`
	Views int    `json:"views"`
}

// Referrer represents referrer data
type Referrer struct {
	Referrer string `json:"referrer"`
	Count    int    `json:"count"`
}

// Country represents country data
type Country struct {
	Country string `json:"country"`
	Count   int    `json:"count"`
}

// AnalyticsData represents analytics data
type AnalyticsData struct {
	Domain         string     `json:"domain"`
	Period         string     `json:"period"`
	PageViews      int        `json:"page_views"`
	UniqueVisitors int        `json:"unique_visitors"`
	Bandwidth      int64      `json:"bandwidth"`
	TopPages       []PageView `json:"top_pages"`
	TopReferrers   []Referrer `json:"top_referrers"`
	TopCountries   []Country  `json:"top_countries"`
}

// AnalyticsOptions represents options for analytics query
type AnalyticsOptions struct {
	Period    string `json:"period,omitempty"`
	StartDate string `json:"start,omitempty"`
	EndDate   string `json:"end,omitempty"`
}

// ==================== Errors ====================

// HostingError represents a hosting error
type HostingError struct {
	Message    string
	Code       string
	StatusCode int
}

func (e *HostingError) Error() string {
	return e.Message
}

// NewHostingError creates a new HostingError
func NewHostingError(message string, code string, statusCode int) *HostingError {
	return &HostingError{
		Message:    message,
		Code:       code,
		StatusCode: statusCode,
	}
}
