<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class CompanyGroup extends Model
{
    protected $fillable = ['group_code', 'name'];

    public function companies()
    {
        return $this->hasMany(Company::class);
    }

    public static function nextGroupCode(): string
    {
        $lastId = static::max('id') ?? 0;

        return 'GRP-' . str_pad((string) ($lastId + 1), 5, '0', STR_PAD_LEFT);
    }

    public function memberCompanyNames(): string
    {
        return $this->companies()->orderBy('name')->pluck('name')->implode(', ') ?: '—';
    }
}
