<?php

namespace App\Models;

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

class CompanyHealthMetric extends Model
{
    use HasFactory;

    protected $fillable = [
        'company_id',
        'metric_date',
        'cash_balance',
        'monthly_revenue',
        'monthly_expenses',
        'profit_margin',
        'runway_days',
        'debt_to_equity',
        'credit_score',
        'liquidity_ratio',
        'additional_metrics',
    ];

    protected $casts = [
        'metric_date' => 'date',
        'cash_balance' => 'decimal:2',
        'monthly_revenue' => 'decimal:2',
        'monthly_expenses' => 'decimal:2',
        'profit_margin' => 'decimal:2',
        'debt_to_equity' => 'decimal:4',
        'liquidity_ratio' => 'decimal:2',
        'additional_metrics' => 'array',
    ];

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

    public function getHealthStatusAttribute()
    {
        if ($this->runway_days === null || $this->cash_balance <= 0) {
            return 'critical';
        }

        if ($this->runway_days < 30) {
            return 'poor';
        }

        if ($this->runway_days < 90) {
            return 'fair';
        }

        if ($this->runway_days < 180) {
            return 'good';
        }

        return 'excellent';
    }
}
