<?php
namespace App\Models;

use App\Core\Model;
use PDO;

class PipelineStage extends Model {
    protected static string $table = 'pipeline_stages';

    public static function getOrdered(): array {
        return self::all('order_index ASC, id ASC');
    }

    public static function getStagesWithDeals(): array {
        $stages = self::getOrdered();
        foreach ($stages as &$stage) {
            $stage['deals'] = Deal::getByStage((int)$stage['id']);
            $stage['total_value'] = array_sum(array_column($stage['deals'], 'value'));
            $stage['count'] = count($stage['deals']);
        }
        return $stages;
    }
}
