<?php

declare(strict_types=1);

namespace App\Models;

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

final class InvestmentPerformance extends Model
{
    use HasFactory;

    protected $fillable = [
        'investment_property_id',
        'reporting_period',
        'occupancy_rate',
        'average_daily_rate',
        'total_revenue',
        'operating_expenses',
        'net_income',
        'property_value',
        'appreciation_rate',
    ];

    protected function casts(): array
    {
        return [
            'investment_property_id' => 'integer',
            'reporting_period' => 'date',
            'occupancy_rate' => 'decimal:2',
            'average_daily_rate' => 'decimal:2',
            'total_revenue' => 'decimal:2',
            'operating_expenses' => 'decimal:2',
            'net_income' => 'decimal:2',
            'property_value' => 'decimal:2',
            'appreciation_rate' => 'decimal:2',
        ];
    }

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

    /**
     * Calculate profit margin
     */
    public function profitMargin(): float
    {
        if ($this->total_revenue == 0) {
            return 0;
        }

        return ($this->net_income / $this->total_revenue) * 100;
    }
}
