<?php

namespace App\Models;

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

class Investment extends Model
{
    use HasFactory;

    protected $fillable = [
        'uuid',
        'investment_no',
        'customer_id',
        'product_id',
        'account_id',
        'principal',
        'interest_rate',
        'tenor_days',
        'start_date',
        'maturity_date',
        'expected_interest',
        'interest_paid',
        'status',
        'created_by',
        'authorized_by',
    ];

    protected static function boot(): void
    {
        parent::boot();

        static::creating(function (self $investment): void {
            if (empty($investment->uuid)) {
                $investment->uuid = (string) Str::uuid();
            }
        });
    }

    protected function casts(): array
    {
        return [
            'principal' => 'decimal:2',
            'interest_rate' => 'decimal:4',
            'expected_interest' => 'decimal:2',
            'interest_paid' => 'decimal:2',
            'start_date' => 'date',
            'maturity_date' => 'date',
        ];
    }

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

    public function investmentProduct(): BelongsTo
    {
        return $this->belongsTo(InvestmentProduct::class, 'product_id');
    }

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