<?php

namespace App\Domain\Compliance\Models;

use App\Domain\Compliance\Enums\ConsentType;
use App\Domain\Identity\Models\Person;
use App\Domain\Participant\Models\Participant;
use App\Domain\Shared\Traits\BelongsToAcademy;
use App\Domain\Shared\Traits\HasUuid;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * One statement, about one document version, at one moment.
 *
 * Append-only, enforced by a database trigger. Withdrawal is a new row with
 * `granted = false`; editing the old one would destroy the only thing that
 * makes it evidence.
 */
class Consent extends Model
{
    use HasUuid, BelongsToAcademy;

    public const UPDATED_AT = null;

    protected $fillable = [
        'academy_id', 'participant_id', 'given_by', 'person_id',
        'type', 'granted', 'document_version', 'document_hash',
        'granted_at', 'ip_address', 'user_agent', 'metadata',
    ];

    protected function casts(): array
    {
        return [
            'type' => ConsentType::class,
            'granted' => 'boolean',
            'granted_at' => 'datetime',
            'metadata' => 'array',
        ];
    }

    public function participant(): BelongsTo
    {
        return $this->belongsTo(Participant::class);
    }

    public function giver(): BelongsTo
    {
        return $this->belongsTo(User::class, 'given_by');
    }

    public function person(): BelongsTo
    {
        return $this->belongsTo(Person::class);
    }
}
