<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Investor extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'investor_number','entity_type','name','company_name','email','phone',
        'address','country','nationality','date_of_birth','id_number','id_type',
        'photo_path','id_document_path','investment_type','money_amount',
        'asset_description','asset_value','intellectual_description','intellectual_value',
        'total_investment_value','equity_percentage','shares_count','share_price',
        'expected_dividend_rate','dividend_frequency','company_id','status',
        'partner_type','investment_date','exit_date','notes','recorded_by',
    ];

    protected $casts = [
        'money_amount'           => 'decimal:2',
        'asset_value'            => 'decimal:2',
        'intellectual_value'     => 'decimal:2',
        'total_investment_value' => 'decimal:2',
        'equity_percentage'      => 'decimal:4',
        'shares_count'           => 'integer',
        'share_price'            => 'decimal:2',
        'expected_dividend_rate' => 'decimal:4',
        'date_of_birth'          => 'date',
        'investment_date'        => 'date',
        'exit_date'              => 'date',
    ];

    public function company()
    {
        return $this->belongsTo(Company::class);
    }

    public function payments()
    {
        return $this->hasMany(InvestorPayment::class);
    }

    public function recorder()
    {
        return $this->belongsTo(User::class, 'recorded_by');
    }

    public function getTotalPaidAttribute(): float
    {
        return (float) $this->payments()->sum('amount');
    }
}
