<?php

namespace Modules\ProductCompat\Entities;

use Illuminate\Database\Eloquent\Model;

class PcDeviceModel extends Model
{
    protected $table = 'pc_device_models';

    protected $fillable = [
        'business_id', 'brand', 'series', 'model', 'normalized_key',
    ];

    /**
     * Build the de-dup key from brand|series|model.
     */
    public static function makeKey(?string $brand, ?string $series, string $model): string
    {
        $parts = array_map(function ($p) {
            return strtoupper(preg_replace('/\s+/', ' ', trim((string) $p)));
        }, [$brand, $series, $model]);

        // Bound the key so a long value can never collide on the (truncated)
        // unique index — legit device models are short anyway.
        return mb_substr(implode('|', $parts), 0, 180);
    }

    public function label(): string
    {
        return trim(implode(' ', array_filter([$this->brand, $this->series, $this->model])));
    }

    public function compatibilities()
    {
        return $this->hasMany(PcDeviceCompat::class, 'device_model_id');
    }
}
