<?php

namespace App\Config\Entity;

use App\Auth\Entity\User;
use Doctrine\ORM\Mapping as ORM;

use App\Config\Repository\TaxRateHistoryRepository;
#[ORM\Entity(repositoryClass: TaxRateHistoryRepository::class)]
#[ORM\Table(name: 'tax_rate_history')]
#[ORM\Index(columns: ['changed_at'], name: 'idx_tax_history_date')]
class TaxRateHistory
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

    #[ORM\Column(name: 'tax_percent', type: 'decimal', precision: 5, scale: 2)]
    private string $taxPercent;

    #[ORM\Column(name: 'enabled', type: 'boolean')]
    private bool $enabled;

    #[ORM\ManyToOne(targetEntity: User::class)]
    #[ORM\JoinColumn(name: 'changed_by', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    private ?User $changedBy = null;

    #[ORM\Column(name: 'changed_at', type: 'integer')]
    private int $changedAt;

    public function __construct(string $taxPercent, bool $enabled, ?User $changedBy)
    {
        $this->taxPercent = $taxPercent;
        $this->enabled    = $enabled;
        $this->changedBy  = $changedBy;
        $this->changedAt  = time();
    }

    public function getId(): ?int          { return $this->id; }
    public function getTaxPercent(): string { return $this->taxPercent; }
    public function isEnabled(): bool      { return $this->enabled; }
    public function getChangedBy(): ?User  { return $this->changedBy; }
    public function getChangedAt(): int    { return $this->changedAt; }

    public function toArray(): array
    {
        return [
            'tax_percent'      => (float) $this->taxPercent,
            'enabled'          => $this->enabled,
            'changed_by_name'  => $this->changedBy?->getRealName(),
            'changed_at'       => $this->changedAt,
        ];
    }
}
