<?php

declare(strict_types=1);

namespace Cycle\Annotated\Annotation;

use Cycle\ORM\Schema\GeneratedField;
use Spiral\Attributes\NamedArgumentConstructor;

/**
 * Specifies that the field value is generated by the database or ORM.
 *
 * - before insert: the field is generated before the entity is inserted into the database on PHP side (e.g., UUIDs).
 * - on insert: the field is generated by the database on insert (e.g., auto-increment).
 * - before update: the field is generated before the entity is updated in the database on PHP side (e.g., `onUpdate`).
 *
 * @Annotation
 * @NamedArgumentConstructor
 * @Target({"PROPERTY"})
 */
#[\Attribute(\Attribute::TARGET_PROPERTY)]
#[NamedArgumentConstructor]
class GeneratedValue
{
    public function __construct(
        protected bool $beforeInsert = false,
        protected bool $onInsert = false,
        protected bool $beforeUpdate = false,
    ) {}

    public function getFlags(): ?int
    {
        if (!$this->beforeInsert && !$this->onInsert && !$this->beforeUpdate) {
            return null;
        }

        return
            ($this->beforeInsert ? GeneratedField::BEFORE_INSERT : 0) |
            ($this->onInsert ? GeneratedField::ON_INSERT : 0) |
            ($this->beforeUpdate ? GeneratedField::BEFORE_UPDATE : 0);
    }
}
