<?php

namespace App\Models;

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

class Beneficiary extends Model
{
    use HasFactory;

    protected $fillable = [
        'company_employee_id',
        'first_name',
        'last_name',
        'address',
        'date_of_birth',
        'percentage_allocation',
        'utility_bill_path',
        'identification_path',
    ];

    protected function casts(): array
    {
        return [
            'date_of_birth' => 'date',
            'percentage_allocation' => 'decimal:2',
        ];
    }

    /**
     * Get the employee that owns the beneficiary.
     */
    public function companyEmployee(): BelongsTo
    {
        return $this->belongsTo(CompanyEmployee::class);
    }

    /**
     * Get full name of the beneficiary.
     */
    public function getFullNameAttribute(): string
    {
        return "{$this->first_name} {$this->last_name}";
    }

    /**
     * Get the authenticated URL for the utility bill file.
     * Files are stored on the private kyc disk — requires auth to access.
     */
    public function getUtilityBillUrlAttribute(): ?string
    {
        return $this->utility_bill_path
            ? url('/api/portal/kyc-file?path=' . rawurlencode($this->utility_bill_path))
            : null;
    }

    /**
     * Get the authenticated URL for the identification document file.
     * Files are stored on the private kyc disk — requires auth to access.
     */
    public function getIdentificationUrlAttribute(): ?string
    {
        return $this->identification_path
            ? url('/api/portal/kyc-file?path=' . rawurlencode($this->identification_path))
            : null;
    }
}
