/**
 * @file hosting.h
 * @brief Synor Hosting SDK for C
 *
 * Decentralized web hosting with domain management, DNS, deployments, and SSL.
 */

#ifndef SYNOR_HOSTING_H
#define SYNOR_HOSTING_H

#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

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

typedef enum {
    SYNOR_HOSTING_OK = 0,
    SYNOR_HOSTING_ERROR_CLIENT_CLOSED,
    SYNOR_HOSTING_ERROR_NETWORK,
    SYNOR_HOSTING_ERROR_HTTP,
    SYNOR_HOSTING_ERROR_ENCODING,
    SYNOR_HOSTING_ERROR_DECODING,
    SYNOR_HOSTING_ERROR_INVALID_RESPONSE,
    SYNOR_HOSTING_ERROR_MEMORY,
    SYNOR_HOSTING_ERROR_UNKNOWN
} synor_hosting_error_t;

/* ==================== Configuration ==================== */

typedef struct {
    const char* api_key;
    const char* endpoint;       /* Default: https://hosting.synor.io/v1 */
    uint32_t timeout_ms;        /* Default: 60000 */
    uint32_t retries;           /* Default: 3 */
    bool debug;
} synor_hosting_config_t;

/* ==================== Enums ==================== */

typedef enum {
    SYNOR_DOMAIN_PENDING,
    SYNOR_DOMAIN_ACTIVE,
    SYNOR_DOMAIN_EXPIRED,
    SYNOR_DOMAIN_SUSPENDED
} synor_domain_status_t;

typedef enum {
    SYNOR_DEPLOYMENT_PENDING,
    SYNOR_DEPLOYMENT_BUILDING,
    SYNOR_DEPLOYMENT_DEPLOYING,
    SYNOR_DEPLOYMENT_ACTIVE,
    SYNOR_DEPLOYMENT_FAILED,
    SYNOR_DEPLOYMENT_INACTIVE
} synor_deployment_status_t;

typedef enum {
    SYNOR_CERT_PENDING,
    SYNOR_CERT_ISSUED,
    SYNOR_CERT_EXPIRED,
    SYNOR_CERT_REVOKED
} synor_cert_status_t;

typedef enum {
    SYNOR_DNS_A,
    SYNOR_DNS_AAAA,
    SYNOR_DNS_CNAME,
    SYNOR_DNS_TXT,
    SYNOR_DNS_MX,
    SYNOR_DNS_NS,
    SYNOR_DNS_SRV,
    SYNOR_DNS_CAA
} synor_dns_record_type_t;

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

typedef struct {
    char* cid;
    char** ipv4;
    size_t ipv4_count;
    char** ipv6;
    size_t ipv6_count;
    char* cname;
    char** txt;
    size_t txt_count;
} synor_domain_record_t;

typedef struct {
    char* name;
    synor_domain_status_t status;
    char* owner;
    int64_t registered_at;
    int64_t expires_at;
    bool auto_renew;
    synor_domain_record_t* records;
} synor_domain_t;

typedef struct {
    synor_domain_t* domains;
    size_t count;
} synor_domain_list_t;

typedef struct {
    char* name;
    bool available;
    double price;
    bool premium;
} synor_domain_availability_t;

typedef struct {
    int32_t years;
    bool auto_renew;
} synor_register_domain_options_t;

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

typedef struct {
    synor_dns_record_type_t type;
    char* name;
    char* value;
    int32_t ttl;
    int32_t priority;
} synor_dns_record_t;

typedef struct {
    char* domain;
    synor_dns_record_t* records;
    size_t record_count;
    int64_t updated_at;
} synor_dns_zone_t;

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

typedef struct {
    char* id;
    char* domain;
    char* cid;
    synor_deployment_status_t status;
    char* url;
    int64_t created_at;
    int64_t updated_at;
    char* build_logs;
    char* error_message;
} synor_deployment_t;

typedef struct {
    synor_deployment_t* deployments;
    size_t count;
} synor_deployment_list_t;

typedef struct {
    const char* domain;
    const char* subdomain;
    bool spa;
    bool clean_urls;
    bool trailing_slash;
} synor_deploy_options_t;

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

typedef struct {
    char* domain;
    synor_cert_status_t status;
    bool auto_renew;
    char* issuer;
    int64_t issued_at;
    int64_t expires_at;
    char* fingerprint;
} synor_certificate_t;

typedef struct {
    bool include_www;
    bool auto_renew;
} synor_provision_ssl_options_t;

/* ==================== Analytics Types ==================== */

typedef struct {
    char* path;
    int64_t views;
} synor_page_view_t;

typedef struct {
    char* domain;
    char* period;
    int64_t page_views;
    int64_t unique_visitors;
    int64_t bandwidth;
    synor_page_view_t* top_pages;
    size_t top_pages_count;
} synor_analytics_data_t;

typedef struct {
    const char* period;
    const char* start;
    const char* end;
} synor_analytics_options_t;

/* ==================== Client Handle ==================== */

typedef struct synor_hosting synor_hosting_t;

/* ==================== Lifecycle Functions ==================== */

synor_hosting_t* synor_hosting_create(const synor_hosting_config_t* config);
void synor_hosting_destroy(synor_hosting_t* hosting);
bool synor_hosting_is_closed(synor_hosting_t* hosting);
bool synor_hosting_health_check(synor_hosting_t* hosting);

/* ==================== Domain Operations ==================== */

synor_hosting_error_t synor_hosting_check_availability(synor_hosting_t* hosting,
    const char* name, synor_domain_availability_t* result);
synor_hosting_error_t synor_hosting_register_domain(synor_hosting_t* hosting,
    const char* name, const synor_register_domain_options_t* options,
    synor_domain_t* result);
synor_hosting_error_t synor_hosting_get_domain(synor_hosting_t* hosting,
    const char* name, synor_domain_t* result);
synor_hosting_error_t synor_hosting_list_domains(synor_hosting_t* hosting,
    synor_domain_list_t* result);
synor_hosting_error_t synor_hosting_resolve_domain(synor_hosting_t* hosting,
    const char* name, synor_domain_record_t* result);
synor_hosting_error_t synor_hosting_renew_domain(synor_hosting_t* hosting,
    const char* name, int32_t years, synor_domain_t* result);

void synor_domain_free(synor_domain_t* domain);
void synor_domain_list_free(synor_domain_list_t* list);
void synor_domain_record_free(synor_domain_record_t* record);
void synor_domain_availability_free(synor_domain_availability_t* avail);

/* ==================== DNS Operations ==================== */

synor_hosting_error_t synor_hosting_get_dns_zone(synor_hosting_t* hosting,
    const char* domain, synor_dns_zone_t* result);
synor_hosting_error_t synor_hosting_set_dns_records(synor_hosting_t* hosting,
    const char* domain, const synor_dns_record_t* records, size_t count,
    synor_dns_zone_t* result);
synor_hosting_error_t synor_hosting_add_dns_record(synor_hosting_t* hosting,
    const char* domain, const synor_dns_record_t* record, synor_dns_zone_t* result);
synor_hosting_error_t synor_hosting_delete_dns_record(synor_hosting_t* hosting,
    const char* domain, const char* record_type, const char* name,
    synor_dns_zone_t* result);

void synor_dns_zone_free(synor_dns_zone_t* zone);

/* ==================== Deployment Operations ==================== */

synor_hosting_error_t synor_hosting_deploy(synor_hosting_t* hosting,
    const char* cid, const synor_deploy_options_t* options,
    synor_deployment_t* result);
synor_hosting_error_t synor_hosting_get_deployment(synor_hosting_t* hosting,
    const char* id, synor_deployment_t* result);
synor_hosting_error_t synor_hosting_list_deployments(synor_hosting_t* hosting,
    const char* domain, synor_deployment_list_t* result);
synor_hosting_error_t synor_hosting_rollback(synor_hosting_t* hosting,
    const char* domain, const char* deployment_id, synor_deployment_t* result);
synor_hosting_error_t synor_hosting_delete_deployment(synor_hosting_t* hosting,
    const char* id);

void synor_deployment_free(synor_deployment_t* deployment);
void synor_deployment_list_free(synor_deployment_list_t* list);

/* ==================== SSL Operations ==================== */

synor_hosting_error_t synor_hosting_provision_ssl(synor_hosting_t* hosting,
    const char* domain, const synor_provision_ssl_options_t* options,
    synor_certificate_t* result);
synor_hosting_error_t synor_hosting_get_certificate(synor_hosting_t* hosting,
    const char* domain, synor_certificate_t* result);
synor_hosting_error_t synor_hosting_renew_certificate(synor_hosting_t* hosting,
    const char* domain, synor_certificate_t* result);
synor_hosting_error_t synor_hosting_delete_certificate(synor_hosting_t* hosting,
    const char* domain);

void synor_certificate_free(synor_certificate_t* cert);

/* ==================== Analytics Operations ==================== */

synor_hosting_error_t synor_hosting_get_analytics(synor_hosting_t* hosting,
    const char* domain, const synor_analytics_options_t* options,
    synor_analytics_data_t* result);
synor_hosting_error_t synor_hosting_purge_cache(synor_hosting_t* hosting,
    const char* domain, const char** paths, size_t path_count, int64_t* purged);

void synor_analytics_data_free(synor_analytics_data_t* data);

#ifdef __cplusplus
}
#endif

#endif /* SYNOR_HOSTING_H */
