<?php

declare(strict_types=1);

namespace App\Models;

use Database\Factories\BalanceTransactionFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Транзакция баланса тенанта (списание/пополнение лидов и/или рублей).
 *
 * Tenant-aware с RLS. log_hash — append-only audit chain (OPEN-И-15),
 * заполняется триггером `audit_chain_hash()` BEFORE INSERT.
 *
 * Источник: db/schema.sql v8.7 §7.6, table `balance_transactions`.
 *
 * @mixin IdeHelperBalanceTransaction
 */
class BalanceTransaction extends Model
{
    /** @use HasFactory<BalanceTransactionFactory> */
    use HasFactory;

    public const TYPE_TRIAL_BONUS = 'trial_bonus';

    public const TYPE_TOPUP = 'topup';

    public const TYPE_LEAD_CHARGE = 'lead_charge';

    public const TYPE_REFUND = 'refund';

    public const TYPE_MANUAL_ADJUSTMENT = 'manual_adjustment';

    public const TYPE_HISTORICAL_IMPORT = 'historical_import';

    public const TYPE_CHARGEBACK_WRITEDOWN = 'chargeback_writedown';

    public const TYPE_CHARGEBACK_REPAYMENT = 'chargeback_repayment';

    public const TYPE_MIGRATION = 'migration';

    public $timestamps = false;

    protected $fillable = [
        'tenant_id',
        'type',
        'amount_rub',
        'amount_leads',
        'balance_rub_after',
        'balance_leads_after',
        'description',
        'related_type',
        'related_id',
        'user_id',
        'admin_user_id',
        'created_at',
    ];

    protected function casts(): array
    {
        return [
            'tenant_id' => 'integer',
            'amount_rub' => 'decimal:2',
            'amount_leads' => 'integer',
            'balance_rub_after' => 'decimal:2',
            'balance_leads_after' => 'integer',
            'related_id' => 'integer',
            'user_id' => 'integer',
            'admin_user_id' => 'integer',
            'created_at' => 'datetime',
        ];
    }

    /** @return BelongsTo<Tenant, $this> */
    public function tenant(): BelongsTo
    {
        return $this->belongsTo(Tenant::class);
    }

    /** @return BelongsTo<User, $this> */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }
}
