<?php

declare(strict_types=1);

namespace App\Entity;

use App\Enum\SystemSettingKey;
use App\Repository\SystemConfigRepository;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: SystemConfigRepository::class)]
#[ORM\Table(name: 'system_config')]
class SystemConfig
{
    public const DEFAULT_STOCK_ADJUSTMENT_LOOKBACK_ORDERS = '3';
    public const DEFAULT_DEFAULT_DESIRED_STOCK = '2';
    public const DEFAULT_SYSTEM_NAME = 'Zaufen';
    public const DEFAULT_STOCK_INCREASE_AMOUNT = '1';
    public const DEFAULT_STOCK_DECREASE_AMOUNT = '1';
    public const DEFAULT_STOCK_LOW_MULTIPLIER = '0.3';

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private null|int $id = null;

    #[ORM\Column(type: 'datetime_immutable')]
    private readonly DateTimeImmutable $createdAt;

    #[ORM\Column(type: 'datetime_immutable')]
    private DateTimeImmutable $updatedAt;

    public function __construct(
        #[ORM\Column(unique: true, enumType: SystemSettingKey::class)]
        private SystemSettingKey $key,
        #[ORM\Column(type: 'text')]
        private string $value,
        null|DateTimeImmutable $createdAt = null,
        null|DateTimeImmutable $updatedAt = null,
    ) {
        $this->createdAt = $createdAt ?? new DateTimeImmutable();
        $this->updatedAt = $updatedAt ?? new DateTimeImmutable();
    }

    public function getId(): null|int
    {
        return $this->id;
    }

    public function getKey(): SystemSettingKey
    {
        return $this->key;
    }

    public function setKey(SystemSettingKey $key): self
    {
        $this->key = $key;
        $this->updateTimestamp();
        return $this;
    }

    public function getValue(): string
    {
        return $this->value;
    }

    public function setValue(string $value): self
    {
        $this->value = $value;
        $this->updateTimestamp();
        return $this;
    }

    public function getCreatedAt(): DateTimeImmutable
    {
        return $this->createdAt;
    }

    public function getUpdatedAt(): DateTimeImmutable
    {
        return $this->updatedAt;
    }

    private function updateTimestamp(): void
    {
        $this->updatedAt = new DateTimeImmutable();
    }
}
