<?php

namespace Modules\ProductCompat\Entities;

use App\Product;
use Illuminate\Database\Eloquent\Model;

class PcPartNumber extends Model
{
    protected $table = 'pc_part_numbers';

    protected $fillable = [
        'business_id', 'product_id', 'part_number', 'normalized', 'type',
        'is_primary', 'source', 'confidence', 'needs_review', 'verified_at', 'verified_by',
    ];

    protected $casts = [
        'is_primary'   => 'boolean',
        'needs_review' => 'boolean',
        'verified_at'  => 'datetime',
    ];

    public const TYPES = ['mpn', 'oem', 'model_code', 'internal', 'other'];

    /**
     * Normalize a raw part number for de-dup / cross-product matching.
     */
    public static function normalize(string $value): string
    {
        return strtoupper(preg_replace('/[^A-Za-z0-9]/', '', $value));
    }

    public function product()
    {
        return $this->belongsTo(Product::class, 'product_id');
    }
}
