<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Лог событий по сделке (примеры event: deal.created, deal.status_changed).
 *
 * Tenant-aware с RLS. deal_id — без FK (deals партиционирована),
 * целостность на уровне приложения. log_hash заполняется триггером
 * audit_chain_hash() BEFORE INSERT (OPEN-И-15).
 *
 * Источник: db/schema.sql v8.7 §6, table `activity_log`.
 *
 * @mixin IdeHelperActivityLog
 */
class ActivityLog extends Model
{
    public const EVENT_DEAL_CREATED = 'deal.created';

    public const EVENT_DEAL_STATUS_CHANGED = 'deal.status_changed';

    public const EVENT_DEAL_ASSIGNED = 'deal.assigned';

    public const EVENT_DEAL_DELETED = 'deal.deleted';

    public const EVENT_DEAL_RESTORED = 'deal.restored';

    public $timestamps = false;

    protected $table = 'activity_log';

    protected $fillable = [
        'tenant_id',
        'user_id',
        'deal_id',
        'event',
        'old_value',
        'new_value',
        'context',
        'ip_address',
        'user_agent',
        'created_at',
    ];

    protected function casts(): array
    {
        return [
            'tenant_id' => 'integer',
            'user_id' => 'integer',
            'deal_id' => 'integer',
            'context' => 'array',
            '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);
    }
}
