<?php

namespace App\Models;

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

class JointInvestmentParticipant extends Model
{
    use HasFactory;

    protected $fillable = [
        'joint_investment_id',
        'user_id',
        'contribution_amount',
        'contribution_percentage',
        'joined_at',
        'status', // active, withdrawn, completed
        'profit_earned',
        'last_profit_distribution',
        'is_manager', // if user can manage the joint investment
        'management_permissions' // json field for specific permissions
    ];

    protected $casts = [
        'joined_at' => 'datetime',
        'last_profit_distribution' => 'datetime',
        'is_manager' => 'boolean',
        'management_permissions' => 'array'
    ];

    public function jointInvestment()
    {
        return $this->belongsTo(JointInvestment::class);
    }

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

    public function transactions()
    {
        return $this->hasMany(JointInvestmentTransaction::class);
    }

    public function getContributionPercentageAttribute($value)
    {
        if ($this->jointInvestment && $this->jointInvestment->target_amount > 0) {
            return ($this->contribution_amount / $this->jointInvestment->target_amount) * 100;
        }
        return $value;
    }
} 