<?php

declare(strict_types=1);

namespace App\Modules\Ledger\Domain;

class LedgerEntry
{
    public function __construct(
        private readonly string $id,
        private readonly string $tenantId,
        private readonly string $memberId,
        private readonly string $entryType,
        private readonly float $amount,
        private readonly string $bookedAt,
        private readonly string $referenceType,
        private readonly ?string $referenceId = null
    ) {
    }

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

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

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

    public function amount(): float
    {
        return $this->amount;
    }

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

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

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

    public function referenceId(): ?string
    {
        return $this->referenceId;
    }

    public function isCorrection(): bool
    {
        return in_array($this->entryType, ['adjustment', 'reversal'], true);
    }

    public function isConsumption(): bool
    {
        return $this->entryType === 'consumption';
    }

    public function isPayment(): bool
    {
        return $this->entryType === 'payment';
    }

    public function isStrikeEntry(): bool
    {
        return $this->isConsumption();
    }

    public function isDeletableStrike(): bool
    {
        return $this->isStrikeEntry();
    }

    public function canBeDeletedByResponsible(): bool
    {
        return $this->isDeletableStrike();
    }

    public function canBeCancelledByResponsible(): bool
    {
        return $this->isPayment();
    }

    /**
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return [
            'id' => $this->id(),
            'tenant_id' => $this->tenantId(),
            'member_id' => $this->memberId(),
            'entry_type' => $this->entryType(),
            'amount' => $this->amount(),
            'booked_at' => $this->bookedAt(),
            'reference_type' => $this->referenceType(),
            'reference_id' => $this->referenceId(),
            'correction' => $this->isCorrection(),
            'strike_entry' => $this->isStrikeEntry(),
            'deletable_strike' => $this->canBeDeletedByResponsible(),
            'cancellable_payment' => $this->canBeCancelledByResponsible(),
        ];
    }
}
