<?php
/**
 * Ciyuan Open Platform PHP SDK
 * 
 * Usage:
 *   $client = new CiyuanOpen\Client('app_xxx', 'xxx');
 *   $resp = $client->health();
 */
namespace CiyuanOpen;

class Client
{
    private string $baseUrl = 'https://api.ciyuanfun.cn/api';
    private string $appId;
    private string $appSecret;

    public function __construct(string $appId, string $appSecret)
    {
        $this->appId = $appId;
        $this->appSecret = $appSecret;
    }

    private function sign(string $method, string $path, string $body = ''): array
    {
        $timestamp = (string)time();
        $nonce = substr(md5(uniqid()), 0, 16);
        $signStr = strtoupper($method) . "\n{$path}\n{$body}\n{$timestamp}\n{$nonce}\n{$this->appSecret}";
        return [
            'X-App-ID: ' . $this->appId,
            'X-Timestamp: ' . $timestamp,
            'X-Nonce: ' . $nonce,
            'X-Sign: ' . hash_hmac('sha256', $signStr, $this->appSecret),
        ];
    }

    private function request(string $method, string $path, ?array $body = null): array
    {
        $bodyStr = $body ? json_encode($body) : '';
        $headers = $this->sign($method, $path, $bodyStr);
        $headers[] = 'Content-Type: application/json';

        $ch = curl_init($this->baseUrl . '/' . $path);
        curl_setopt_array($ch, [
            CURLOPT_CUSTOMREQUEST => $method,
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT => 30,
        ]);
        if ($bodyStr) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyStr);
        }

        $resp = curl_exec($ch);
        curl_close($ch);
        return json_decode($resp, true) ?? ['error' => 'parse_failed'];
    }

    public function health(): array
    {
        return $this->request('GET', 'open/v1/health');
    }

    public function imageGenerate(array $params): array
    {
        return $this->request('POST', 'open/v1/image/generate', $params);
    }

    public function imageTask(string $taskId): array
    {
        return $this->request('GET', "open/v1/image/task/{$taskId}");
    }

    public function videoGenerate(array $params): array
    {
        return $this->request('POST', 'open/v1/video/generate', $params);
    }

    public function listWorks(): array
    {
        return $this->request('GET', 'open/v1/works');
    }

    public function getWork(string $id): array
    {
        return $this->request('GET', "open/v1/works/{$id}");
    }

    public function assetCertify(string $content, string $creator = '', string $type = 'digital_work'): array
    {
        return $this->request('POST', 'open/v1/asset/certify', [
            'content' => $content, 'creator' => $creator, 'type' => $type,
        ]);
    }

    public function assetVerify(string $certId): array
    {
        return $this->request('GET', "open/v1/asset/verify/{$certId}");
    }

    public function userCredits(): array
    {
        return $this->request('GET', 'open/v1/user/credits');
    }
}
