<?php

declare(strict_types=1);

namespace App\Http\Resources\Api\V1;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class SslCertificateResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'domain_id' => $this->domain_id,
            'common_name' => $this->common_name,
            'alt_names' => $this->alt_names,
            'issuer' => $this->issuer,
            'challenge' => $this->challenge,
            'status' => $this->status?->value,
            'auto_renew' => $this->auto_renew,
            'issued_at' => $this->issued_at?->toIso8601String(),
            'expires_at' => $this->expires_at?->toIso8601String(),
            'days_until_expiry' => $this->daysUntilExpiry(),
            'expiring_soon' => $this->isExpiringSoon(),
            'last_error' => $this->last_error,
            'last_renewal_attempt_at' => $this->last_renewal_attempt_at?->toIso8601String(),
            'created_at' => $this->created_at?->toIso8601String(),
            'links' => [
                'self' => route('api.v1.ssl-certificates.show', $this->id),
                'domain' => route('api.v1.domains.show', $this->domain_id),
                'renew' => route('api.v1.ssl.renew', $this->id),
            ],
        ];
    }
}
