<?php

declare(strict_types=1);

namespace App\Models;

use App\Traits\MultiTenantScope;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Meta diaria de cobranza por collector.
 *
 * Una fila por (company_id, collector_user_id).
 * Sin fila → meta no configurada (goal = null, status = 'no_goal').
 */
class CompanyCollectorGoal extends Model
{
    use HasFactory, MultiTenantScope;

    protected $fillable = [
        'company_id',
        'collector_user_id',
        'daily_goal',
    ];

    protected $casts = [
        'daily_goal' => 'decimal:2',
    ];

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

    public function collector(): BelongsTo
    {
        return $this->belongsTo(User::class, 'collector_user_id');
    }

    /**
     * Devuelve el mapa [collector_user_id => daily_goal] para una empresa.
     * 1 query para todos los collectors.
     *
     * @return array<int, float>
     */
    public static function forCompany(int $companyId): array
    {
        return static::withoutGlobalScopes()
            ->where('company_id', $companyId)
            ->pluck('daily_goal', 'collector_user_id')
            ->map(fn ($v) => (float) $v)
            ->toArray();
    }
}
