<?php

declare(strict_types=1);

namespace App\Framework\Deployment\Ssl\ValueObjects;

use App\Framework\Filesystem\ValueObjects\FilePath;

/**
 * Certificate file paths - immutable value object using FilePath
 */
final readonly class CertificatePath
{
    public function __construct(
        public FilePath $certificateDir,
        public FilePath $fullchainPath,
        public FilePath $privatekeyPath,
        public FilePath $chainPath,
        public FilePath $certPath
    ) {}

    public static function forDomain(string $domain, string $baseDir = '/etc/letsencrypt'): self
    {
        $liveDirPath = FilePath::create($baseDir)->join('live', $domain);

        return new self(
            certificateDir: $liveDirPath,
            fullchainPath: $liveDirPath->join('fullchain.pem'),
            privatekeyPath: $liveDirPath->join('privkey.pem'),
            chainPath: $liveDirPath->join('chain.pem'),
            certPath: $liveDirPath->join('cert.pem')
        );
    }

    public function exists(): bool
    {
        return $this->fullchainPath->exists() && $this->privatekeyPath->exists();
    }

    public function isReadable(): bool
    {
        return $this->fullchainPath->isReadable() && $this->privatekeyPath->isReadable();
    }

    public function toArray(): array
    {
        return [
            'certificate_dir' => $this->certificateDir->toString(),
            'fullchain' => $this->fullchainPath->toString(),
            'privatekey' => $this->privatekeyPath->toString(),
            'chain' => $this->chainPath->toString(),
            'cert' => $this->certPath->toString(),
        ];
    }
}
