<?php

namespace App\Domain\Identity\Models;

use App\Domain\Shared\Traits\Auditable;
use App\Domain\Shared\Traits\BelongsToAcademy;
use App\Domain\Shared\Traits\HasUuid;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Guardian extends Model
{
    use BelongsToAcademy, HasUuid, Auditable;

    protected $fillable = [
        'academy_id', 'person_id', 'user_id',
        'relationship_type', 'occupation', 'workplace',
        'can_pickup', 'is_emergency_contact', 'is_financial_responsible',
        'notes',
    ];

    protected $casts = [
        'can_pickup' => 'boolean',
        'is_emergency_contact' => 'boolean',
        'is_financial_responsible' => 'boolean',
    ];

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

    public function user(): BelongsTo
    {
        return $this->belongsTo(\App\Models\User::class);
    }

    public function participants(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
    {
        return $this->belongsToMany(\App\Domain\Participant\Models\Participant::class, 'guardian_participant')
            ->withPivot('relationship_type', 'is_primary', 'is_emergency_contact', 'can_pickup', 'receives_notifications', 'can_authorize_payment', 'created_at');
    }

    public function getNameArAttribute(): string
    {
        return $this->person?->name_ar ?? '';
    }

    public function getPhoneAttribute(): string
    {
        return $this->person?->phone ?? '';
    }
}
