<?php

namespace mystic\forum\attributes;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class References {
    public const NO_ACTION = 0;
    public const CASCADE = 1;
    public const SET_NULL = 2;
    public const SET_DEFAULT = 3;

    public function __construct(
        public readonly string $foreignTableName,
        public readonly ?string $foreignColumnName = null,
        public readonly int $onDelete = self::NO_ACTION,
    ) {}

    public function __toString(): string {
        return $this->foreignTableName
            . ($this->foreignColumnName !== null ? " ({$this->foreignColumnName})" : "")
            . ([
                    self::NO_ACTION => "",
                    self::CASCADE => " ON DELETE CASCADE",
                    self::SET_NULL => " ON DELETE SET NULL",
                    self::SET_DEFAULT => " ON DELETE SET DEFAULT",
               ][$this->onDelete] ?? "");
    }
}