<?php

namespace Modules\SystemCore\App\Models;

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

class CorePurchaseOrderLine extends Model
{
    use SoftDeletes;

    protected $table = 'core_purchase_order_lines';

    protected $fillable = [
        'purchase_order_id',
        'product_id',
        'product_variant_id',
        'unit_id',
        'product_name',
        'product_sku',
        'quantity',
        'weight',
        'unit_cost',
        'tax_rate',
        'tax_amount',
        'discount_rate',
        'discount_amount',
        'subtotal',
        'line_total',
        'received_quantity',
        'is_received',
        'accepted_quantity',
        'rejected_quantity',
        'rejection_reason',
        'created_by_id',
        'updated_by_id',
    ];

    protected function casts(): array
    {
        return [
            'quantity' => 'decimal:2',
            'weight' => 'decimal:2',
            'unit_cost' => 'decimal:4',
            'tax_rate' => 'decimal:2',
            'tax_amount' => 'decimal:2',
            'discount_rate' => 'decimal:2',
            'discount_amount' => 'decimal:2',
            'subtotal' => 'decimal:2',
            'line_total' => 'decimal:2',
            'is_received' => 'boolean',
        ];
    }

    /**
     * Get purchase order
     */
    public function purchaseOrder(): BelongsTo
    {
        return $this->belongsTo(CorePurchaseOrder::class, 'purchase_order_id');
    }

    /**
     * Get product
     */
    public function product(): BelongsTo
    {
        return $this->belongsTo(CoreProduct::class, 'product_id');
    }

    /**
     * Get product variant
     */
    public function productVariant(): BelongsTo
    {
        return $this->belongsTo(CoreProductVariant::class, 'product_variant_id');
    }

    /**
     * Get tax configuration
     */
    public function taxConfiguration(): BelongsTo
    {
        return $this->belongsTo(CoreTaxConfiguration::class, 'tax_id');
    }
}
