<?php

declare(strict_types=1);

namespace FecaMailshots\Infrastructure;

use PDO;

final class SchemaEnsuringDatabaseRouter implements DatabaseRouter
{
    private DatabaseRouter $inner;
    private bool $installed = false;

    public function __construct(DatabaseRouter $inner)
    {
        $this->inner = $inner;
    }

    public function mailshotsPdo(): PDO
    {
        $this->installOnce();
        return $this->inner->mailshotsPdo();
    }

    public function membersPdo(): PDO
    {
        return $this->inner->membersPdo();
    }

    public function mailshotsDbName(): string
    {
        return $this->inner->mailshotsDbName();
    }

    public function membersDbName(): string
    {
        return $this->inner->membersDbName();
    }

    public function fenDbName(): string
    {
        return $this->inner->fenDbName();
    }

    private function installOnce(): void
    {
        if ($this->installed) {
            return;
        }
        (new MailshotSchemaInstaller($this->inner))->install();
        $this->installed = true;
    }
}
