<?php
/**
 * license-php/api/status.php
 * GET /api/license/status
 */

require_once __DIR__ . '/../lib/Response.php';
Response::handleCors();

$config = require __DIR__ . '/../config/config.php';

$domain = trim((string)($_GET['domain'] ?? ''));
if ($domain) {
    require_once __DIR__ . '/../lib/Database.php';
    require_once __DIR__ . '/../lib/LicenseRepository.php';
    require_once __DIR__ . '/../lib/LicenseCrypto.php';
    try {
        $pdo = Database::getConnection($config);
        $repo = new LicenseRepository($pdo);
        $cleanDomain = LicenseCrypto::cleanDomain($domain);
        $lic = $repo->findByDomain($cleanDomain);
        if ($lic && $lic['status'] === 'active') {
            Response::json(200, [
                'success' => true,
                'has_license' => true,
                'status' => 'active',
                'license' => [
                    'license_key' => $lic['license_key'],
                    'domain' => $lic['domain'],
                    'customer_name' => $lic['customer_name'],
                    'plan' => $lic['plan'],
                    'status' => $lic['status'],
                    'expires_at' => $lic['expires_at']
                ]
            ]);
        }
    } catch(Exception $e) {}
}

Response::json(200, [
    'success' => true,
    'server' => 'Production License Server (PHP)',
    'status' => 'online',
    'timestamp' => gmdate('Y-m-d\TH:i:s.v\Z'),
    'product' => $config['product_name']
]);
