<?php

namespace App\Models\Tenant;

use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class MarketingChannel extends Model
{
    use BelongsToTenant, SoftDeletes;

    protected $fillable = [
        'company_id', 'name', 'icon', 'color',
        'budget_monthly', 'spent_monthly',
        'leads_count', 'converted_count', 'revenue',
        'is_active', 'notes',
    ];

    protected $casts = [
        'budget_monthly' => 'decimal:2',
        'spent_monthly' => 'decimal:2',
        'revenue' => 'decimal:2',
        'is_active' => 'boolean',
    ];

    public function getRoiAttribute(): float
    {
        $spent = (float) $this->spent_monthly;
        if ($spent <= 0) {
            return 0;
        }
        return round((((float) $this->revenue - $spent) / $spent) * 100, 2);
    }

    public function getConversionRateAttribute(): float
    {
        $leads = (int) $this->leads_count;
        if ($leads <= 0) {
            return 0;
        }
        return round(((int) $this->converted_count / $leads) * 100, 2);
    }

    public function getCostPerLeadAttribute(): float
    {
        $leads = (int) $this->leads_count;
        if ($leads <= 0) {
            return 0;
        }
        return round((float) $this->spent_monthly / $leads, 2);
    }
}
