<?php

namespace App\classes;

class DataIntegrationConfig {
    private $conn;
    
    public function __construct($connection) {
        $this->conn = $connection;
    }
    
    /**
     * Get data integration configuration
     * @return array
     */
    public function getConfig() {
        $stmt = $this->conn->prepare("SELECT * FROM data_integration_config WHERE id = 1");
        $stmt->execute();
        $result = $stmt->get_result();
        
        if ($result->num_rows > 0) {
            $config = $result->fetch_assoc();
        } else {
            // Return null if no configuration exists
            $config = null;
        }
        
        $stmt->close();
        return $config;
    }
    
    /**
     * Check if integration is enabled and configured
     * @return bool
     */
    public function isEnabled() {
        $config = $this->getConfig();
        return $config && $config['enabled'] && !empty($config['data_website_api_key']);
    }
    
    /**
     * Get data website URL
     * @return string
     */
    public function getDataWebsiteUrl() {
        $config = $this->getConfig();
        return $config ? rtrim($config['data_website_url'], '/') : '';
    }
    
    /**
     * Get API key
     * @return string
     */
    public function getApiKey() {
        $config = $this->getConfig();
        return $config ? $config['data_website_api_key'] : '';
    }
    
    /**
     * Get webhook URL
     * @return string
     */
    public function getWebhookUrl() {
        $config = $this->getConfig();
        return $config ? $config['webhook_url'] : '';
    }
}
