/**
 * Synor Hosting SDK Types
 * Decentralized web hosting with domain management, DNS, and deployments.
 */

// Network environments
export type HostingNetwork = 'mainnet' | 'testnet' | 'devnet';

// Hosting configuration
export interface HostingConfig {
  apiKey: string;
  endpoint?: string;
  network?: HostingNetwork;
  timeout?: number;
  retries?: number;
  debug?: boolean;
}

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

export type DomainStatus = 'pending' | 'active' | 'expired' | 'suspended';

export interface Domain {
  name: string;
  status: DomainStatus;
  owner: string;
  registeredAt: number;
  expiresAt: number;
  autoRenew: boolean;
  records?: DomainRecord;
}

export interface DomainRecord {
  cid?: string;
  ipv4?: string[];
  ipv6?: string[];
  cname?: string;
  txt?: string[];
  metadata?: Record<string, string>;
}

export interface RegisterDomainOptions {
  years?: number;
  autoRenew?: boolean;
  records?: DomainRecord;
}

export interface DomainAvailability {
  name: string;
  available: boolean;
  price?: number;
  premium?: boolean;
}

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

export type DnsRecordType = 'A' | 'AAAA' | 'CNAME' | 'TXT' | 'MX' | 'NS' | 'SRV' | 'CAA';

export interface DnsRecord {
  type: DnsRecordType;
  name: string;
  value: string;
  ttl?: number;
  priority?: number; // For MX and SRV
}

export interface DnsZone {
  domain: string;
  records: DnsRecord[];
  updatedAt: number;
}

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

export type DeploymentStatus = 'pending' | 'building' | 'deploying' | 'active' | 'failed' | 'inactive';

export interface Deployment {
  id: string;
  domain: string;
  cid: string;
  status: DeploymentStatus;
  url: string;
  createdAt: number;
  updatedAt: number;
  buildLogs?: string;
  errorMessage?: string;
}

export interface DeployOptions {
  domain?: string;
  subdomain?: string;
  headers?: Record<string, string>;
  redirects?: RedirectRule[];
  spa?: boolean; // Single-page app mode
  cleanUrls?: boolean;
  trailingSlash?: boolean;
}

export interface RedirectRule {
  source: string;
  destination: string;
  statusCode?: number;
  permanent?: boolean;
}

export interface DeploymentStats {
  requests: number;
  bandwidth: number;
  uniqueVisitors: number;
  period: string;
}

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

export type CertificateStatus = 'pending' | 'issued' | 'expired' | 'revoked';

export interface Certificate {
  domain: string;
  status: CertificateStatus;
  issuedAt?: number;
  expiresAt?: number;
  autoRenew: boolean;
  issuer: string;
  fingerprint?: string;
}

export interface ProvisionSslOptions {
  includeWww?: boolean;
  autoRenew?: boolean;
}

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

export interface SiteConfig {
  domain: string;
  cid?: string;
  headers?: Record<string, string>;
  redirects?: RedirectRule[];
  errorPages?: {
    '404'?: string;
    '500'?: string;
  };
  spa?: boolean;
  cleanUrls?: boolean;
  trailingSlash?: boolean;
}

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

export interface AnalyticsData {
  domain: string;
  period: string;
  pageViews: number;
  uniqueVisitors: number;
  bandwidth: number;
  topPages: { path: string; views: number }[];
  topReferrers: { referrer: string; count: number }[];
  topCountries: { country: string; count: number }[];
}

export interface AnalyticsOptions {
  period?: '24h' | '7d' | '30d' | '90d';
  startDate?: string;
  endDate?: string;
}

// ==================== Error Types ====================

export class HostingError extends Error {
  code?: string;
  statusCode?: number;

  constructor(message: string, code?: string, statusCode?: number) {
    super(message);
    this.name = 'HostingError';
    this.code = code;
    this.statusCode = statusCode;
  }
}

export class DomainNotFoundError extends HostingError {
  constructor(domain: string) {
    super(`Domain not found: ${domain}`, 'DOMAIN_NOT_FOUND', 404);
    this.name = 'DomainNotFoundError';
  }
}

export class DomainUnavailableError extends HostingError {
  constructor(domain: string) {
    super(`Domain is not available: ${domain}`, 'DOMAIN_UNAVAILABLE', 409);
    this.name = 'DomainUnavailableError';
  }
}
