<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

// [INTEGRATION] Consumable products used per service sale — mirrors Flutter ServiceProductUsageModel
class ServiceProductUsage extends Model
{
    use HasUlids, SoftDeletes;

    // Migration creates 'service_product_usage' (singular); declare explicitly to avoid auto-pluralization
    protected $table = 'service_product_usage';

    protected $fillable = [
        'user_id', 'store_id', 'local_id', 'service_id', 'product_id',
        'quantity_required', 'device_id', 'sync_version', 'is_deleted',
    ];

    protected $casts = [
        'quantity_required' => 'float',
        'is_deleted' => 'boolean',
        'sync_version' => 'integer',
    ];

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

    /** The service product (type='service') */
    public function service(): BelongsTo
    {
        return $this->belongsTo(Product::class, 'service_id');
    }

    /** The physical product consumed */
    public function product(): BelongsTo
    {
        return $this->belongsTo(Product::class, 'product_id');
    }
}
